Selecting Cells from Array

smpatty08

Board Regular
Joined
May 16, 2014
Messages
155
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.
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:

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
Try
Code:
Dim Rng As Range
For y = LBound(SelectionArray, 1) To UBound(SelectionArray, 1)
If Rng Is Nothing Then Set Rng = Range(SelectionArray(y)) Else Set Rng = Union(Rng, Range(SelectionArray(y)))
Next y
Rng.Select
 
Upvote 0
Try
Code:
Dim Rng As Range
For y = LBound(SelectionArray, 1) To UBound(SelectionArray, 1)
If Rng Is Nothing Then Set Rng = Range(SelectionArray(y)) Else Set Rng = Union(Rng, Range(SelectionArray(y)))
Next y
Rng.Select

Thank you this works perfectly!
 
Upvote 0
Glad to help & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,216,101
Messages
6,128,837
Members
449,471
Latest member
lachbee

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top