selecting even rows in A and B columns - macro please -urgent

osmanoca

Board Regular
Joined
Apr 16, 2016
Messages
87
Dear Mrexcel lovers and helpfull friends.

hello please help me urgently with macro

i have two columns. A and B
i have many rows. but i want to select all even numbered rows (2,4,6,8.....) in two columns. because i have to merge and center these rows in columns in one click.

im selecting now one by one and this is very hard.
please give me a macro.
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
Personally I would seriously look at Center across selection rather than merge but here you go.
Code:
Sub EvenIt()
    Dim LastRow As Long
    Dim x As Long

    LastRow = Range("A" & Rows.Count).End(xlUp).Row
    With Application
        .DisplayAlerts = False
        .ScreenUpdating = False
    End With

    For x = 1 To LastRow
        If (x Mod 2) = 0 Then
            With Cells(x, 1).Resize(, 2)
                .HorizontalAlignment = xlCenter
                .MergeCells = True
            End With
        End If
    Next x

    With Application
        .DisplayAlerts = True
        .ScreenUpdating = True
    End With
End Sub
 
Last edited:
Upvote 0
This allows a variable range and you can go into the code and choose even or odd...

Code:
Sub sb_select_even_odd_cells()

Dim even_odd As Integer

' if you want even cells, set to 0.  If you want odd, set to 1
even_odd = 1

Dim rng As Range
Dim MyRange As Range

Set rng = Selection

Dim cel As Range

    For Each cel In rng
        If cel.Row Mod 2 = even_odd Then
            If MyRange Is Nothing Then
                Set MyRange = Range(cel.Address)
            Else
                Set MyRange = Union(MyRange, Range(cel.Address))
            End If
        End If
    Next
     'Select the new range of only matching criteria
    MyRange.Select
    
    With MyRange
        .HorizontalAlignment = xlCenter
        .MergeCells = True
    End With


End Sub
 
Upvote 0
Personally I would seriously look at Center across selection rather than merge but here you go.
Code:
Sub EvenIt()
    Dim LastRow As Long
    Dim x As Long

    LastRow = Range("A" & Rows.Count).End(xlUp).Row
    With Application
        .DisplayAlerts = False
        .ScreenUpdating = False
    End With

    For x = 1 To LastRow
        If (x Mod 2) = 0 Then
            With Cells(x, 1).Resize(, 2)
                .HorizontalAlignment = xlCenter
                .MergeCells = True
            End With
        End If
    Next x

    With Application
        .DisplayAlerts = True
        .ScreenUpdating = True
    End With
End Sub


THANKS REALLY VERY GOOD WORKED. IM HAPPY. GOD ALSO HELPS YOU ALLWAYS IN YOUR LIFE.
THANKS TO OTHER FRIENDS ALSO.

this code merged even rows well. and i want to add a new row under all even rows (that merged under A and B) now.
please give me a macro.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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