【问题标题】:Set range as a single cell relative to active cell将范围设置为相对于活动单元格的单个单元格
【发布时间】:2013-09-19 05:43:08
【问题描述】:

当我尝试将范围定义为相对于当前单元格的单个单元格时,我收到一个错误,指出范围失败:

If target.Cells.Column = 2 Then
    If target.Cells.Row > 3 Then
        If target.Cells.Count = 1 Then
        Range(ActiveCell.Offset(0, 1)).Select
        End If
    End If
End If

我知道我可以在当前单元格中创建范围,然后进行偏移,但我只想定义范围

【问题讨论】:

  • 你没有在你的代码中定义一个范围,你的选择一个。什么是目标?这是在 WorkSheet_Change 或 WorkSheet_SelectionChange 事件中吗?
  • 请您解释一下您想要的结果。目前您正在选择一个单元格,但随后不对其进行任何操作。您的目标是返回范围变量、返回表示地址的字符串变量还是做其他事情?
  • Range 参数要么需要作为文本输入,要么需要为要编译的代码提供第二个引用。在这种情况下,无论如何它都是多余的,因为您可以使用:ActiveCell.Offset(0, 1).Select

标签: excel vba range offset


【解决方案1】:
Dim c As Range

If target.Cells.Count = 1 Then
    If target.Row > 3 and target.Column = 2 Then
        Set c = target.Offset(0, 1)
        'do something with c
    End If
End If

【讨论】:

    猜你喜欢
    • 2017-03-30
    • 1970-01-01
    • 2014-10-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多