【问题标题】:Referencing indirect cells in excel VBA在excel VBA中引用间接单元格
【发布时间】:2018-05-12 23:31:51
【问题描述】:

所以我试图让我的代码中的单元格引用成为间接引用。例如,我想更新 B 列单元格“X”中的值,其中 X 在单元格 B1 中定义。 这是我目前正在尝试的代码,但我不断收到超出范围的异常。我对 VBA 很陌生,所以我的语法可能离得很远。

Workbooks("Personal_Finances").Sheets(categoryType).Range("$B($B$1)").Value = ammount

【问题讨论】:

    标签: vba excel


    【解决方案1】:

    试试,

    with Workbooks("Personal_Finances").Sheets(categoryType)
        .cells(.Range("B1").Value, "B") = ammount
        'alternate
        .Range("B" & .Range("B1").Value) = ammount
    end with
    

    这里 .Range("B1").Value 用于 .Cells 中的行引用。替代方案更接近您最初的尝试。

    我已将工作代码封装在 With ... End With 块中以维护父工作表引用。

    除非在一次填充多个单元格的公式中使用,否则在带引号的字符串单元格引用中不需要 $

    【讨论】:

      【解决方案2】:

      定义一个新变量并将单元格 $b$1 的值分配给它。

      dim temp_row as integer
      temp_row.value =Workbooks("Personal_Finances").Sheets("categoryType").Range("B1").Value
      Workbooks("Personal_Finances").Sheets(categoryType).Range("$B" & temp_row).Value = amount
      

      或者只是在一行中做同样的事情。

      【讨论】:

        【解决方案3】:

        或者:

        With Workbooks("Personal_Finances").Sheets(categoryType).Range("B1")
            .Offset(.Value2 - 1) = ammount
        End With
        

        “With ... End With”块在通缉工作簿中引用通缉工作表的单元格 B1 并且嵌套语句将其偏移其值减一(以到达通缉行)

        【讨论】:

        • 我很感激勇敢和蒙面的投票者说出我的答案有什么问题并有助于增强我的答案。但恐怕他没那么勇敢,会一直蒙面
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2016-08-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-03-31
        相关资源
        最近更新 更多