Macro to create new line in cell

BMil8

Board Regular
Joined
Aug 9, 2010
Messages
153
Office Version
  1. 365
Hello,

I have a list of about 1,000 zip codes in a cell, all of which are seperated by a semicolon. After every 7th semicolon I would like to create a new line. (I'm having a problem with text wrapping when loading the data into our software and it needs to be formatted this way.) I could manually do this by using Alt/Enter, but this would take way to long.

Is there a vba code that will look at the cell and enter a new line after every 7th semicolon?

Thanks so much,
Brian
 

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
So I guess we could also use after every 42nd character if that makes it easier.
 
Upvote 0
try this:

Code:
Public Sub test()

Dim aStr As Variant
Dim i As Long
Dim sOutput As String
Dim lCount As Long

aStr = Split(Sheet1.Range("H7").Value, ";")

lCount = 7
For i = 0 To UBound(aStr, 1)
    sOutput = sOutput & aStr(i) & ";"
    If i + 1 = lCount Then
        sOutput = sOutput & vbCrLf
        lCount = lCount + 7
    End If
Next i

MsgBox sOutput

End Sub
 
Upvote 0
Hi Chris,

It's odd. Nothing happens except that I get a blank pop-up box.
 
Upvote 0
Well, you have to adapt the code! It's looking at sheet 1, cell h7. You need to change that to the correct sheet and cell.

It's also not saving the data in the worksheet, instead of the
Message box you need to specify a cell to save to.

You didn't provide all the specific details so I assumed
You just needed the basics.
 
Upvote 0
I'm sorry I'm still kind of new to this. I actually did update the sheet and the cell though. But how do I change it so it removes the message box and puts it in another cell?

Thanks for your help!
 
Upvote 0
Remove the msgbox line and replace with:

Sheets("yoursheetname").range("a1") = soutput

Change a1 to your desired cell
 
Upvote 0

Forum statistics

Threads
1,224,582
Messages
6,179,670
Members
452,936
Latest member
anamikabhargaw

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