簡體   English   中英

在Excel VBA中使用“單元”功能

[英]Using the “Cells” function in excel vba

我正在嘗試通過使用range函數在使用vba的excel中選擇一個單元格。

在使用變量指定范圍而不只是數字來指定范圍之前,我已經做過很多次了。

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

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

下面是我用來嘗試選擇范圍(僅1個單元格)的語法。

另外,當我運行代碼時,我收到以下消息:

“運行時錯誤'1004':對象'_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

您正在像使用函數一樣使用Range類型。 實際上, 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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM