Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I want to define a variable in a for loop as a specific cell that will change as the for loop iterates through. I am just unsure of the syntax to do so. This is what I have so far. How could I make this do what I just explained? Any help would be greatly appreciated.

Key = Sheet1.Columns("A:A").Rows("i")
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
363 views
Welcome To Ask or Share your Answers For Others

1 Answer

If you need to refer to the cell as a Range object, then:

Dim Key as Range
Set Key = Sheet1.Range("A" & i)

You must use the Set keyword when assigning to an object variable. A Range is an object.

If you need to refer only to the cell's value, then:

Dim Key as Variant
Key = Sheet1.Range("A" & i)

I declare Key as type Variant because cells may contain error values/etc. which will cause an error if you strictly define the variable as type like String or Long, etc.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...