I am trying to set the following Range:
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")
I tried to build it iteratively using a For Loop and Application.Union, instead of typing the Range manually because I have to build many ranges like that one.
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
I get the following range, which is missing the last column ("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")
I tried increasing the last i value in the For Loop, but I get the same range as above.
I tried the following code to Set the range manually. I get the same range as above, again missing the last column:
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
I searched to see if it was a limit in the maximum size of a Range or something similar but I couldn't find anything.
I'm printing the built Range on a MsgBox and on Range("B103").Value, for debugging purposes only (I do not need to display the Range but to use it / work with it).