Inserting space between number and letter

mikezoro

New Member
Joined
Dec 15, 2012
Messages
11
Hi again, guys!

I was looking at the old posts in the forum and I ran into this post:

http://www.mrexcel.com/forum/excel-...cations-put-space-between-string-numbers.html

My problem is almost the same, the problem is that I need to add a space in between a number and a letter and these macros seem to work the other way around (creating a space between a letter and a number).

Tried to change some things... still no results. I have a ~16,000 row file, and some cells have measurement units like:

2,200c.u. or 426cc

I'm looking for a way to add a space in between IF there's no space, there are some rows that are correctly formatted and I'd hate to have double spaces there. Any help is, as usual, very welcome.

Column to be formatted would be "H" (there are other columns with data that I'm not supposed to change). Using Excel 2010.
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
Are the values always like 2,200c.u and 426cc where the digits appear first and then the alphabets?
 
Upvote 0
Hi guys...

Here's more info:

Columns A -F (Untouchable stuff like ID codes, etc.
Column G English version of the data (this is an XLSX translation file)
Column H Spanish version of the data

Some rows are just words like

ENGLISH
SPANISH
Model
Modelo
Banner (2.8in.x8in.)
Banner (2.8pulg.x8pulg.)
80mm
80mm

<tbody>
</tbody>

In the last two rows, I'd need to add a space (2.8in >> 2.8 and 80mm >> 80 mm)

hope this info helps
 
Upvote 0
Give this macro a try...
Code:
Sub InsertSpaceBetweenDigitAndLetter()
  Dim X As Long, Z As Long, LastRow As Long, vArr As Variant
  Const DataColumn As String = "A"
  Const StartRow As Long = 2
  LastRow = Cells(Rows.Count, DataColumn).End(xlUp).Row
  vArr = Cells(StartRow, DataColumn).Resize(LastRow - StartRow + 1)
  For X = 1 To UBound(vArr)
    For Z = Len(vArr(X, 1)) - 1 To 1 Step -1
      If Mid(vArr(X, 1), Z, 1) Like "#" And Mid(vArr(X, 1), Z + 1, 1) Like "[A-Za-z]" Then
        vArr(X, 1) = Left(vArr(X, 1), Z) & " " & Mid(vArr(X, 1), Z + 1)
      End If
    Next
  Next
  Cells(StartRow, DataColumn).Resize(LastRow - StartRow + 1) = vArr
End Sub
You can adjust the constants (Const statements) to match your actual setup.
 
Upvote 0
Hi Ric...

Your macro does the trick... however, it changed the cells with numbers to a different format...

Ex. 1/1 changed to Jan-01... and 23578 (left aligned) changed to 23578 (right aligned).

other than that, it did the trick... thanks a lot!
 
Upvote 0
Hi Ric...

Your macro does the trick... however, it changed the cells with numbers to a different format...

Ex. 1/1 changed to Jan-01... and 23578 (left aligned) changed to 23578 (right aligned).

other than that, it did the trick... thanks a lot!
I am not sure what you are referring to here... can you show us sample original data that is getting changed the way you indicate above?
 
Upvote 0
English
Spanish
Flag 15m
Bandera 15 m
1/1
Jan-01
512478
512478
24/2
Feb-24

<tbody>
</tbody>

Anyway.. I must say that there were just a few... and I was able to put them back to "text" status... it took me way less time than looking and correcting all of the measurement units... so, that last macro was a huge success... thanks a lot!
 
Upvote 0
English
Spanish
Flag 15m
Bandera 15 m
1/1
Jan-01
512478
512478
24/2
Feb-24

<TBODY>
</TBODY>

Anyway.. I must say that there were just a few... and I was able to put them back to "text" status... it took me way less time than looking and correcting all of the measurement units... so, that last macro was a huge success... thanks a lot!
For future use, I think this macro will work the way you actually want...
Code:
Sub InsertSpaceBetweenDigitAndLetter()
  Dim X As Long, Z As Long, LastRow As Long, vArr As Variant
  Const DataColumn As String = "A"
  Const StartRow As Long = 2
  LastRow = Cells(Rows.Count, DataColumn).End(xlUp).Row
  vArr = Cells(StartRow, DataColumn).Resize(LastRow - StartRow + 1).Value
  For X = 1 To UBound(vArr)
    For Z = Len(vArr(X, 1)) - 1 To 1 Step -1
      If Mid(vArr(X, 1), Z, 1) Like "#" And Mid(vArr(X, 1), Z + 1, 1) Like "[A-Za-z]" Then
        vArr(X, 1) = Left(vArr(X, 1), Z) & " " & Mid(vArr(X, 1), Z + 1)
      End If
    Next
  Next
  Cells(StartRow, DataColumn).Resize(LastRow - StartRow + 1).NumberFormat = "@"
  Cells(StartRow, DataColumn).Resize(LastRow - StartRow + 1) = vArr
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,848
Messages
6,121,914
Members
449,054
Latest member
luca142

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