【问题标题】:Using the "Cells" function in excel vba在excel vba中使用“单元格”功能
【发布时间】:2013-07-09 18:50:58
【问题描述】:

我正在尝试通过 range 函数使用 vba 在 excel 中选择一个单元格。

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

但是,我似乎无法使用单元格功能选择范围。

我知道这是可能的,因为我做了很多研究并看到其他人的代码可以使用这个函数成功运行。

以下是我用来尝试选择范围(只有 1 个单元格)的语法。

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

“运行时错误'1004':对象'_Global'的'Range'方法失败”。

感谢您的任何帮助。

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

【问题讨论】:

    标签: excel function vba range cell


    【解决方案1】:

    您正在像函数一样使用 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
    

    【讨论】:

      猜你喜欢
      • 2018-07-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多