VBA Create spaces before the first character wherever there is a number and a minus sign

Slavio

Board Regular
Joined
Mar 28, 2021
Messages
59
Office Version
  1. 365
Platform
  1. Windows
  2. Web
Hi guis,
Can someone help me with some code where I can v hroughout the Sheet in the table to check the data and change the data directly in the cell where the error is?

If there is a letter in the cell at the beginning, add a space before.
If the gap is already there then continue
ABCDEFG
H345545.2301-03-2021mnohjupi-032Lopa25
<ODK243323RLO*56412898874B*>

What the table should look like after verification (The space is marked in bold)

H345545.2301-03-2021 mnoh jupi-032 Lopa25
<ODK243323 RLO *56412898874 B*>
Any idea?
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
Hi Slavio - The code below may help you get started. Hope this helps.

VBA Code:
Sub FirstChar()
'This First Character test is set to look at only Cell A1 or Cells(1,1).Value
    Dim xlString As String
    Dim xlFirstChar As String

    xlString = Cells(1, 1).Value

    xlFirstChar = Left$(xlString, 1)
    
'Test if first character is NOT numeric and if so - add a space before.
    If Not IsNumeric(xlFirstChar) Then
    MsgBox "Yes"
    xlString = " " & xlString
    
    Cells(1, 1).Value = xlString
    End If
    
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,587
Messages
6,120,406
Members
448,958
Latest member
Hat4Life

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top