1

if i have an 1 dimensional array and each element of the array carry cell address.

Now I want to select those cells in a worksheet of carrying these addresses
Hint the selection should be multi selection of all addresses Something like .select property
In order to do use the selection for further operations like selection.copy and so on

Community
  • 1
  • 1
walid attia
  • 25
  • 2
  • 6

2 Answers2

3

If your array is in a variable like "arr", then you could use something like this;

dim rng as Range

set rng=Sheet1.Range(Join(arr,","))

Now rng has will have your address in your array. But bear in mind, some operations such as copy may not be possible on such multiple selections. But could certainly do some formatting

gr8tech
  • 174
  • 1
  • 7
  • 1
    Can you provide more information. Did you get some sort of error and what did you intend to do with the range. – gr8tech Apr 21 '17 at 16:24
2

You would loop through the array and use Union:

Sub foo()
Dim rng As Range
Dim arr()
Dim i As Long

arr = Array("$A$1", "$CD$5", "$F$4", "$H$8")

For i = LBound(arr) To UBound(arr)
    If i = LBound(arr) Then
        Set rng = Worksheets("Sheet1").Range(arr(i))
    Else
        Set rng = Union(Worksheets("Sheet1").Range(arr(i)), rng)
    End If
Next i

Debug.Print rng.Address(0, 0, xlA1, 1)
End Sub
Scott Craner
  • 148,073
  • 10
  • 49
  • 81