【问题标题】:VBA to run (premade) formulas based on active cellsVBA 基于活动单元格运行(预制)公式
【发布时间】:2016-10-14 13:47:41
【问题描述】:

我正在尝试使用 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&” - 客户界限:“&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&")以太网"&CZ2&"("&DA2&"Mb)电路 与 ( "&DG2&" ) 从 ( "&DB2&" ) 到 ( [Customer Prem] "&Q2&" "&R2&" “&S2&”、“&T2&”、“&U2&”、“&V2&”)。这个新电路将用于 替换现有客户电路ECCKT:“&F2&”、“&DJ2&”、“&DK2&”、 ICCKT:“&G2&”。客户prem地址是(“&Q2&”“&R2&”“&S2&”, "&T2&", "&U2&", "&V2&" ) 和客户界限是 ( "&W2&" "&Y2&", "&X2&" "&Z2&")。

"

【问题讨论】:

    标签: vba excel macros


    【解决方案1】:

    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
    

    【讨论】:

    • 首先非常感谢米奇抽出宝贵时间。有很多乐趣让我头疼试图让这件作品工作。我进入并研究了 VBA 中的公式,但我似乎在运行它时遇到了问题。我觉得我只是错过了我未经训练的眼睛缺少的一件小事。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-05
    • 2013-01-13
    • 2020-05-15
    • 1970-01-01
    相关资源
    最近更新 更多