2016-01-21 48 views
-1

我正在從Excel電子表格中取數據並將其插入到Word文檔中。這是我正在使用的代碼,它按預期工作。插入excel中的百分比類型數據

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%」被插入到Word中,而不是「0.857394723」

回答

0

我竟然想通出來這個答案,這是非常簡單的。而不是使用... RefersToRange.Value我可以使用... RefersToRange.Text所以行變成

ActiveDocument.Bookmarks(currentBM).Range.Text = wb.Names(currentBM).RefersToRange.Text 
0

格式的值。因此,使用Strings.Format方法從VBA標準庫:

Dim formattedValue As String 
formattedValue = Format(wb.Names(currentBM).RefersToRange.Value2,"0%") 

ActiveDocument.Bookmarks(currentBM).Range.Text = formattedValue 
+0

感謝您的回答。我試了一下,它確實有效,但我不得不擴展我的代碼來格式化一些值而不是其他值。 – MightyMouseZ