【问题标题】:Reference cell location with variable带变量的参考单元格位置
【发布时间】:2017-06-28 21:17:59
【问题描述】:

我需要用一个变量来引用一个单元格位置,这里是代码:

 Dim jj As Integer
 jj = 1
 Do While jj <= x
      if Cells(jj, 2) = "Some Criteria" Then Cells(jj, 3).Formula = "=Cells(jj,1)^2"
      jj = jj + 1
      Else
      jj = jj + 1
      End If
 Loop

问题是单元格 C1 中的值(即当 jj =1 时的单元格(jj, 3)时)字面上读取 =Cells(jj,1)^2 导致错误,因为 VBA 之外的 excel 无法识别cells(jj,3) 作为公式或任何类型的值。

我需要循环浏览向下移动的行。有没有办法做到这一点并将公式插入到引用单元格位置的单元格中,其中变量作为行?

谢谢!有什么帮助!

【问题讨论】:

    标签: vba excel


    【解决方案1】:

    为此使用R1C1 表示法:

    if Cells(jj, 2) = "Some Criteria" Then Cells(jj, 3).FormulaR1C1 = "=RC1^2"
    

    或者您可以计算 A1 符号公式,例如

    if Cells(jj, 2) = "Some Criteria" Then Cells(jj, 3).Formula = "=$A" & jj & "^2"
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-11-27
      • 2015-03-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多