【问题标题】:VBA: preceding zeros dropped when copied overVBA:复制时删除前面的零
【发布时间】:2010-10-04 08:58:06
【问题描述】:

我正在使用 VBA 创建 Excel 文件的副本。在该文件中,有一列包含前面带零的数字。文件的副本已创建,但该列中的数据被删除。我需要保留前面零的值。如何使用 VBA 解决此问题?

【问题讨论】:

    标签: vba excel


    【解决方案1】:

    最好的方法是通过将 Range.NumberFormat 设置为“@”来将列预先格式化为文本。这样,如果用户编辑单元格,则单元格将保持为文本并保持其前导零。这是一个 VBA 示例:

    ActiveSheet.Range("C:C").NumberFormat = "@"

    【讨论】:

      【解决方案2】:

      在导出之前将该列中的每个单元格转换为文本字段。这应该确保每个字符都被保留(而不是像数字一样对待,这听起来像是正在发生的事情)。

      【讨论】:

        【解决方案3】:

        另一种可能性是在每个单元格上附加一个撇号。这会将所有值视为文本,这在不同选项卡将常见值视为文本与数字(即复制与计算)时很有用。

        这是通过使用 Chr() 函数并为其分配字符代码 39(') 来完成的:

        For x = 1 to 100
        If Sheets(origSheet).Cells(x, "A").Value <> "" Then
                    Sheets(origSheet).Cells(x, "A").Value = Chr(39) & Sheets(origSheet).Cells(x, "A").Value
                End If
        

        【讨论】:

        • 做得好,我需要保留文本“00:00”,但即使使用格式(“00:00”)我也只有“0:00”,并使用“Chr(39)&”解决了这个问题。
        【解决方案4】:

        鉴于已接受的答案,这可能不是您所需要的,但设置自定义数字格式也会将前面的零恢复为显示的值。

        要显示一个前导零最多 8 位的值,例如,将格式设置为 00000000,则 123 将显示为 00000123。

        此处的方法和格式为文本的方法都将产生在计算中仍然有效的单元格值,尽管默认情况下水平对齐方式会有所不同。另请注意,例如,将字符串连接到值会导致差异:

        作为文本:显示00000123,附加“x”得到00000123x

        作为自定义格式的数字:显示 00000123,附加“x”得到 123x,因为它仍然是一个数字。

        不过可能是 TMI!

        【讨论】:

          【解决方案5】:

          这是我为解决此问题而创建的代码:

          Public Sub Change_10_Digit()
          '----------------------------------------------------------------------------
          ' Change numeric loan number ot a 10 digit text number
          ' 2010-05-21 by Jamie Coxe
          '
          ' Note: Insure exracted data Column is formated as text before running macro
          '----------------------------------------------------------------------------
          
                  Dim Lastrow As Long
                  Dim StartRow As Long
                  Dim Col As String
                  Dim RowNum As Long
                  Dim nCol As String
                  Dim Loan As String
                  Dim Digit_Loan As String
                  Dim MyCell As String
                  Dim NewCell As String
                  Dim Cell_Len As Long
                  Dim MyOption As Long
          
          '----- Set Options -------------------------------------------------------
          
              MyOption = 2 '1 = place data in new column, 2 = Replace data in cell
              StartRow = 2 'Start processing data at this row (skip header row)
              Col = "B" 'Loan number in this colmun to be changed to 10 digit
              nCol = "G" 'New column to place value (option 1 only)
          
          '----- End Option Setings ------------------------------------------------
          
              'Get last row
              Lastrow = Range(Col & "65536").End(xlUp).Row
          
              For RowNum = StartRow To Lastrow
                  'Combined Column and Row number to get cell data
                  MyCell = Col & RowNum
          
                  'Get data in cell
                  Loan = Range(MyCell).Value
          
                  'Change data in cell to 10 digit numeric with leading zeros
                  Digit_Loan = Format(Loan, "0000000000")
          
                  If My0ption = 1 Then
                      'Option 1 enter value in new cell
                      NewCell = nCol & RowNum
                      Range(NewCell).Value = Digit_Loan
                  Else
                      'Option 2 replace value in cell
                      Range(MyCell).Value = Digit_Loan
                  End If
          
              Next RowNum
          
          End Sub
          

          【讨论】:

            猜你喜欢
            • 2022-12-18
            • 1970-01-01
            • 2021-10-02
            • 1970-01-01
            • 2011-11-23
            • 1970-01-01
            • 1970-01-01
            • 2011-01-03
            相关资源
            最近更新 更多