baronbeef
baronbeef

Reputation: 23

VBA Question about Set same value and finding the same value

I am looking at some VBA code but I am confused about this line.

Set rngLookup = CurrWPWkSht.Columns("A").Find(rngLookup, lookat:=xlPart)

Does this line mean that the code is finding rngLookup in column A and setting it in the same variable?

Upvotes: 2

Views: 56

Answers (1)

Christofer Weber
Christofer Weber

Reputation: 1474

Not really

So rngLookup should be a range variable. Using a range, the default interpretation is Range.Value, so if you say Msgbox Range("A") that would return the same as Msgbox Range("A").value

Set rngLookup = CurrWPWkSht.Columns("A").Find(rngLookup.value, lookat:=xlPart)

Should work the same.
It's not uncommon to use a variable to change the same variable, as for a counter type variable: i = i + 1

In this case we are searching for the value in the range specified in rngLookup, and then set the result of the search as the new range for the variable, replacing the old range.

.Find returns a range, hence the set and the fact that rngLookup is a range variable. And while the range stored in the variable has a .value property the variable stores the whole range, with all range properties.

So
We're not really searching for a value and then setting it to that same value.
We're searching for a value, and then setting it to the range where the value was found.

Upvotes: 3

Related Questions