如何从 VBA 的单元格中读取格式化的百分比?
How to read the formatted percentage from a cell with VBA?
我正在从 Excel 电子表格中获取数据并将其插入到 Word 文档中。这是我正在使用的代码,它按预期工作。
Private Sub insertData(wb As Excel.Workbook)
Dim numBM As Integer
Dim countBM As Integer
Dim currentBM As String
numBM = ActiveDocument.Bookmarks.Count
For countBM = 1 To numBM
currentBM = ActiveDocument.Bookmarks(countBM).Name
ActiveDocument.Bookmarks(currentBM).Range.Text = wb.Names(currentBM).RefersToRange.Value2
Next
End Sub
在Excel中,我有一些单元格是百分比类型的格式。因此单元格的值为 0.857394723 但单元格显示“86%”。如何更改我的代码,以便将“86%”而不是“0.857394723”插入到 Word 中
相应地格式化值,使用 VBA 标准库中的 Strings.Format
方法:
Dim formattedValue As String
formattedValue = Format(wb.Names(currentBM).RefersToRange.Value2,"0%")
ActiveDocument.Bookmarks(currentBM).Range.Text = formattedValue
我居然想出了这个答案,非常简单。我可以使用 ...RefersToRange.Text 而不是使用 ...RefersToRange.Value 所以该行变为
ActiveDocument.Bookmarks(currentBM).Range.Text = wb.Names(currentBM).RefersToRange.Text
我正在从 Excel 电子表格中获取数据并将其插入到 Word 文档中。这是我正在使用的代码,它按预期工作。
Private Sub insertData(wb As Excel.Workbook)
Dim numBM As Integer
Dim countBM As Integer
Dim currentBM As String
numBM = ActiveDocument.Bookmarks.Count
For countBM = 1 To numBM
currentBM = ActiveDocument.Bookmarks(countBM).Name
ActiveDocument.Bookmarks(currentBM).Range.Text = wb.Names(currentBM).RefersToRange.Value2
Next
End Sub
在Excel中,我有一些单元格是百分比类型的格式。因此单元格的值为 0.857394723 但单元格显示“86%”。如何更改我的代码,以便将“86%”而不是“0.857394723”插入到 Word 中
相应地格式化值,使用 VBA 标准库中的 Strings.Format
方法:
Dim formattedValue As String
formattedValue = Format(wb.Names(currentBM).RefersToRange.Value2,"0%")
ActiveDocument.Bookmarks(currentBM).Range.Text = formattedValue
我居然想出了这个答案,非常简单。我可以使用 ...RefersToRange.Text 而不是使用 ...RefersToRange.Value 所以该行变为
ActiveDocument.Bookmarks(currentBM).Range.Text = wb.Names(currentBM).RefersToRange.Text