Macro: Move active row to top of VISIBLE rows?

jeffcoleky

Active Member
Joined
May 24, 2011
Messages
274
Rows 2-XXXXX are all hidden. The row number of the last hidden row increases occasionally.

I need a macro that will move the row of currently active cell, to the top of the VISIBLE (unhidden) rows.

For instance, if rows 2-1500 are hidden and the cursor is on cell D1600, when the macro is run it would CUT row 1600 and paste it (insert cut cells) between as 1501.

Can anyone please help?
 

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
Try the following. It's based on Column D having values in every row.

Code:
Sub MoveRowtoTopOfVisibleRange()
ActiveCell.EntireRow.Cut
With Range("D" & Rows.Count).End(xlUp).CurrentRegion.SpecialCells(xlCellTypeVisible)
    For i = 1 To .Areas.Count
        If Not Intersect(.Areas(i), Cells(ActiveCell.Row, 4)) Is Nothing Then
            intersectArea = i
        End If
    Next i
    .Areas(intersectArea).Cells(1, 1).EntireRow.Insert
End With
End Sub
 
Upvote 0
Try the following. It's based on Column D having values in every row.

Code:
Sub MoveRowtoTopOfVisibleRange()
ActiveCell.EntireRow.Cut
With Range("D" & Rows.Count).End(xlUp).CurrentRegion.SpecialCells(xlCellTypeVisible)
    For i = 1 To .Areas.Count
        If Not Intersect(.Areas(i), Cells(ActiveCell.Row, 4)) Is Nothing Then
            intersectArea = i
        End If
    Next i
    .Areas(intersectArea).Cells(1, 1).EntireRow.Insert
End With
End Sub

Works fantastically, just as I requested. However, I mistakenly stated I wanted it to work on one row at a time. Would you be able to easily modify this so that it works on multiple selected rows?
 
Upvote 0
A few more checks are required. Try the following:

Code:
Sub MoveRowtoTopOfVisibleRange2()
Dim rTest As Range
'check for valid selection
Set rTest = Range("D" & Selection.Cells(1, 1).Row & ":D" & Selection.Cells(Selection.Rows.Count, 1).Row)
If rTest.Count > 1 Then
    If rTest.Count <> rTest.SpecialCells(xlCellTypeVisible).Count Then
        MsgBox "That selection is invalid as it crosses hidden rows."
        Exit Sub
    End If
End If
Selection.EntireRow.Cut
With Range("D" & Rows.Count).End(xlUp).CurrentRegion.SpecialCells(xlCellTypeVisible)
    For i = 1 To .Areas.Count
        If Not Intersect(.Areas(i), rTest) Is Nothing Then
            intersectArea = i
        End If
    Next i
    .Areas(intersectArea).Cells(1, 1).EntireRow.Insert
End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,243
Messages
6,170,967
Members
452,371
Latest member
Frana

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