[英]VBA to run (premade) formulas based on active cells
我正在嘗試使公式與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&”
規划師注意事項:
該項目將以(“&DG2&”)為基礎的新的&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&願願願願願願願樂樂演員統統統志施加仕仕/&n&n-強制已經已經已經有足夠更多更多的新的/// Imp的軟件開發了新的基於&& CZ2&的解決方案了。
RFA#“&DH2&”安裝說明:
請安裝(“&AJ2&”)以太網“&CZ2&”(“&DA2&” Mb)電路,並將(“&DG2&”)從(“&DB2&”)到([Customer Prem]“&Q2&”“&R2&”“&S2&”,“&T2&”, “&U2&”,“&V2&”)。 此新電路將用於替換現有的客戶電路ECCKT:“&F2&”,“&DJ2&”,“&DK2&”,ICCKT:“&G2&”。 客戶的專用地址是(“&Q2&”“&R2&”“&S2&”,“&T2&”,“&U2&”,“&V2&”),客戶邊界是(“&W2&”“&Y2&”,“&X2&”“&Z2&”)。
”
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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.