I'm writing some code in Excel to scan a row for a particular value, then get the cell address of that value.
lngCol = 0
search_start = "D1"
Do While current_week <> week_one
lgnCol = lngCol + 1
current_week = Range(search_start).Offset(0, lngCol)
Loop
wk_one_col = current_week.Address
Debug.Print wk_one_col
The code is mostly working and I'm not getting any errors, but the lngCol
variable isn't incrementing as it should be.
In the loop, lngCol
is always 1 after the first loop and lngCol+1
is adding 1 to the original value of the variable, which is zero.
What I'm not understanding is, each loop should be incrementing it. So first loop, it adds 1 to zero, setting lngCol
to 1. Second loop, it adds another 1 to 1, making it 2.
I have another function this idea is based off and it works as expected. Each loop the lngCol
variable increments by 1.
I know I know I need the variable itself to be out of scope from the loop, otherwise it'll keep resetting itself to zero every time. Where am I going wrong here?