在VBA公式中使用命名地址

时间:2018-01-09 20:45:15

标签: excel vba excel-vba

我目前有以下命名地址:

diffs<-data.frame(matrix(ncol=ncol(ts)))
colnames(diffs)<-colnames(ts)

  for (row in 1:nrow(pairs)){
      row1<-ts[(ts$name==pairs[row,"name1"]),]
      row2<-ts[(ts$name==pairs[row,"name2"]),]
      difference<-rbind(row1,row2)
      difference[3,1]<-pairs[row,"number"]
      difference[3,2:ncol(difference)]<-difference[1,2:ncol(difference)]-difference[2,2:ncol(difference)]
      diffs<-rbind(diffs,difference[3,])
                   }

我想用该地址划分一个单元格(N2)。 AB2将得到结果。

PLVlast = Sheets("Summary").Range("D" & Cells.Rows.Count).End(xlUp).Address

错误:AB2中的Excel公式显示以下内容

Range("AB2") = "=(RC[-14]/PLVlast)"

我在这里缺少什么?

1 个答案:

答案 0 :(得分:1)

您想要使用PLVlast变量的值,而不是变量名称。如果您打算在R1C1公式中使用其值,还需要将PLVlast设置为具有R1C1地址格式:

PLVlast = Sheets("Summary").Range("D" & Sheets("Summary").Rows.Count).End(xlUp).Address(ReferenceStyle:=xlR1C1)

Range("AB2").FormulaR1C1 = "=(RC[-14]/" & PLVlast & ")"

或者,如果您想在整个过程中使用A1表示法:

PLVlast = Sheets("Summary").Range("D" & Sheets("Summary").Rows.Count).End(xlUp).Address

Range("AB2").Formula = "=(N2/" & PLVlast & ")"

或者,如果要将此公式应用于AB列中的所有单元格,直到D列中最后一个单元格之前的行(我假设这是某种总行数):

Dim lastRow As Long
lastRow = Sheets("Summary").Range("D" & Sheets("Summary").Rows.Count).End(xlUp).Row

Range("AB2").Resize(lastRow - 2, 1).Formula = "=(N2/D$" & lastRow & ")"
相关问题