Why doesn't this work? If statements and filling an array

Luke777

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

This bit if code works perfectly

VBA Code:
        For Each cell In Range(Cells(searchRow, 3), Cells(searchRow, lastCol))
            If cell.Value = string1 Then s = s & "," & cell.Address
        Next cell

where string1 is found, that cell address is added to s. So far so good.

What I actually want to do is loop through a second if statement where n number of cells below the initial cell are checked which I thought would look like this

VBA Code:
        For Each cell In Range(Cells(searchRow, 3), Cells(searchRow, lastCol))
            If cell.Value = string1 Then
                For Y = 1 To tSlots
                    If cell.Offset(Y, 0).Value = string1 Then
                        tCheck = tCheck + 1
                    End If
                Next Y
            End If
            If tCheck = tSlots Then s = s & "," & cell.Address
        Next cell

tSlots is a number determined elsewhere and is the number of cells that need to contain the string in order for the first cell.address to be added to s. I expect just the original cell address to be added to s rather than a range of cell addresses.

regardless, when tCheck does = tSlots, nothing is added to s like it should be and I'm not sure why.

Any help on this one? :)

Thanks all!
 

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
If you're not getting anything added to s, then I don't think tCheck does equal tSlots.
 
Upvote 0
If you're not getting anything added to s, then I don't think tCheck does equal tSlots.
I've watched it in the locals window and stepped through the code - both values are 2
 
Upvote 0
If you're not getting anything added to s, then I don't think tCheck does equal tSlots.
I'm not sure why but i decided to check what data type they were - both matched (Double), so I decided to change this to Byte (its never going to be a long number) and it now works?

But the value matched beforehand so why did the data type matter?
 
Upvote 0

Forum statistics

Threads
1,214,918
Messages
6,122,249
Members
449,075
Latest member
staticfluids

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