VBA更改从Excel导入的数据在Word中的数字格式。

huangapple go评论98阅读模式
英文:

VBA Change Number format in word of data imported from excel

问题

我试图实现的目标是将从Excel表格导入的数字格式更改,并以“0.00%”的格式打印在Word文档表格中。在Excel中预更改格式是无用的,因为每当数据被转录时,它只会粘贴原始数字。

这是我使用的代码:

Const stWordDocument As String = "Doc.docm"
    
    Dim wdApp As Word.Application
    Dim wdDoc As Word.Document
    Dim wdCell As Word.Cell
    Dim wbBook As Workbook
    Dim wsSheet As Worksheet
    Dim lnCountItems As Long
    Dim vaData As Variant

    Set wbBook = ThisWorkbook
    Set wsSheet = wbBook.ActiveSheet
    vaData = ActiveSheet.Range(Cells(1, "F"), Cells(rcount, lastcol0)).Value

    Set wdApp = CreateObject("word.Application")
    Set wdDoc = wdApp.Documents.Open("https://orgindustrie-my.sharepoint.com/personal/Different_person_org_com/Documents/folder/subfolder/subsubfolder/Doc.docm")
    wdApp.Visible = True

For i = 1 To lastcol0 - 5 Step 1
    lnCountItems = 1
        For Each wdCell In wdDoc.Tables(1).Columns(i).Cells
            If lnCountItems = rcount + 1 Then
                Exit For
            Else
                Cell_Text = wdCell.Range.Text
                whte = IsNumeric(vaData(lnCountItems, i))
                If whte = True Then
                    wdCell.Range.Text = Format(Cell_Text, "0.00%")
                End If
                wdCell.Range.Text = vaData(lnCountItems, i)
                lnCountItems = lnCountItems + 1
            End If
        Next wdCell
    Next i

由于某种原因,它将所有Excel数据识别为数值,将所有Word数据识别为非数值,因此当我使用

whte = IsNumeric(vaData(lnCountItems, i))

它始终为真(这也不会更改行wdCell.Range.Text = Format(Cell_Text, "0.00%")中的输出格式)。

如果我将其更改为Word数据,例如

whte = IsNumeric(Cell_Text)

它始终为假。

它始终以以下形式出现:

Word示例输出 VBA更改从Excel导入的数据在Word中的数字格式。

Excel示例源 VBA更改从Excel导入的数据在Word中的数字格式。

我尝试使用

wdCell.Range.Text = FormatPercent(Cell_Text, 2)

但出现“类型不匹配”错误。

我还尝试使用

wdCell.Range.Text.NumberFormat = "0.00%"

在这里,出现“无效限定符”错误,或者当我删除.Text时出现“找不到方法或数据成员”。

可能重要的是,这些数字来自.Sumifs

.Cells(i, lastcol).Value = Application.WorksheetFunction.SumIfs(Range(Cells(rcount, "E"), Cells(2, "E")), Range(Cells(rcount, "B"), Cells(2, "B")), Cells(i, "B"))

我不知道还能尝试什么,非常感谢任何帮助。

英文:

what I'm trying to achieve is to change the format of numbers that I import from a excel sheet and print them in the word document table with the format "0.00%". Prechanging the format in excel is no use, because whenever the data is transcribed it just pastes the original number.

Here's the code that I used:

Const stWordDocument As String = "Doc.docm"
    
    Dim wdApp As Word.Application
    Dim wdDoc As Word.Document
    Dim wdCell As Word.Cell
    Dim wbBook As Workbook
    Dim wsSheet As Worksheet
    Dim lnCountItems As Long
    Dim vaData As Variant

    Set wbBook = ThisWorkbook
    Set wsSheet = wbBook.ActiveSheet
    vaData = ActiveSheet.Range(Cells(1, "F"), Cells(rcount, lastcol0)).Value

    Set wdApp = CreateObject("word.Application")
    Set wdDoc = wdApp.Documents.Open("https://orgindustrie-my.sharepoint.com/personal/Different_person_org_com/Documents/folder/subfolder/subsubfolder/Doc.docm")
    wdApp.Visible = True

For i = 1 To lastcol0 - 5 Step 1
    lnCountItems = 1
        For Each wdCell In wdDoc.Tables(1).Columns(i).Cells
            If lnCountItems = rcount + 1 Then
                Exit For
            Else
                Cell_Text = wdCell.Range.Text
                whte = IsNumeric(vaData(lnCountItems, i))
                If whte = True Then
                    wdCell.Range.Text = Format(Cell_Text, "0.00%")
                End If
                wdCell.Range.Text = vaData(lnCountItems, i)
                lnCountItems = lnCountItems + 1
            End If
        Next wdCell
    Next i

This by some reason identifies all excel data as a numeric value, and all word data as non numeric value, so when i use

whte = IsNumeric(vaData(lnCountItems, i))

it's always true (this also doesn't change the format of the output in the line wdCell.Range.Text = Format(Cell_Text, "0.00%"))

if i change it to word data like

whte = IsNumeric(Cell_Text)

it's always false

it always comes in the form

word example output VBA更改从Excel导入的数据在Word中的数字格式。

Excel example source VBA更改从Excel导入的数据在Word中的数字格式。

I tried using

wdCell.Range.Text = FormatPercent(Cell_Text, 2)

but error "Type Mismatch" appears

I also tried using

wdCell.Range.Text.NumberFormat = "0.00%"

here, error "Invalid Qualifier" appears or "Method or data member not found" when I remove .Text

Something that might be important is that this numbers come from a .Sumifs line

.Cells(i, lastcol).Value = Application.WorksheetFunction.SumIfs(Range(Cells(rcount, "E"), Cells(2, "E")), Range(Cells(rcount, "B"), Cells(2, "B")), Cells(i, "B"))

I don't know what else to try, any help would be highly appreciated.

rcount= last row with information
lastcol0= last column with information 
lastcol= last column without information ( basically lastcol0 + 1 )

答案1

得分: 2

也许是这样的吗?

对于每个 wdCell 在 wdDoc.Tables(1).Columns(i).Cells 中
    如果 lnCountItems = rcount + 1 则
        退出循环
    否则
        v = vaData(lnCountItems, i)                   '从数组中获取值
        如果是数字则 v = 格式化(v, "0.00%")   '如果是数字则进行格式化
        wdCell.Range.Text = v                         '放入单元格中
        lnCountItems = lnCountItems + 1
    结束如果
下一个 wdCell
英文:

More like this maybe?

For Each wdCell In wdDoc.Tables(1).Columns(i).Cells
    If lnCountItems = rcount + 1 Then
        Exit For
    Else
        v = vaData(lnCountItems, i)                   'get value from array
        If IsNumeric(v) Then v = Format(v, "0.00%")   'format if numeric
        wdCell.Range.Text = v                         'put in cell
        lnCountItems = lnCountItems + 1
    End If
Next wdCell

huangapple
  • 本文由 发表于 2023年8月9日 00:06:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/76861352.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定