VBA从string/单元格的开头删除数字

我希望你能帮上忙。

我有一段代码,目前正在从列G中的单元格中删除所有文本。我需要的是这个代码,而不是删除文本,我希望它删除数字,我只希望它删除在string/单元格开头的数字我想保持其余的数据不变。

我已经附上一张图片PIC.1作为投注的理解。

PIC1 在这里输入图像说明

我现在有的代码,我希望可以修改下面,如往常一样,所有的帮助, 非常感谢

Sub RemoveNonDigits() Dim X As Long, Z As Long, LastRow As Long, CellVal As String Const StartRow As Long = 1 Const DataColumn As String = "G" Application.ScreenUpdating = False LastRow = Cells(Rows.Count, DataColumn).End(xlUp).Row For X = StartRow To LastRow CellVal = Cells(X, DataColumn) For Z = 1 To Len(CellVal) If Mid(CellVal, Z, 1) Like "[!0-9]" Then Mid(CellVal, Z, 1) = " " Next With Cells(X, DataColumn) .NumberFormat = "@" .Value = Replace(CellVal, " ", "") End With Next Application.ScreenUpdating = True End Sub 

请参阅下面的修改代码:

 Sub RemoveNonDigits() Dim X As Long, Z As Long, LastRow As Long, CellVal As String Const StartRow As Long = 1 Const DataColumn As String = "G" Application.ScreenUpdating = False LastRow = Cells(Rows.Count, DataColumn).End(xlUp).Row For X = StartRow To LastRow CellVal = Cells(X, DataColumn) While IsNumeric(Left(CellVal, 1)) ' Here CellVal = Mid(CellVal, 2) ' all digits at the start Wend ' are removed Cells(X, DataColumn) = Trim(CellVal) Next Application.ScreenUpdating = True End Sub 

也就是说,当CellVal的起始字符是一个数字时,得到以第二个字符开始的子string,并继续直到不匹配。

 CellVal = LTrimDigits(Cells(X, DataColumn)) 

有了这个相当高效的:

 Public Function LTrimDigits(value As String) As String Dim i As Long For i = 1 To Len(value) '//loop each char Select Case Mid$(value, i, 1) '//examine current char Case "0" To "9" '//permitted chars Case Else: Exit For '// i is the cut off point End Select Next LTrimDigits = Mid$(value, i) '//strip lead End Function 

该function将从string中去除前导数字和空格

 Function RemoveLeadingDigits(str As String) As String Dim i As Long Dim chr As String ' Loop through string For i = 1 To Len(str) ' Get character i chr = Mid(str, i, 1) ' Keep looping until character is not a number or space If Not IsNumeric(chr) And Not chr = " " Then ' If it is a number or space, strip checked characters ' from str (because they'll be numeric or space) str = Right(str, Len(str) - i + 1) ' Stop looping as non-numeric characters encountered Exit For End If Next i ' Return the value of str RemoveLeadingDigits = str End Function 

你可以从你的代码中调用它

 Sub RemoveNonDigits() Dim X As Long, LastRow As Long, CellVal As String Const StartRow As Long = 1 Const DataColumn As String = "G" Application.ScreenUpdating = False LastRow = Cells(Rows.Count, DataColumn).End(xlUp).Row For X = StartRow To LastRow CellVal = Cells(X, DataColumn).Value ' ---------------------------------------- CellVal = RemoveLeadingDigits(CellVal) ' ---------------------------------------- Next Application.ScreenUpdating = True End Sub 

关于你的代码的说明虽然:

你应该完全确定你的细胞。 例如,将整个循环部分包装在With ThisWorkbook.Sheets("YourSheet") ,然后使用.Cells(row, col)而不是Cells(row, col)访问单元Cells(row, col)

有点怪异的更短的select(假设所有值都以整数开始)

 For Each cell in Range([G7], [G7].End(xlDown)) cell.Value2 = Trim(Mid(cell, Len(Str(Val(cell))))) Next 

我想像这样一个简单的变化:

 For X = StartRow To LastRow Cells(X, DataColumn).Formular1c1 = Application.Trim(Replace(Cells(X, DataColumn).Text, Val(Cells(X, DataColumn).Text), "")) Next X 

将解决你的问题…