Vba 用于在特定Word文档表列中的数字前面添加零的宏

Vba 用于在特定Word文档表列中的数字前面添加零的宏,vba,ms-word,Vba,Ms Word,有人能提供VBA宏建议吗 在Word文档表中,我只需要对第一列应用以下规则: 我需要在数字的前面加上零(0),使它们的总长度达到8个字符。所有数字都以一两个字母结尾,这将包含在8个字符的计数中 例如: 2020A (5 characters) must read 0002020A (8 characters) 123456AB (8 characters) remains unchanged 765432X (7 characters) must read 0765432X (8 charact

有人能提供VBA宏建议吗

在Word文档表中,我只需要对第一列应用以下规则:

我需要在数字的前面加上零(0),使它们的总长度达到8个字符。所有数字都以一两个字母结尾,这将包含在8个字符的计数中

例如:

2020A (5 characters) must read 0002020A (8 characters)
123456AB (8 characters) remains unchanged
765432X (7 characters) must read 0765432X (8 characters)

如何将其仅应用于Word文档表格第一列中的每个字段?

答案由macropod在vbaexpress论坛上提供:

Dim RngCel As Range, oCel As Cell, oTbl As Table 
For Each oTbl In ActiveDocument.Tables 
    For Each oCel In oTbl.Columns(1).Cells 
        Set RngCel = oCel.Range 
        RngCel.End = RngCel.End - 1 
        While Len(RngCel.Text) < 8 
            RngCel.InsertBefore "0" 
        Wend 
    Next oCel 
Next oTbl
Dim RngCel作为范围,oCel作为单元格,oTbl作为表格
对于ActiveDocument.Tables中的每个oTbl
对于oTbl.列(1.单元格)中的每个oCel
设置RngCel=oCel.范围
RngCel.End=RngCel.End-1
而Len(RngCel.Text)<8
RngCel.InsertBefore“0”
温德
下奥塞尔
下一个oTbl

macropod在vbaexpress论坛上提供的答案:

Dim RngCel As Range, oCel As Cell, oTbl As Table 
For Each oTbl In ActiveDocument.Tables 
    For Each oCel In oTbl.Columns(1).Cells 
        Set RngCel = oCel.Range 
        RngCel.End = RngCel.End - 1 
        While Len(RngCel.Text) < 8 
            RngCel.InsertBefore "0" 
        Wend 
    Next oCel 
Next oTbl
Dim RngCel作为范围,oCel作为单元格,oTbl作为表格
对于ActiveDocument.Tables中的每个oTbl
对于oTbl.列(1.单元格)中的每个oCel
设置RngCel=oCel.范围
RngCel.End=RngCel.End-1
而Len(RngCel.Text)<8
RngCel.InsertBefore“0”
温德
下奥塞尔
下一个oTbl