我试着用VBA做一个公式。我的最终目标是让它运行多个公式,所有的点击都是基于选定的一批单元格。关于我用这些公式所做的事情。我使用公式自动生成基于预先填充的字段的工作范围。例如:
Circuit ID scope data1 data2 data3 data4
123 a b c d
234 f g h j我希望高亮显示电路in,并在相对于所选的电路in的范围内填充公式。这就是我目前所拥有的..。我用了宏记录器,它不想工作。
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&“- Customer Bus Org:”&AI2 2&“-内部电路ID:"&G2&”-客户prem地址:"&Q2&“&R2&”&S2&","&T2&","&U2&","&V2&“-客户名称:"&W2&”&Y2&,"&X2&“& "&Z2&”- MRR:“&BU2 2&”- MRC:$"&O2&“-保证金百分比:"&CP2&”-带宽:"&K2&“(”L2&“Mb)-客户术语结束日期:"&TEXT(AK2,“mmm-dd-yyyy”)和“新供应商”:"&DG2&“-新MRC:$"&DC2&”-新NRC:$"&DD2&“-新安装间隔:"&DF2&”-新术语:"&DE2&“
规划师备注:
该项目将现有的"&K2&“( "&L2&"Mb )基础解决方案从( "&J2&”)替换为新的“&CZ2 2&”( "&DA2&"Mb )基础解决方案( "&DG2&“)。
RFA # "&DH2&“安装说明:
请安装(“&AJ2 2&”)以太网“&CZ2 2&”(“&DA2 2&”Mb)电路( "&DG2&“),从"&DB2&”安装到( Customer Prem &Q2&& "&R2&“"&S2&","&T2&",”&T2&“,"&U2&","&V2&”)。这个新电路将用于取代现有的客户电路ECCKT:"&F2&“、”&DJ2 2&“、”&DK2 2&“、ICCKT:"&G2&”。客户的prem地址是( "&Q2&“&R2&”&S2&“、"&T2&”、"&U2&“、"&V2&”)和“客户需求”( "&W2&“&Y2&”、"&X2&“& "&Z2&”)。
“
发布于 2016-10-14 14:50:07
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 Subhttps://stackoverflow.com/questions/40044808
复制相似问题