PDA

View Full Version : Solved: little sub that can MERGE



Gingertrees
11-15-2009, 05:24 AM
Hello,
I hope this is an easy fix for someone - I want to do a repeated merge for the range I select.
So if I select D1:L12, I want to merge
D1:L1
D2:L2
D3:L3 etc down to D12:L12
Here's what I came up with that merges nothing.

Am I on the right track at least?
Sub MergeRows()
Dim MergeRange As Range
Dim Cell1 As Object
Dim CellLast As Object

With ActiveSheet
On Error Resume Next

Cell1 = InputBox("1st cell?")
CellLast = InputBox("Last cell?")

MergeRange = Range(Cell1, CellLast)

For Each Row In MergeRange.Rows
Range(Cells(MergeRange.Row, 1), Cells(MergeRange.Row, 9)).Merge
Next Row

End With

End Sub

mdmackillop
11-15-2009, 05:38 AM
Even simpler

Dim r As Range
For Each r In Selection.Rows
r.Merge
Next

mdmackillop
11-15-2009, 06:20 AM
To fix your code, althogh I would not recommend Row as a variable name,

Sub MergeRows()
Dim MergeRange As Range
Dim Row As Range
Dim Cell1 As String
Dim CellLast As String

With ActiveSheet
On Error Resume Next

Cell1 = InputBox("1st cell?")

CellLast = InputBox("Last cell?")

Set MergeRange = Range(Range(Cell1), Range(CellLast))

For Each Row In MergeRange.Rows
Row.Merge
Next Row

End With

End Sub

Gingertrees
11-15-2009, 07:07 AM
Thanks a lot!