将公式添加到单元格并引用变量

时间:2015-09-24 13:48:50

标签: excel-vba vba excel

我正在尝试将公式放入包含VBA变量的单元格中,但我得到另一个Application or Object defined error。错误是在第一行op Range("R" & i).FormulaR1C1 = "=Q" & i & "/" & DollarRate上触发的。我尝试仅使用Formula代替FormulaR1C1

列Q和R为空。列Q将包含以€为单位的值,而列R将以$。

显示值
Dim LastRow As Integer, i As Integer
Dim DollarRate As Double

DollarRate = InputBox("Enter dollar rate:", "Dollar rate")

Range("Q1").Value = "$ POS"
Range("R1").Value = "€ POS"

LastRow = Cells(Rows.Count, "A").End(xlUp).Row

For i = 2 To LastRow
    Range("R" & i).FormulaR1C1 = "=Q" & i & "/" & DollarRate
Next i

1 个答案:

答案 0 :(得分:1)

尝试替换它:

For i = 2 To LastRow
    Range("R" & i).FormulaR1C1 = "=Q" & i & "/" & DollarRate
Next i

用这个:

Range("R2:R" & lastrow).FormulaLocal = "=Q2/" & DollarRate