add cell address to array

Luke777

Board Regular
Joined
Aug 10, 2020
Messages
244
Office Version
  1. 365
Platform
  1. Windows
hi all,

How would I go about adding the address of a cell to an array?

To be specific, I'm testing cells in a range for a value (lets call it X). If that value is true Then add cell.address to array. I'm using an array because I would like to work with all cells where X = true later down the line. I *Think* creating an array is my best option as I cannot assign each its own variable as I do not know how many cells where this value will be true there are.

Thanks all!
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
You could adapt this method:
VBA Code:
Dim myArr()  As String

'to add the first item (0) when still empty
ReDim Preserve myArr(0)
myArr(0) = "$G$15"

'then keep adding with UBound(myArr) + 1
ReDim Preserve arr(UBound(myArr) + 1)
myArr(UBound(myArr)) = "$D$5"
 
Upvote 0
You could adapt this method:
VBA Code:
Dim myArr()  As String

'to add the first item (0) when still empty
ReDim Preserve myArr(0)
myArr(0) = "$G$15"

'then keep adding with UBound(myArr) + 1
ReDim Preserve arr(UBound(myArr) + 1)
myArr(UBound(myArr)) = "$D$5"
Thanks

I tried something like
VBA Code:
        For Each cell In Range(Cells(searchRow, 3), Cells(searchRow, lastCol))
            If Application.CountIf(cell.Resize(tSlots, 1), "teststring") = tSlots Then 'tSlots = 4 in the test but that shouldnt affect what I'm trying to do with the Then.
                ReDim Preserve myArray(UBound(myArray) + 1) 'I think here I'm missing the initial first item?
                myArray(UBound(myArray)) = cell.Address 
            Else: Cells(searchRow, lastCol + 1) = "false" 'convert to add 'ignore this line
            End If
        Next cell

but I can't see as I'm entering anything into my array correctly
 
Upvote 0
Thanks

I tried something like
VBA Code:
        For Each cell In Range(Cells(searchRow, 3), Cells(searchRow, lastCol))
            If Application.CountIf(cell.Resize(tSlots, 1), "teststring") = tSlots Then 'tSlots = 4 in the test but that shouldnt affect what I'm trying to do with the Then.
                ReDim Preserve myArray(UBound(myArray) + 1) 'I think here I'm missing the initial first item?
                myArray(UBound(myArray)) = cell.Address
            Else: Cells(searchRow, lastCol + 1) = "false" 'convert to add 'ignore this line
            End If
        Next cell

but I can't see as I'm entering anything into my array correctly
To
Thanks

I tried something like
VBA Code:
        For Each cell In Range(Cells(searchRow, 3), Cells(searchRow, lastCol))
            If Application.CountIf(cell.Resize(tSlots, 1), "teststring") = tSlots Then 'tSlots = 4 in the test but that shouldnt affect what I'm trying to do with the Then.
                ReDim Preserve myArray(UBound(myArray) + 1) 'I think here I'm missing the initial first item?
                myArray(UBound(myArray)) = cell.Address
            Else: Cells(searchRow, lastCol + 1) = "false" 'convert to add 'ignore this line
            End If
        Next cell

but I can't see as I'm entering anything into my array correctly
okay, fixed that to...

VBA Code:
With ws1
        For Each cell In Range(Cells(searchRow, 3), Cells(searchRow, lastCol))
            If Application.CountIf(cell.Resize(tSlots, 1), "Busiest Unhosted") = tSlots Then
                ReDim Preserve myArray(0)
                myArray(0) = cell.Address
            Else: Cells(searchRow, lastCol + 1) = "false" 'convert to add
            End If
        Next cell

Which always replaces the same part of the array with the next result address (improvement on not working at all). I'm a little unsure how to tell it if myArray(0) is full, then +1
 
Upvote 0
You missed the first part of my suggestion, Ubound can't be used on an empty array so this could be a solution:
VBA Code:
ReDim Preserve myArray(0)
For Each cell In Range(Cells(searchRow, 3), Cells(searchRow, lastCol))
    If Application.CountIf(cell.Resize(tSlots, 1), "teststring") = tSlots Then
        If myArray(0) = 0 Then                'to add the first item (0) when still empty
            myArray(0) = cell.Address
        Else
            ReDim Preserve myArray(UBound(myArray) + 1) 'then keep adding with UBound(myArr) + 1
            myArray(UBound(myArray)) = cell.Address
        End If
    Else
        Cells(searchRow, lastCol + 1) = "false" 'convert to add 'ignore this line
    End If
Next cell
 
Upvote 0
Solution
You missed the first part of my suggestion, Ubound can't be used on an empty array so this could be a solution:
VBA Code:
ReDim Preserve myArray(0)
For Each cell In Range(Cells(searchRow, 3), Cells(searchRow, lastCol))
    If Application.CountIf(cell.Resize(tSlots, 1), "teststring") = tSlots Then
        If myArray(0) = 0 Then                'to add the first item (0) when still empty
            myArray(0) = cell.Address
        Else
            ReDim Preserve myArray(UBound(myArray) + 1) 'then keep adding with UBound(myArr) + 1
            myArray(UBound(myArray)) = cell.Address
        End If
    Else
        Cells(searchRow, lastCol + 1) = "false" 'convert to add 'ignore this line
    End If
Next cell
Looks really promising but I'm getting a type mismatch on the line "If myArray(0) = 0 Then" for some reason - I'm not sure what error this is... its certainly finding the string and the array name is correct
 
Upvote 0
The array must be declared Variant, not String as I suggested in post #2.
VBA Code:
Dim myArray() As Variant
 
Upvote 0
Glad having been of some help(y). Sorry for the String but it was a generic suggestion.
 
Upvote 0

Forum statistics

Threads
1,214,643
Messages
6,120,702
Members
448,980
Latest member
CarlosWin

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