在excel vba中使用“单元格”function

我想通过使用范围函数使用VBA在Excel中select一个单元格。

甚至在使用variables来指定范围而不是数字之前,我已经做了很多次了。

但是,我似乎无法使用单元格functionselect范围。

我知道这是可能的,因为我已经做了很多研究,并看到其他人的代码将成功运行使用此function。

下面是我用来尝试和select范围(这是只有一个单元格)的语法。

另外,当我运行代码时,我收到以下消息:

“运行时错误”1004“:对象'全局'的'范围'方法失败。

感谢您的帮助。

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 

你正在使用Rangetypes,就像一个函数; Cells格调用实际上已经返回给你一个Range对象。 代替

 Range(Cells(1, (21 + itemcnt))).Select 

..尝试:

 Cells(1, (21 + itemcnt)).Select 

如果你想更加明确:

 Dim target As Range Set target = Cells(1, (21 + itemcnt)) target.Select