2013-07-11 34 views
5

我想通過使用範圍函數使用vba在Excel中選擇一個單元格。在Excel中使用「單元格」功能vba

甚至在使用變量來指定範圍而不是數字之前,我已經完成了很多次。

但是,我似乎無法選擇使用單元格功能的範圍。

我知道這是可能的,因爲我已經做了大量的研究,並看到其他人的代碼將成功運行使用此功能。

下面是我用來嘗試和選擇範圍(它只有1個單元格)的語法。

此外,當我運行代碼我得到以下信息:

「運行時錯誤‘1004’:object'_Global的‘範圍’的方法失敗」。

謝謝你的任何和所有幫助。

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 

回答

5

您正在使用Range類型,如函數; Cells調用實際上已經返回給您一個Range對象。取而代之的

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

..try:

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

如果你想更明確的:

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