繁体   English   中英

循环将文本转换为数字,同时避免使用前导零的单元格

[英]Loop Convert text to number while avoiding cells with leading zero

SAP 导出将所有数字转换为文本我需要将前导零的单元格作为文本,同时将 rest(如果数字文本转换为数字)。

Sub ConvertUsingLoop()
For Each r In Sheets("Loop&CSng").UsedRange.SpecialCells(xlCellTypeConstants)
If IsNumeric(r) Then
r.Value = CSng(r.Value)
r.NumberFormat = "General"
End If
Next
End Sub

我正在尝试合并Left(Str, 1) = "0"以排除前导零的单元格。 谢谢您的意见。

也许只需将您的Left$支票添加到If

If IsNumeric(r.Value)  And Left$(r.Value, 1) <> "0" Then

这是我达到的最终代码:

Sub Text2Number()
    On Error GoTo EH

    Application.ScreenUpdating = False
    Application.Calculation = xlCalculationManual
    Application.EnableEvents = False

Set Rng = ActiveSheet.UsedRange

Rng.Cells(1, 1).Select

For i = 1 To Rng.Rows.Count
    For j = 1 To Rng.Columns.Count
        If Rng.Cells(i, j) <> "" Then
            Union(Selection, Rng.Cells(i, j)).Select
        End If
    Next j
Next i
For Each c In Rng.Cells
    If IsNumeric(c.Value) And Left$(c.Value, 1) <> "0" Then
        c.NumberFormat = "General"
     c.Value = c.Value
    End If
Next
CleanUp:
    On Error Resume Next
    Application.ScreenUpdating = True
    Application.Calculation = xlCalculationAutomatic
    Application.EnableEvents = True
Exit Sub
EH:
    ' Do error handling
    Resume CleanUp
End Sub

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM