Need a simple macro

Hi,

I'm new to VBA programming, and can really only so far adapt a similar macro to do what I need. But I can't find a macro to modify to help me with my current problem.  Can anyone help?

It should be fairly simple I think.  I have lots of rows of text which have 2 pieces of info in, separated by a hyphen.  What I want is a macro to cut all the text after the hyphen and paste it into the next row.  So instead of:

Column A

Info - Additional

Text- More text

I need:

Column A       Column B

Info -             Additional

Text -            More Text

 

Thanks for any help you can give a VBA newbie!!

 

Comments
Andrew Burnett's picture

Splitting Text

Andrew Burnett | | Permalink

The following should do it for you. Paste into the macro sheet for the sheet containing your data.

Sub splittext()
    Dim I As Long
    I = 1
    Do While Not IsEmpty(Cells(I, 1).Value)
        Cells(I, 1).Resize(1, 2).Value = Split(Cells(I, 1).Value, "- ")
        Cells(I, 1).Value = Cells(I, 1).Value & "-"
        I = I + 1
    Loop
End Sub
 

Andrew
Logic Software and Systems Ltd.
www.logicarchitect.co.uk/cs

 

VBA not the only option?

paulwakefield1 | | Permalink

You could do this by formula if you wish and if you are happy to have the results in different columns.

e.g. In column C, =LEFT(A1,FIND("-",A1)) and, in column D, =RIGHT(A1,LEN(A1)-FIND("-",A1))

splitting text

ted.henderson | | Permalink

why not use the excel engine and avoid unnecessary vba?

if your original text is in A5 for example put into B5 for the first part

=TRIM(LEFT(A5,FIND("-",A5,1)-1) )

which finds the position of the hyphen and trims the characters to the left to remove unwanted spaces

and into C5 for the second

=TRIM(RIGHT(A5,LEN(A5)-(FIND("-",A5,1)+1)))

which finds the position of the hyphen and trims the characters to the right after calculating the length of the string

Copy the furmulae down.

John

A Simpler Way

ianw33 | | Permalink

If you don't need the hyphen, use Excel's "Text to Columns" feature. In the wizard, opt for delimited and enter a hyphen in the "other" box.

It's in the section in 2010 - sorry can't remember where for earlier versions, but it is there.

Add comment
Log in or register to post comments
Group: ExcelZone
A gathering place for the Excel community to explore new ideas and techniques and a forum to debate product features and best practices.