Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel 从列中删除前导零(非函数)_Excel_Vba - Fatal编程技术网

Excel 从列中删除前导零(非函数)

Excel 从列中删除前导零(非函数),excel,vba,Excel,Vba,这是Dammer15发布的,这就是我要找的。但是我需要它在整个列中循环。我会直接问,但我还不能评论 Dim My_Number As String Dim i As Integer My_Number = Range("AJ") For i = 1 To Len(My_Number) - 1 If InStr(1, My_Number, "0") = 1 Then My_Number = Right(My_Number, Len(My_Number) - 1) Else Ran

这是Dammer15发布的,这就是我要找的。但是我需要它在整个列中循环。我会直接问,但我还不能评论

Dim My_Number As String
Dim i As Integer
My_Number = Range("AJ")
For i = 1 To Len(My_Number) - 1
If InStr(1, My_Number, "0") = 1 Then
    My_Number = Right(My_Number, Len(My_Number) - 1)
 Else
    Range("AJ") = My_Number
    Exit For
 End If
Next

您将需要遍历每个值。我建议将整个范围上传到一个数组中,然后通过该数组循环:

Sub foo()
Dim rng As Range
Dim i As Long, j As Long
Dim arr() As Variant
Dim lastRow As Long

With ActiveSheet
    lastRow = .Cells(.Rows.Count, "AJ").End(xlUp).Row
    Set rng = .Range("AJ1", .Cells(lastRow, "AJ"))
    arr = rng.Value
    For i = LBound(arr, 1) To UBound(arr, 1)
        My_Number = arr(i, 1)
        For j = 1 To Len(arr(i, 1)) - 1
        If Mid(arr(i, 1), j, 1) <> 0 Then
            arr(i, 1) = Right(arr(i, 1), Len(arr(i, 1)) - (j - 1))
            Exit For
         End If
        Next j
    Next i
    rng.Value = arr
End With

End Sub
Sub-foo()
变暗rng As范围
我和我一样长,我和我一样长
Dim arr()作为变量
最后一排一样长
使用ActiveSheet
lastRow=.Cells(.Rows.Count,“AJ”).End(xlUp).Row
设置rng=.Range(“AJ1”,.Cells(最后一行,“AJ”))
arr=平均值
对于i=LBound(arr,1)到UBound(arr,1)
My_Number=arr(i,1)
对于j=1到Len(arr(i,1))-1
如果Mid(arr(i,1),j,1)为0,则
arr(i,1)=右(arr(i,1),Len(arr(i,1))-(j-1))
退出
如果结束
下一个j
接下来我
rng.Value=arr
以
端接头

您可以使用正则表达式来匹配lead 0s,而不是您当前拥有的庞大替换功能:

Sub TestRe()
    Dim LastCell As Range: Set LastCell = ActiveSheet.Columns("AJ").Find("*", SearchDirection:=xlPrevious)
    Dim Rng As Range: Set Rng = ActiveSheet.Range("AJ1", LastCell)
    Dim Cell As Range: For Each Cell In Rng
        Cell.Value = RemoveLead0s(Cell.Value)
    Next Cell
End Sub

Function RemoveLead0s(AlphaNum As String) As String
    RemoveLead0s = AlphaNum

    ' RegExp requires "Microsoft VBScript Regular Expressions 5.5" reference
    Dim RegEx As New RegExp
    With RegEx
        .Pattern = "^0*"
        If .test(AlphaNum) Then RemoveLead0s = .Replace(AlphaNum, "")
    End With
End Function

您不能执行此操作
My_Number=Range(“AJ”)
您需要在该范围内循环并逐个查看每个单元格。但它们都是数字还是字符串中有文本?是字母数字