我试图用这样的for循环填充单元格:
For i = 1 To Target
Range("C" & i & ":C" & i ).Formula = "='Sheet1'!A" & i & "/" & "'Sheet2'!B" & i"
Next i
我希望在公式栏中看到:
=' Sheet1'!A1& " /" &安培; ' Sheet2的'!B1
=' Sheet1'!A2& " /" &安培; ' Sheet2'!B2
...
不幸的是它没有用。如果我只尝试第一部分:
For i = 1 To Target
Range("C" & i & ":C" & i ).Formula = "='Sheet1'!A" & i
Next i
这段代码很好,但这对我来说还不够:
='工作表Sheet'!A1
='工作表Sheet'!B1
...
我的frist代码出了什么问题?
答案 0 :(得分:0)
你可以尝试
Range("C" & i & ":C" & i).FormulaR1C1 = "=Sheet1!RC1 & ""/"" & Sheet2!RC2"
答案 1 :(得分:0)
如果您在连接字符串中使用双引号时出现问题,我会尽量删除您删除的内容。单个字符可以通过带有Chr function的ASCII代码编号来引用。 /
字符为47。
Dim i As Long, target As Long
With ActiveSheet
target = .Cells(Rows.Count, "A").End(xlUp).Row
For i = 1 To target
.Range("C" & i).Formula = "='Sheet1'!A" & i & Chr(47) & "'Sheet2'!B" & i
Next i
End With
如果公式构造正确,则不必增加范围。可以使用xlA1或xlR1C1参考样式(参见xlReferenceStyle enumeration)。
Dim i As Long, target As Long
With ActiveSheet
target = .Cells(Rows.Count, "A").End(xlUp).Row
'xlA1 style
.Range("C1:C" & target).Formula = "='Sheet1'!A1/'Sheet2'!B1"
'xlR1C1 Style
.Range("C1:C" & target).FormulaR1C1 = "='Sheet1'!RC1/'Sheet2'!RC2"
End With
请注意,xlR1C1样式需要Range.FormulaR1C1而不是Range.Formula。