Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
373 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 Reply

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...