gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 23:22:48 25 4
gpt4 key购买 nike

希望能帮到你。

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

我附上了图片PIC.1以供投注理解。

PIC1 enter image description here

我目前拥有的代码(我希望可以修改)如下,一如既往,我们非常感谢任何和所有帮助。非常感谢

代码

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

最佳答案

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

关于string - VBA从字符串/单元格的开头删除数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42716936/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com