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

yk9xbfzb  于 2022-11-18  发布在  其他
关注(0)|答案(2)|浏览(237)

SAP导出将所有数字转换为文本,我需要将带有前导零的单元格保留为文本,同时将其余的数字文本转换为数字。

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"以排除带有前导零的单元格。感谢您的输入。

t98cgbkg

t98cgbkg1#

也许只需将Left$检查添加到If

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

ohtdti5x2#

这是我到达的最后一个代码:

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

相关问题