VBA put space between string and numbers

Bablu

Board Regular
Joined
Dec 9, 2008
Messages
131
Office Version
  1. 2016
Platform
  1. Windows
Hi All,

I have a quick request. I have some data in a cell B2 such as Feeder5846. What I would like to do is a put 2 spaces between the letters and numbers so that it is like Feeder 5846.

Currently, I have a formula to do this like =LEFT(B2,6)&" "&RIGHT(B2,LEN(B2)-6)

I would like to do this using VBA such as...

For each cell in selection

Cell.value = "VBA command goes here"

Next

This is not so much out neccesity - I want know how to do this. I searched for it on the web but did not find anything.

Any help would be great.

Thanks,

Bablu
 
I think some conditional formatting rules were slowing it down, but mostly I think it was the fact that I was selecting the entire column, rather than just selecting the cells I wanted to fix.

Mostly just out of curiosity now, but is there a way to do a quick test & have excel just skip a cell if it is blank?
Yes, selecting the entire column, with your code as written, would be a problem as it will physically look at each cell. To skip blank cells, you could put this as the first line of code in my function...

Code:
If Len(str) = 0 Then Exit Sub
Doing that would help a little, but you would still lose time as Excel looked at each and every cell in the column (when you select the entire column). You could "short-circuit" this problem by changing your "test" sub to this...

Code:
vSub test()
Dim c As Range
For Each c In Intersect(Selection, ActiveSheet.UsedRange)
    c = InsertSpace(c.Text)
Next
MsgBox "macro has finished!"
End Sub
Note what I did with your For Each statement... that restricts the loop to cells within your data range even if you select the entire column to process.
 
Upvote 0
I'll test that out later today. Thanks so much for the assistance. I really appreciate you taking the time to help me out and explain this.
 
Upvote 0
Hi i have similer issue i need to turn these serial numbers
3476bf
1448cu
6649ju
8843a
1843d
8497c
123ada
456dab
658sam

into this with space
3476 bf
1448 cu
6649 ju
8843 a
1843 d
8497 c
123 ada
456 dab
658 sam
 
Upvote 0
Give this macro a try...

Rich (BB code):
Sub InsertSpaceInSerialNumber()
  Dim R As Long, P As Long, vArr As Variant
  vArr = Range("A2:A" & Cells(Rows.Count, "A").End(xlUp).Row)
  For R = 1 To UBound(vArr)
    For P = 1 To Len(vArr(R, 1))
      If Mid(vArr(R, 1), P, 1) Like "[!0-9]" Then
        vArr(R, 1) = WorksheetFunction.Replace(vArr(R, 1), P, 0, " ")
        Exit For
      End If
    Next
  Next
  Range("A2:A" & Cells(Rows.Count, "A").End(xlUp).Row) = vArr
End Sub

Change the three red "A" letters to the letter designation for the column containing your serial numbers and change the green "2" on that same code line to the row number where your first serial number is located.
 
Upvote 0
Try this:

Code:
Sub test()
Dim r As Range
For Each r In Range("A1:A" & Range("A" & Rows.Count).End(xlUp).Row)
    r = Val(r) & " " & Replace(r, Val(r), "")
Next
End Sub
 
Upvote 0
Try this:

Code:
Sub test()
Dim r As Range
For Each r In Range("A1:A" & Range("A" & Rows.Count).End(xlUp).Row)
    r = Val(r) & " " & Replace(r, Val(r), "")
Next
End Sub
I believe my approach will execute quicker than yours; however, putting that aside for the moment, your code will not handle a serial number that starts with one or more zeroes correctly (the Val function will "eat" them up).
 
Upvote 0

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