Find specific cell Excel VBA macro

2019-09-22 01:03发布

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")

1条回答
叼着烟拽天下
2楼-- · 2019-09-22 01:47

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.

查看更多
登录 后发表回答