Excel vba 正在更改我设置的范围
Excel vba is changing the Range I set
我试图设置以下范围:
Range("F3:F102,J3:J102,N3:N102,R3:R102,V3:V102,Z3:Z102,AD3:AD102,AH3:AH102,AL3:AL102,AP3:AP102,AT3:AT102,AX3:AX102,BB3:BB102,BF3:BF102,BJ3:BJ102,BN3:BN102,BR3:BR102,BV3:BV102,BZ3:BZ102,CD3:CD102")
我尝试使用 For 循环和 Application.Union
迭代构建它,而不是手动输入范围,因为我必须构建许多类似的范围,而我将永远输入它们,所以我使用了以下代码:
Sub Build_Range()
Dim FirstParamCol, ParamCells As Range
Dim i As Integer
Application.ScreenUpdating = False
Application.EnableEvents = False 'avoid infinite loop if any target cell is changed
Set ParamCells = Range("F3:F102") 'There 20 different "Parameter" columns, the first one being Column F (column 6)
'Note: After column F (6), the other 19 columns are 4 columns apart each (j/10, N/14, ...)
'so I looped for i = 1 to 19 and used the formula column = 6 (column F) + i * 4:
For i = 1 To 19 'There are other 19 "Parameter" columns
Set ParamCells = Application.union(ParamCells, Range(ActiveSheet.Cells(3, 6 + 4 * i), ActiveSheet.Cells(102, 6 + 4 * i)))
Next i
MsgBox ParamCells.Address 'TODO: For Debugging only
Range("B103").Value = ParamCells.Address
Exitsub:
Application.EnableEvents = True
Application.ScreenUpdating = True
End Sub
在 运行 上面的代码之后,我得到以下范围,缺少最后一列 ("CD3:CD102")
:
Range("F3:F102,J3:J102,N3:N102,R3:R102,V3:V102,Z3:Z102,AD3:AD102,AH3:AH102,AL3:AL102,AP3:AP102,AT3:AT102,AX3:AX102,BB3:BB102,BF3:BF102,BJ3:BJ102,BN3:BN102,BR3:BR102,BV3:BV102,BZ3:BZ102")
我尝试增加 For 循环中的最后一个 i 值,但我一直得到与上面相同的范围。
我什至尝试了以下代码来手动设置范围,但我得到的范围与上面相同,再次遗漏了最后一列:
Sub Build_Range_2()
Dim ParamCells As Range
Set ParamCells = Range("F3:F102,J3:J102,N3:N102,R3:R102,V3:V102,Z3:Z102,AD3:AD102,AH3:AH102,AL3:AL102,AP3:AP102,AT3:AT102,AX3:AX102,BB3:BB102,BF3:BF102,BJ3:BJ102,BN3:BN102,BR3:BR102,BV3:BV102,BZ3:BZ102,CD3:CD102")
MsgBox ParamCells.Address
Range("B103").Value = ParamCells.Address
End Sub
在我手动设置 Rage 后仍然 excel/vba 改变了范围,我感到很沮丧。我搜索了看它是否是范围的最大大小的某种限制或类似的东西,但我找不到任何东西。
澄清更新:我正在 MsgBox 和 Range("B103").Value 上打印构建的范围,仅用于调试目的(我不需要显示范围但要使用它/与它)。
首先,好消息是您构建该范围的代码工作正常!
插入 ParamCells.Select
并检查它选择的内容。即使 "CD3:CD102"
也没有出现在地址 ParamCells.Address
中,它是范围 ParamCells
.
的一部分
问题是 .Address
限制为 255 个字符。
你可以很容易地检查,如果你比较
Debug.Print ParamCells.Address
Debug.Print ParamCells.Address(False, False)
$F:$F2,$J:$J2,$N:$N2,$R:$R2,$V:$V2,$Z:$Z2,$AD:$AD2,$AH:$AH2,$AL:$AL2,$AP:$AP2,$AT:$AT2,$AX:$AX2,$BB:$BB2,$BF:$BF2,$BJ:$BJ2,$BN:$BN2,$BR:$BR2,$BV:$BV2,$BZ:$BZ2
F3:F102,J3:J102,N3:N102,R3:R102,V3:V102,Z3:Z102,AD3:AD102,AH3:AH102,AL3:AL102,AP3:AP102,AT3:AT102,AX3:AX102,BB3:BB102,BF3:BF102,BJ3:BJ102,BN3:BN102,BR3:BR102,BV3:BV102,BZ3:BZ102,CD3:CD102
第一个有253个字符,加上CD3:CD102
会超过255个,所以被截断了。第二个没有绝对地址,因此它更短,所以最后你可以看到 …,BZ3:BZ102,CD3:CD102
CD3:CD102
实际上在范围内。
因此,无论何时使用 ParamCells
,无论 ParamCells.Address
被截断成什么,它都适用于整个范围。
所以问题是你需要显示地址吗?然后你需要一些解决方法(取决于你的实际目标是什么)。或者只是为了调试您的代码然后使用 ParamCells.Select
来检查它。
如果你想输出ParamCells
的实际地址用于验证,你可以通过循环ParamCells.Areas
.
来实现
Dim AddrOfParamCells As String
Dim Area As Range
For Each Area In ParamCells.Areas
AddrOfParamCells = IIf(AddrOfParamCells <> vbNullString, AddrOfParamCells & ",", vbNullString) & Area.Address
Next Area
Debug.Print AddrOfParamCells
请注意,此地址只能用于验证,您不能使用它来构建类似Set TestRange = Range(AddrOfParamCells)
的范围,因为它超过了 255 个字符。
.Address 限制为 255 个字符...但您可以使用字符串处理。
Sub Build_Range()
Dim FirstParamCol As Range
Dim ParamCells As Range
Dim i As Integer
Dim StrRange As String 'String to store the address of ParamCells
Application.ScreenUpdating = False
Application.EnableEvents = False
'Is not a good practice to hard code... but it is your code, and
'You will debug this... in the future!
Set ParamCells = Range("F3:F102")
StrRange = ParamCells.Address(1, 1, xlA1, 0, 0)
'To find some help in .Address:
'https://docs.microsoft.com/en-us/office/vba/api/excel.range.address
For i = 1 To 19
'Here you store the address.
'Again, don't like to hard code... but the & "," & in this point it is necessary
StrRange = StrRange & "," & Range(ActiveSheet.Cells(3, 6 + 4 * i), ActiveSheet.Cells(102, 6 + 4 * i)).Address(1, 1, xlA1, 0, 0)
'I Keep youy var, if you want to do something with the range.
'You can not print the full address in the immediate window, but
'You can still use the full range.
Set ParamCells = Application.Union(ParamCells, Range(ActiveSheet.Cells(3, 6 + 4 * i), ActiveSheet.Cells(102, 6 + 4 * i)))
Next i
'Just to check if is working.
'ParamCells.Select
MsgBox ParamCells.Address 'TODO: For Debugging only
'No use this
'Range("B103").Value = ParamCells.Address
'Use this:
Range("B103").Value = ParamCells.Address
Exitsub:
Application.EnableEvents = True
Application.ScreenUpdating = True
End Sub
我试图设置以下范围:
Range("F3:F102,J3:J102,N3:N102,R3:R102,V3:V102,Z3:Z102,AD3:AD102,AH3:AH102,AL3:AL102,AP3:AP102,AT3:AT102,AX3:AX102,BB3:BB102,BF3:BF102,BJ3:BJ102,BN3:BN102,BR3:BR102,BV3:BV102,BZ3:BZ102,CD3:CD102")
我尝试使用 For 循环和 Application.Union
迭代构建它,而不是手动输入范围,因为我必须构建许多类似的范围,而我将永远输入它们,所以我使用了以下代码:
Sub Build_Range()
Dim FirstParamCol, ParamCells As Range
Dim i As Integer
Application.ScreenUpdating = False
Application.EnableEvents = False 'avoid infinite loop if any target cell is changed
Set ParamCells = Range("F3:F102") 'There 20 different "Parameter" columns, the first one being Column F (column 6)
'Note: After column F (6), the other 19 columns are 4 columns apart each (j/10, N/14, ...)
'so I looped for i = 1 to 19 and used the formula column = 6 (column F) + i * 4:
For i = 1 To 19 'There are other 19 "Parameter" columns
Set ParamCells = Application.union(ParamCells, Range(ActiveSheet.Cells(3, 6 + 4 * i), ActiveSheet.Cells(102, 6 + 4 * i)))
Next i
MsgBox ParamCells.Address 'TODO: For Debugging only
Range("B103").Value = ParamCells.Address
Exitsub:
Application.EnableEvents = True
Application.ScreenUpdating = True
End Sub
在 运行 上面的代码之后,我得到以下范围,缺少最后一列 ("CD3:CD102")
:
Range("F3:F102,J3:J102,N3:N102,R3:R102,V3:V102,Z3:Z102,AD3:AD102,AH3:AH102,AL3:AL102,AP3:AP102,AT3:AT102,AX3:AX102,BB3:BB102,BF3:BF102,BJ3:BJ102,BN3:BN102,BR3:BR102,BV3:BV102,BZ3:BZ102")
我尝试增加 For 循环中的最后一个 i 值,但我一直得到与上面相同的范围。
我什至尝试了以下代码来手动设置范围,但我得到的范围与上面相同,再次遗漏了最后一列:
Sub Build_Range_2()
Dim ParamCells As Range
Set ParamCells = Range("F3:F102,J3:J102,N3:N102,R3:R102,V3:V102,Z3:Z102,AD3:AD102,AH3:AH102,AL3:AL102,AP3:AP102,AT3:AT102,AX3:AX102,BB3:BB102,BF3:BF102,BJ3:BJ102,BN3:BN102,BR3:BR102,BV3:BV102,BZ3:BZ102,CD3:CD102")
MsgBox ParamCells.Address
Range("B103").Value = ParamCells.Address
End Sub
在我手动设置 Rage 后仍然 excel/vba 改变了范围,我感到很沮丧。我搜索了看它是否是范围的最大大小的某种限制或类似的东西,但我找不到任何东西。
澄清更新:我正在 MsgBox 和 Range("B103").Value 上打印构建的范围,仅用于调试目的(我不需要显示范围但要使用它/与它)。
首先,好消息是您构建该范围的代码工作正常!
插入 ParamCells.Select
并检查它选择的内容。即使 "CD3:CD102"
也没有出现在地址 ParamCells.Address
中,它是范围 ParamCells
.
问题是 .Address
限制为 255 个字符。
你可以很容易地检查,如果你比较
Debug.Print ParamCells.Address
Debug.Print ParamCells.Address(False, False)
$F:$F2,$J:$J2,$N:$N2,$R:$R2,$V:$V2,$Z:$Z2,$AD:$AD2,$AH:$AH2,$AL:$AL2,$AP:$AP2,$AT:$AT2,$AX:$AX2,$BB:$BB2,$BF:$BF2,$BJ:$BJ2,$BN:$BN2,$BR:$BR2,$BV:$BV2,$BZ:$BZ2
F3:F102,J3:J102,N3:N102,R3:R102,V3:V102,Z3:Z102,AD3:AD102,AH3:AH102,AL3:AL102,AP3:AP102,AT3:AT102,AX3:AX102,BB3:BB102,BF3:BF102,BJ3:BJ102,BN3:BN102,BR3:BR102,BV3:BV102,BZ3:BZ102,CD3:CD102
第一个有253个字符,加上CD3:CD102
会超过255个,所以被截断了。第二个没有绝对地址,因此它更短,所以最后你可以看到 …,BZ3:BZ102,CD3:CD102
CD3:CD102
实际上在范围内。
因此,无论何时使用 ParamCells
,无论 ParamCells.Address
被截断成什么,它都适用于整个范围。
所以问题是你需要显示地址吗?然后你需要一些解决方法(取决于你的实际目标是什么)。或者只是为了调试您的代码然后使用 ParamCells.Select
来检查它。
如果你想输出ParamCells
的实际地址用于验证,你可以通过循环ParamCells.Areas
.
Dim AddrOfParamCells As String
Dim Area As Range
For Each Area In ParamCells.Areas
AddrOfParamCells = IIf(AddrOfParamCells <> vbNullString, AddrOfParamCells & ",", vbNullString) & Area.Address
Next Area
Debug.Print AddrOfParamCells
请注意,此地址只能用于验证,您不能使用它来构建类似Set TestRange = Range(AddrOfParamCells)
的范围,因为它超过了 255 个字符。
.Address 限制为 255 个字符...但您可以使用字符串处理。
Sub Build_Range()
Dim FirstParamCol As Range
Dim ParamCells As Range
Dim i As Integer
Dim StrRange As String 'String to store the address of ParamCells
Application.ScreenUpdating = False
Application.EnableEvents = False
'Is not a good practice to hard code... but it is your code, and
'You will debug this... in the future!
Set ParamCells = Range("F3:F102")
StrRange = ParamCells.Address(1, 1, xlA1, 0, 0)
'To find some help in .Address:
'https://docs.microsoft.com/en-us/office/vba/api/excel.range.address
For i = 1 To 19
'Here you store the address.
'Again, don't like to hard code... but the & "," & in this point it is necessary
StrRange = StrRange & "," & Range(ActiveSheet.Cells(3, 6 + 4 * i), ActiveSheet.Cells(102, 6 + 4 * i)).Address(1, 1, xlA1, 0, 0)
'I Keep youy var, if you want to do something with the range.
'You can not print the full address in the immediate window, but
'You can still use the full range.
Set ParamCells = Application.Union(ParamCells, Range(ActiveSheet.Cells(3, 6 + 4 * i), ActiveSheet.Cells(102, 6 + 4 * i)))
Next i
'Just to check if is working.
'ParamCells.Select
MsgBox ParamCells.Address 'TODO: For Debugging only
'No use this
'Range("B103").Value = ParamCells.Address
'Use this:
Range("B103").Value = ParamCells.Address
Exitsub:
Application.EnableEvents = True
Application.ScreenUpdating = True
End Sub