我的代码:
Dim ws_fe As Worksheet
Set ws_fe = ActiveWorkbook.Worksheets("xx")
Dim ws_de As Worksheet
Set ws_de = ActiveWorkbook.Worksheets("xxx")
Dim a as string
a = B
ws_fe.Cells(5, 10).FormulaR1C1 = "=IF(RC[-4]="""","""",RC[-5]-" & a & 6
我想为“B6”分配一个范围,但我得到的 a 值是:
问题是 R1C1 公式不仅识别 B,还识别“B”,这在分配范围时会出现问题,因为这会得到“B”6 而不是 B6
如何才能让 VBA 正确识别 B 为简单字符串 B ?
我期待的是:
ws_fe.Cells(5, 10).FormulaR1C1 = "=IF(RC[-4]="""","""",RC[-5]-" & a & 6 = =if(rxcx= "" ; "" ; rxcx - B6)
如果a=“B”
H28 单元格中的公式
"=IF(RC[-4]="""","""",RC[-5]-R" & columns(a).column & "C6)"
给出结果
=IF(D28="","",C28-$F$2)
还有这个
"=IF(RC[-4]="""","""",RC[-5]-R[" & columns(a).column & "]C[6])"
结果
=IF(D28="","",C28-N30)
您的代码由于多种原因而无法工作。
a = B
时,代码很乐意将 B 定义为空变量。R6C2
适用于 B6。将两行更改为
a = "C2"
ws_fe.Cells(5, 10).FormulaR1C1 = "=IF(RC[-4]="""","""",RC[-5]-R6" & a & ")"
会将公式
=IF(F5="","",E5-$B$6)
添加到单元格 J5
。