[英]Remove zeros from the beginning of string VBA or Function
I have an issue about removing zeros from the beginning of a string.我有一个关于从字符串开头删除零的问题。 I have used我用过
=trim(left(substitute(A1,"0"," "),3))
in string = '0060'
PPC mobile phone I want to have result equals to 60
.在 string = '0060'
PPC mobile phone 我希望结果等于60
。 But this function gives me 6.但是这个函数给了我 6。
Not all my records start with double zeros.并非我所有的记录都以双零开头。 There can be one, two or three zeros.可以有一个、两个或三个零。
As a UDF (User Define Function)作为 UDF(用户定义函数)
Function Strip0(r As Range) As Variant
Dim i As Long
Dim s As String
s = CStr(r.Value)
If s <> "0" Then
Do While Left$(s, 1) = "0"
s = Mid$(s, 2)
Loop
End If
Strip0 = s
End Function
Longer code but runs quickly over a range of values (permanently changing them) rather than use UDFs on an ongoing basis.更长的代码,但在一系列值(永久更改它们)上快速运行,而不是持续使用 UDF。
Uses variant arrays and regexp for efficiency.使用变体数组和正则表达式来提高效率。
Sub KillLeadingZeroes()
Dim rng1 As Range
Dim rngArea As Range
Dim lngRow As Long
Dim lngCol As Long
Dim lngCalc As Long
Dim objReg As Object
Dim X()
On Error Resume Next
Set rng1 = Application.InputBox("Select range for the replacement of leadoimg zeroes", "User select", Selection.Address, , , , , 8)
If rng1 Is Nothing Then Exit Sub
On Error GoTo 0
'See Patrick Matthews excellent article on using Regular Expressions with VBA
Set objReg = CreateObject("vbscript.regexp")
objReg.Pattern = "^0+"
objReg.Global = False
'Speed up the code by turning off screenupdating and setting calculation to manual
'Disable any code events that may occur when writing to cells
With Application
lngCalc = .Calculation
.ScreenUpdating = False
.Calculation = xlCalculationManual
.EnableEvents = False
End With
'Test each area in the user selected range
'Non contiguous range areas are common when using SpecialCells to define specific cell types to work on
For Each rngArea In rng1.Areas
'The most common outcome is used for the True outcome to optimise code speed
If rngArea.Cells.Count > 1 Then
'If there is more than once cell then set the variant array to the dimensions of the range area
'Using Value2 provides a useful speed improvement over Value. On my testing it was 2% on blank cells, up to 10% on non-blanks
X = rngArea.Value2
For lngRow = 1 To rngArea.Rows.Count
For lngCol = 1 To rngArea.Columns.Count
'replace the leading zeroes
X(lngRow, lngCol) = objReg.Replace(X(lngRow, lngCol), vbNullString)
Next lngCol
Next lngRow
'Dump the updated array sans leading zeroes back over the initial range
rngArea.Value2 = X
Else
'caters for a single cell range area. No variant array required
rngArea.Value = objReg.Replace(rngArea.Value, vbNullString)
End If
Next rngArea
'cleanup the Application settings
With Application
.ScreenUpdating = True
.Calculation = lngCalc
.EnableEvents = True
End With
Set objReg = Nothing
End Sub
The following should work if a space always follows the first number:如果空格始终跟在第一个数字之后,则以下内容应该有效:
=TEXT(LEFT(A1, FIND(" ", A1)), "#")
In case the space condition is not guaranteed, a UDF would be needed.如果不能保证空间条件,则需要 UDF。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.