vba 在excel vba中使用“单元格”功能
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17594402/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using the "Cells" function in excel vba
提问by user2572734
I am trying to select a cell in excel using vba through the use of the range function.
我正在尝试通过使用范围函数使用 vba 在 excel 中选择一个单元格。
I have done this many times before even with using variables to specify the range instead of just numbers.
即使使用变量来指定范围而不仅仅是数字,我也已经多次这样做了。
However, I cannot seem to select a range using the cells function.
但是,我似乎无法使用单元格功能选择范围。
I know that this is possible as I have done much research and have seen other people's code that will successfully run using this function.
我知道这是可能的,因为我已经做了很多研究并且已经看到其他人的代码可以使用这个函数成功运行。
Below is the syntax I'm using to try and select the range (which is only 1 cell).
下面是我用来尝试选择范围(只有 1 个单元格)的语法。
Additionally, when i run the code i get the following message:
此外,当我运行代码时,我收到以下消息:
"Run-time error '1004': Method of 'Range' of object'_Global' failed".
“运行时错误‘1004’:对象‘_Global’的‘范围’方法失败”。
Thank you for any and all help.
感谢您的任何帮助。
Dim item As String
Dim itempaste As Boolean
Dim itemcnt As Integer
itemcnt = 1
itempaste = False
Range("X3").Select
Do
item = ActiveCell.Value
If item <> "" Then
Range("A18").Select
Do
If ActiveCell.Value = "" Then
ActiveCell.Value = item
itempaste = True
Else
ActiveCell.Offset(1, 0).Select
End If
Loop Until itempaste = True
End If
itemcnt = itemcnt + 2
Range(Cells(1, (21 + itemcnt))).Select
Loop Until itemcnt = 11
回答by Geoff
You're using the Range type like a function; the Cells
call is actually returning you a Range
object already. Instead of
您像函数一样使用 Range 类型;该Cells
调用实际上已经返回给您一个Range
对象。代替
Range(Cells(1, (21 + itemcnt))).Select
..try:
..尝试:
Cells(1, (21 + itemcnt)).Select
If you want to be more explicit:
如果你想更明确:
Dim target As Range
Set target = Cells(1, (21 + itemcnt))
target.Select