VBA基于活动单元运行(预制)公式

时间:2016-10-14 13:47:42

标签: excel vba excel-vba macros

我正在尝试使用VBA运行公式。我最终的目标是让它根据所选的一批单元格单击运行多个公式。关于我正在使用这些公式的一些背景。我正在使用公式自动生成基于预填充字段的工作范围。举个例子:

Circuit ID        scope                data1    data2   data3  data4
123                                     a        b       c      d
234                                     f        g       h      j

我想突出显示电路ID,并在相对于所选电路ID的范围内填写公式。这就是我到目前为止......我使用的是宏录像机而且它不想工作。

Sub Formulas()
'
' Formulas Macro
'

'

    Range("E2").Select
    ActiveCell.Formula = "="•     Customer name: ""&RC[29]&"""&chr(10)&"•     Customer Bus Org: ""&RC[30]&"""&chr(10)&"•     Internal Circuit ID: ""&RC[2]&"""&chr(10)&"•     Customer prem address: ""&RC[12]&"" ""&RC[13]&"" ""&RC[14]&"", ""&RC[15]&"", ""&RC[16]&"", ""&RC[17]&"""&chr(10)&"•     Customer demarc: ""&RC[18]&"" ""&RC[20]&"", ""&RC[19]&"" ""&RC[21]&"""&chr(10)&"•     MRR: ""&RC[68]&"""&chr(10)&"•     Current Off Net MRC: $""&RC[10]&"""&chr(10)&"•   "& _
        " Percent: ""&RC[89]&"""&chr(10)&"•     Bandwidth: ""&RC[6]&"" ( ""&RC[7]&""Mb )"&chr(10)&"•     Customer term end date: ""&TEXT(RC[32],""mmm-dd-yyyy"")&"""&chr(10)&"•     New Vendor: ""&RC[106]&"""&chr(10)&"•     New MRC: $""&RC[102]&"""&chr(10)&"•     New NRC: $""&RC[103]&"""&chr(10)&"•     New Install Interval: ""&RC[105]&"""&chr(10)&"•     New Term: ""&RC[104]&"""&chr(10)&""&chr(10)&"Planner Notes:"&chr(10)&"This project is replacing the existing ""&RC[6]&"" ( "& _
        """Mb ) based solution from ( ""&RC[5]&"" ) with a new ""&RC[99]&"" ( ""&RC[100]&""Mb ) based solution from ( ""&RC[106]&"" ). "&chr(10)&""&chr(10)&"RFA # ""&RC[107]&"" install notes:"&chr(10)&"Please install ( ""&RC[31]&"" ) Ethernet ""&RC[99]&"" ( ""&RC[100]&""Mb  ) circuit with ( ""&RC[106]&"" ) from ( ""&RC[101]&"" )  to ( [Customer Prem] ""&RC[12]&"" ""&RC[13]&"" ""&RC[14]&"", ""&RC[15]&"", "& _
        "&"", ""&RC[17]&"" ).  This new circuit will be used to replace existing customer circuit ECCKT: ""&RC[1]&"", ""&RC[109]&"", ""&RC[110]&"", ICCKT: ""&RC[2]&"".  The customer prem address is ( ""&RC[12]&"" ""&RC[13]&"" ""&RC[14]&"", ""&RC[15]&"", ""&RC[16]&"", ""&RC[17]&"" ) and customer demarc is ( ""&RC[18]&"" ""&RC[20]&"", ""&RC[19]&"" ""&RC[21]&"" )."&chr(10)&""&chr(10)&""&chr(10)&"""""& _

End Sub

这是我的实际公式,我注意到录音机改变了很多东西 =" - 客户名称:"& AH2&" - 客户巴士组织:"& AI2&" - 内部电路ID:"& G2&" - 客户预告地址:"& Q2&" "&安培; R2&安培;" "& S2&","& T2&","& U2&","& V2&&#34 ; - 客户分界:"& W2&" "& Y2&","& X2&" "&安培; Z2&安培;" - MRR:"& BU2&" - 当前关闭净MRC:$"& O2&" - 保证金百分比:"& CP2&" - 带宽:"& K2&" ("& L2&" Mb) - 客户期限结束日期:"& TEXT(AK2," mmm-dd-yyyy")&" - 新供应商:"& DG2&" - 新的MRC:$"& DC2&" - 新NRC:$"& DD2&" - 新安装间隔:"& DF2&" - 新术语:"& DE2&"

规划师备注:

  

该项目正在取代现有的"& K2&" ("& L2&" Mb)基于   来自("& J2&")的解决方案与新的"& CZ2&" ("& DA2&" Mb)基于   来自("& DG2&")的解决方案。

RFA#"& DH2&"安装说明:

  

请安装("& AJ2&")Ethernet"& CZ2&" ("& DA2&" Mb)电路   ("& DG2&")从("& DB2&")到([Customer Prem]"& Q2&"&# 34;&安培; R2&安培;"   "& S2&","& T2&","& U2&","& V2&&#34 ; )。这个新电路将用于   替换现有的客户电路ECCKT:"& F2&","& DJ2&","& DK2&",   ICCKT:"& G2&"。客户预告地址是("& Q2&""& R2&""& S2&",   "& T2&","& U2&","& V2&" )和客户分界是("& W2&""& Y2&",   "&安培; X2&安培;" "&安培; Z2&安培;" )。

"

1 个答案:

答案 0 :(得分:0)

1)您需要使用FormulaR1C1,而不仅仅是Formula

2)不要使用Select,而只是直接引用单元格,例如:

Range("E2").FormulaR1C1 = "=""Customer name: "" & RC[29]"

如果宏录像机搞砸了,那么就自己逐步建立配方。下一步将是:

Range("E2").FormulaR1C1 = "=""Customer name: "" & RC[29] & "" Customer Bus Org: "" & RC[30]"

等。 等

3)我建议使用完全合格的参考文献:

Sub formulas()

    Dim ws As Excel.Worksheet

    Set ws = ThisWorkbook.Sheets("Test") ' change to name of your sheet

    With ws

        ' In production, you probably want to loop through the rows and insert the formula dynamically.     
        ' If so, insert loop here and reference the scope column cell dynamically instead of using range("E2")
        .Range("E2").FormulaR1C1 = "=""Customer name: "" & RC[29] & "" Customer Bus Org: "" & RC[30]"

    End With

End Sub