自动填充公式到数据VBA的结尾

时间:2018-10-11 15:37:26

标签: excel vba concatenation formula autofill

当我查找VBA以自动将公式填充到工作簿中所有工作表上数据的末尾时,我发现了以下内容:

Sub FillFormula()
Dim ws As Worksheet
For Each ws In ActiveWorkbook.Worksheets
ws.Range("Q2").Formula = "=CONCATENATE(A2,B2,C2)"
ws.Range("Q2", "Q" & Cells(Rows.Count, 1).End(xlUp).Row).FillDown
Next
End Sub

它工作正常,但不适用于我的公式。我需要它来使用以下公式:

=CONCATENATE(A2,CHAR(10),"Serving:"," ", O2,CHAR(10),"Contact:"," ", B2," ", C2,CHAR(10), F2, ",", " ", G2, " ",H2,CHAR(10), I2,",", " ", J2, " ",K2,CHAR(10),"Phone:"," ",L2,CHAR(10), "Fax:"," ",M2,CHAR(10), "Email:", " ", D2,CHAR(10),"Website:", " ", N2,CHAR(10),P2,CHAR(10)," ",CHAR(10))

但是,由于公式中有许多“”,因此无法正常工作。

有什么建议吗?

谢谢!

1 个答案:

答案 0 :(得分:1)

就像Marcucciboy2所说的那样,使用VBA时必须在公式中加双引号:

Sub FillFormula()
Dim ws As Worksheet
For Each ws In ActiveWorkbook.Worksheets
ws.Range("Q2").Formula = "=CONCATENATE(A2,CHAR(10),""Serving:"","" "", O2,CHAR(10),""Contact:"","" "", B2,"" "", C2,CHAR(10), F2, "","", "" "", G2, "" "",H2,CHAR(10), I2,"","", "" "", J2, "" "",K2,CHAR(10),""Phone:"","" "",L2,CHAR(10), ""Fax:"","" "",M2,CHAR(10), ""Email:"", "" "", D2,CHAR(10),""Website:"", "" "", N2,CHAR(10),P2,CHAR(10),"" "",CHAR(10))"
ws.Range("Q2", "Q" & Cells(Rows.Count, 1).End(xlUp).Row).FillDown
Next
End Sub