Loop / Do while macos. Need help

Evgeny

New Member
Joined
Dec 12, 2012
Messages
7
I'm new in macros, went through previous posts but can't make mine work.

I have a worksheet (MS office 2007) with table. I'd like to merge cell 1 and 2 in each columns till there is text in cell 1 and 2. Below is example for column A:

Sub Macro7()
'
' Macro7 Macro
'

'
Range("A1:A2").Select
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlCenter
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
Selection.Merge
End Sub

Thanks
Evgeny
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
Try

Code:
Sub mergecells()
    c = 1
    Do
        Range(Cells(1, c), Cells(2, c)).Select
        Selection.mergecells = True
        c = c + 1
    Loop Until Cells(1, c) <> "" And Cells(2, c) <> ""
End Sub
 
Last edited:
Upvote 0
Try

Code:
Sub mergecells()
    c = 1
    Do
        Range(Cells(1, c), Cells(2, c)).Select
        Selection.mergecells = True
        c = c + 1
    Loop Until Cells(1, c) <> "" And Cells(2, c) <> ""
End Sub

nope. merge A1 and A2, but doesn't continue for the rest columns.
 
Upvote 0
I dont get. The macro is to merge rows(1 and 2) of column A and then proceed to rows(1 and 2) of column B and so on till it gets to a column where rows(1 and 2) are not blank.

Is that right?
 
Upvote 0
I just ran it and it merged up to column F 'cos i had empty rows from A to E.

Are you certain that column B is blank(rows 1 and 2)? or post a sample of your workshett
 
Upvote 0
Meaning it should run while there are no texts maybe numbers and it should cease when it encounters texts,is that it?
 
Upvote 0
Macros should merge the cells 1 and 2 with any inputs (text, digits). Stop macros if cells are empty.
 
Upvote 0
When you merge rows in the same column, you get a warning that only the upperleftmost value would be displayed

Is this what you want? (Just one value displayed))

what i have done is to suppress that warning in this macro and still merge the cells all the same

Code:
Sub mergecells()
    Application.DisplayAlerts = False
    c = 1
        Do
            Range(Cells(1, c), Cells(2, c)).Select
            Selection.mergecells = True
            c = c + 1
        Loop Until Cells(1, c) = "" And Cells(2, c) = ""
    Application.DisplayAlerts = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,579
Messages
6,125,646
Members
449,245
Latest member
PatrickL

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