2

I hope you can help.

I have a piece of code that is currently removing all the text from the cells in Column G. What I need is for this code to instead of removing the text I would like it to remove the numbers, and I only want it to remove the numbers at the beginning of the string/cell the rest of the data I would like to remain the same.

I have attached a picture PIC.1 for betting understanding.

PIC1 enter image description here

The code I currently have and I hope can be amended is below and as always any and all help is greatly appreciated.

CODE

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
4
  • As it appears, the number you want to remove is isolated from the rest by a space. Is that assured? because the solution would be much easier in that case. Commented Mar 10, 2017 at 11:16
  • So, your current code removes all but digits, and you want just the opposite, right? Commented Mar 10, 2017 at 11:17
  • If Mid(CellVal, Z, 1) Like "[0-9]" Then Mid(CellVal, Z, 1) = " " else exit for a do until maybe a neater approach. Commented Mar 10, 2017 at 11:22
  • Hi all thank you taking the time to respond @ASH as space after the digit is not assured unfortunately. AT:Wiktor Stibizew. What i need is for the digits at the start to be removed only the rest of the cell content can remain the same. AT:Nathan_Sav I cant follow the code logic unfortunately. Thanks for the the help guys its greatly appreciated. Commented Mar 10, 2017 at 11:30

5 Answers 5

5
CellVal = LTrimDigits(Cells(X, DataColumn))

With this fairly efficient:

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
Sign up to request clarification or add additional context in comments.

1 Comment

Efficient implementation with no useless string copies. I love it.
3

See the modified code below:

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

That is, while the starting char in CellVal is a digit, get the substring starting with the second char, and go on until no match.

1 Comment

BOO YAAHH!! Awesome work Wiktor. Thank you so much for taking the time. This works perfectly. Much respect from Dublin :-) Have a great day and thank you again. :-)
2

This function will strip leading digits and spaces from a 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

You can call it from your code by

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

A note on your code though:

You should really fully qualify your cells. For instance, wrap the whole looping section in With ThisWorkbook.Sheets("YourSheet") and then accessing cells using .Cells(row, col) rather than just Cells(row, col).

Comments

2

A bit hacky shorter alternative (assuming all values start with integer)

For Each cell in Range([G7], [G7].End(xlDown))

    cell.Value2 = Trim(Mid(cell, Len(Str(Val(cell)))))

Next

Comments

1

I think a simple change like this:

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

will solve your problem...

2 Comments

Nope, this will also remove the number at the end of the strings, which is the same as the number at the start of the strings! Good thinking though
yep tried that piece of code. Not working as hoped :-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.