在 Excel VBA 中构建字符串以输出到 Word 返回 "False" 而不是字符串
Building string in Excel VBA to output to Word is returning "False" instead of string
我正在尝试在 Excel VBA 中构建一个字符串,然后将其输出到 Word 文件。
我创建的基本字符串工作正常,但这些复杂的字符串作为 "False" 进入 Word,所以我不确定我做错了什么。
我正在使用 Excel / Word 2013。
示例 excel 文件和 word 模板在这里:
https://dl.dropboxusercontent.com/u/5611192/Sales%20Package%20-%20Test%20for%20Word%20Proposal.xlsm
https://dl.dropboxusercontent.com/u/5611192/Proposal.docx
无效的字符串是使用以下循环构建的:
With Sheet1
For RowCnt = Firstrow To Lastrow
If .Cells(RowCnt, 15).Value = "x" And .Cells(RowCnt, 13).Value = "" Then
strProducts = strProducts & .Cells(RowCnt, 2).Value & " " & .Cells(RowCnt, 8).Value & Chr(11)
ElseIf .Cells(RowCnt, 15).Value = "x" And Cells(RowCnt, 13).Value = "1" Then
strOptions1 = strOptions1 & .Cells(RowCnt, 2).Value & " " & .Cells(RowCnt, 8).Value & Chr(11)
ElseIf .Cells(RowCnt, 15).Value = "x" And .Cells(RowCnt, 13).Value = "2" Then
strOptions2 = strOptions2 & .Cells(RowCnt, 2).Value & " " & .Cells(RowCnt, 8).Value & Chr(11)
ElseIf .Cells(RowCnt, 15).Value = "x" And .Cells(RowCnt, 13).Value = "3" Then
strOptions3 = strOptions3 & .Cells(RowCnt, 2).Value & " " & .Cells(RowCnt, 8).Value & Chr(11)
ElseIf .Cells(RowCnt, 15).Value = "x" And .Cells(RowCnt, 13).Value = "4" Then
strOptions4 = strOptions4 & .Cells(RowCnt, 2).Value & " " & .Cells(RowCnt, 8).Value & Chr(11)
ElseIf .Cells(RowCnt, 15).Value = "x" And .Cells(RowCnt, 13).Value = "5" Then
strOptions5 = strOptions5 & .Cells(RowCnt, 2).Value & " " & .Cells(RowCnt, 8).Value & Chr(11)
ElseIf .Cells(RowCnt, 15).Value = "x" And .Cells(RowCnt, 13).Value = "6" Then
strOptions6 = strOptions6 & .Cells(RowCnt, 2).Value & " " & .Cells(RowCnt, 8).Value & Chr(11)
End If
Next RowCnt
End With
strProducts = strProducts & Chr(11) & Range("A47").Font.Bold = True & Chr(11) & "Purchase, including installation: " & strPrice
strOptions1 = strOptions1 & Chr(11) & Range("A47").Font.Bold = True & Chr(11) & "Purchase, including installation: " & strOption1Price
strOptions2 = strOptions2 & Chr(11) & Range("A47").Font.Bold = True & Chr(11) & "Purchase, including installation: " & strOption2Price
strOptions3 = strOptions3 & Chr(11) & Range("A47").Font.Bold = True & Chr(11) & "Purchase, including installation: " & strOption3Price
strOptions4 = strOptions4 & Chr(11) & Range("A47").Font.Bold = True & Chr(11) & "Purchase, including installation: " & strOption4Price
strOptions5 = strOptions5 & Chr(11) & Range("A47").Font.Bold = True & Chr(11) & "Purchase, including installation: " & strOption5Price
strOptions6 = strOptions6 & Chr(11) & Range("A47").Font.Bold = True & Chr(11) & "Purchase, including installation: " & strOption6Price
解决方案
以第一个字符串为例:
strProducts = strProducts & Chr(11) & Range("A47").Font.Bold = True & Chr(11) & "Purchase, including installation: " & strPrice
这部分是你的问题:
Range("A47").Font.Bold = True
将此更改为:
Iif(Range("A47").Font.Bold = True,True,False)
其他字符串也一样。
最终代码
根据您的评论反馈,您的代码应如下所示:
strProducts = strProducts & Chr(11) & Range("A47").Value & Chr(11) & "Purchase, including installation: " & strPrice
Range("A47").Font.Bold = True
我正在尝试在 Excel VBA 中构建一个字符串,然后将其输出到 Word 文件。
我创建的基本字符串工作正常,但这些复杂的字符串作为 "False" 进入 Word,所以我不确定我做错了什么。
我正在使用 Excel / Word 2013。
示例 excel 文件和 word 模板在这里: https://dl.dropboxusercontent.com/u/5611192/Sales%20Package%20-%20Test%20for%20Word%20Proposal.xlsm
https://dl.dropboxusercontent.com/u/5611192/Proposal.docx
无效的字符串是使用以下循环构建的:
With Sheet1
For RowCnt = Firstrow To Lastrow
If .Cells(RowCnt, 15).Value = "x" And .Cells(RowCnt, 13).Value = "" Then
strProducts = strProducts & .Cells(RowCnt, 2).Value & " " & .Cells(RowCnt, 8).Value & Chr(11)
ElseIf .Cells(RowCnt, 15).Value = "x" And Cells(RowCnt, 13).Value = "1" Then
strOptions1 = strOptions1 & .Cells(RowCnt, 2).Value & " " & .Cells(RowCnt, 8).Value & Chr(11)
ElseIf .Cells(RowCnt, 15).Value = "x" And .Cells(RowCnt, 13).Value = "2" Then
strOptions2 = strOptions2 & .Cells(RowCnt, 2).Value & " " & .Cells(RowCnt, 8).Value & Chr(11)
ElseIf .Cells(RowCnt, 15).Value = "x" And .Cells(RowCnt, 13).Value = "3" Then
strOptions3 = strOptions3 & .Cells(RowCnt, 2).Value & " " & .Cells(RowCnt, 8).Value & Chr(11)
ElseIf .Cells(RowCnt, 15).Value = "x" And .Cells(RowCnt, 13).Value = "4" Then
strOptions4 = strOptions4 & .Cells(RowCnt, 2).Value & " " & .Cells(RowCnt, 8).Value & Chr(11)
ElseIf .Cells(RowCnt, 15).Value = "x" And .Cells(RowCnt, 13).Value = "5" Then
strOptions5 = strOptions5 & .Cells(RowCnt, 2).Value & " " & .Cells(RowCnt, 8).Value & Chr(11)
ElseIf .Cells(RowCnt, 15).Value = "x" And .Cells(RowCnt, 13).Value = "6" Then
strOptions6 = strOptions6 & .Cells(RowCnt, 2).Value & " " & .Cells(RowCnt, 8).Value & Chr(11)
End If
Next RowCnt
End With
strProducts = strProducts & Chr(11) & Range("A47").Font.Bold = True & Chr(11) & "Purchase, including installation: " & strPrice
strOptions1 = strOptions1 & Chr(11) & Range("A47").Font.Bold = True & Chr(11) & "Purchase, including installation: " & strOption1Price
strOptions2 = strOptions2 & Chr(11) & Range("A47").Font.Bold = True & Chr(11) & "Purchase, including installation: " & strOption2Price
strOptions3 = strOptions3 & Chr(11) & Range("A47").Font.Bold = True & Chr(11) & "Purchase, including installation: " & strOption3Price
strOptions4 = strOptions4 & Chr(11) & Range("A47").Font.Bold = True & Chr(11) & "Purchase, including installation: " & strOption4Price
strOptions5 = strOptions5 & Chr(11) & Range("A47").Font.Bold = True & Chr(11) & "Purchase, including installation: " & strOption5Price
strOptions6 = strOptions6 & Chr(11) & Range("A47").Font.Bold = True & Chr(11) & "Purchase, including installation: " & strOption6Price
解决方案
以第一个字符串为例:
strProducts = strProducts & Chr(11) & Range("A47").Font.Bold = True & Chr(11) & "Purchase, including installation: " & strPrice
这部分是你的问题:
Range("A47").Font.Bold = True
将此更改为:
Iif(Range("A47").Font.Bold = True,True,False)
其他字符串也一样。
最终代码
根据您的评论反馈,您的代码应如下所示:
strProducts = strProducts & Chr(11) & Range("A47").Value & Chr(11) & "Purchase, including installation: " & strPrice
Range("A47").Font.Bold = True