Code to put blank line before text in each cell of column

skyport

Active Member
Joined
Aug 3, 2014
Messages
374
I have text that is pasted in cells in column G of page 2. In each of those cells containing text, I need a code that would place a blank line before the text and in essence, place the text in the cell on line 2 within the cell.

Can anyone help with this one?
 

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
So, it sounds like you want to insert a soft-carriage return at the beginning of any cell in column G that has an entry. Is that correct?

If so, this should do that:
Code:
Sub MyMacroCode()

    Dim myLastRow As Long
    Dim myRow As Long

    Application.ScreenUpdating = False
    
'   Find last row in column G
    myLastRow = Cells(Rows.Count, "G").End(xlUp).Row
    
'   Loop through all cells in column G
    For myRow = 1 To myLastRow
'       Check to see if there is a value in column G...
        If Cells(myRow, "G") <> "" Then
'           ...if so, insert soft carriage return
            Cells(myRow, "G") = Chr(10) & Cells(myRow, "G")
        End If
    Next myRow
    
    Application.ScreenUpdating = True
        
End Sub
 
Upvote 0
Hi Joe, thanks so much for helping. What happens when I run the code, it does move the text down creating a one line space however, it and dance the first line inward by a character or two. When I tried a backspace to eliminate the one or two spaces before the first word, it eliminates the one line space in its entirety that was created by the code. This would create a problem because the first sentence has to began at the far left margin just like the other sentences below. Perhaps even more important is it would prevent me from doing a function that occurs after the code would be ran and that was the leader and inserting other text, such as headlines are notes in that blank line space that the code would create. For some reason the code does not allow that as if there is actually no true blank space above the text that can be added into with more text. Any thoughts about how to make that work? Once again I sure appreciate any help you are able to give.
 
Upvote 0
I am not sure I understand your question. I am not showing any indentation. Everything is left justified. Do you have other characters or spaces in your data to start with?

All that code does is insert a soft carriage return before the beginning of your entry (which is what you asked for). That is what the Chr(10) is (a soft carriage return). If you want some spaces in the first row before the carriage return, just add them in to the code, i.e.
change this line:
Code:
'           ...if so, insert soft carriage return
            Cells(myRow, "G") = Chr(10) & Cells(myRow, "G")
to this:
Code:
'           ...if so, insert soft carriage return
            Cells(myRow, "G") = " " & Chr(10) & Cells(myRow, "G")
You can place as many spaces as you would like between the two quotes.
 
Upvote 0
Joe, sorry. It now works perfectly. there was a conflict I discovered working against the code, now resolved. Your solution is now perfect. Thanks so much for the help.
 
Upvote 0

Forum statistics

Threads
1,223,238
Messages
6,170,939
Members
452,368
Latest member
jayp2104

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