breaking a cell into two cells via VBA program

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hello,
I have a bunch of numers written in an excell sheet that I want to be able
to seperate the first two letters and move the rest to collumn B.

Ex)
Column A Column B
37001-03501
38001-03502
39001-03503
40001-03504

would become
A B
37 001-03501
38 001-03502
39 001-03503
40 001-03504

Thank you very much for any help with this,
Jordan
 
Hi Jordan,

Select the numbers
Data | Text to columns
Check "Fixed Width"
In the Data Preview window, click between the 2nd and 3rd digits of any of
the numbers
Finish
 
Hi Jordan,

Using VBA, try:

Sub Test()
Dim rng As Range
Dim cell As Range

Columns(2).Insert 'to blank cells in column B
Set rng = Range("A1").CurrentRegion

For Each cell In rng.Cells
cell(1, 2).Value = Mid(cell.Value, 2)
cell.Value = Left(cell.Value, 2)
Next

End Sub
 
Hi Jordan,

Columns(2).Insert 'to blank cells in column B

is mising the word "ensure" in the appended comment and should read:

Columns(2).Insert 'to ensure blank cells in column B
 
Sub SplitValues()
Dim sStr as String, rng as Range
set rng = Range("A1:A2000")
for each cell in rng
sStr = Trim(Cell.Value)
if sStr <> "" then
Cell.Value = "'" & Left(sStr,2)
cell.offset(0,1).Value = Right(sStr,len(sStr)-1)
end if
Next
End sub
 
Hi Jordan,

In Tom's SplitValues Sub, which I prefer to my suggestion, there is a small
typo in the line:
cell.offset(0,1).Value = Right(sStr,len(sStr)-1)

which should read:

cell.offset(0,1).Value = Right(sStr,len(sStr)-2)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top