I have an array that stores range addresses. Is there a way I can select all cells that are in the array? Something like this.
The error occurs on the red line above. I seems like the Union property won't work this way. Is there a simple solution to this? Thanks
Code:
Sub FillDownList()On Error Resume Next
Dim rng1, rng2, rng3 As String, x As Integer, MyArray As Variant, SelectionArray(1 To 90) As Variant
Application.ScreenUpdating = False
Counter = 1
MyArray = Array(19, 51, 83, 115, 147, 179, 211, 243, 275, 307, 339, 371, 403, 435, 467)
For x = 7 To 467 Step 2
'Checks to see if Array contains the value of x
If Contains(MyArray, x) Then
' Stop
SelectionArray(Counter) = Range(Cells(x, 3), Cells(x, 5)).Address
Debug.Print x & vbNewLine & "End of Page "
x = x + 20
Else
SelectionArray(Counter) = Range(Cells(x, 3), Cells(x, 5)).Address
' SelectionArray(Counter) = x
Debug.Print x
End If
Counter = Counter + 1
Next x
For y = LBound(SelectionArray, 1) To UBound(SelectionArray, 1)
[COLOR=#ff0000]Union(Range(SelectionArray(y))).Select[/COLOR]
Next y
With Selection.Validation
.Delete
.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
xlBetween, Formula1:="=" & rng1
.IgnoreBlank = True
.InCellDropdown = True
.InputTitle = ""
.ErrorTitle = ""
.InputMessage = ""
.ErrorMessage = ""
.ShowInput = True
.ShowError = False
End With
Range("$G$1:$H$1").Select
ReagentPreparation.Activate
cell.Select
ReagentPreparation.Protect DrawingObjects:=False, Contents:=True, Scenarios:=False, AllowFormattingCells:=True, AllowFormattingColumns:=True, _
AllowFormattingRows:=True, AllowInsertingColumns:=True, AllowInsertingRows:=True, AllowDeletingColumns:=True, AllowDeletingRows:=True
Application.ScreenUpdating = True
End Sub
Function Contains(arr, v) As Boolean
Dim rv As Boolean, lb As Long, ub As Long, i As Long
lb = LBound(arr)
ub = UBound(arr)
For i = lb To ub
If arr(i) = v Then
rv = True
Exit For
End If
Next i
Contains = rv
End Function
The error occurs on the red line above. I seems like the Union property won't work this way. Is there a simple solution to this? Thanks
Last edited: