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

Categories

0 votes
378 views
in Technique[技术] by (71.8m points)

Find specific cell Excel VBA macro

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

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

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...