Help with macro to move rows

shani

New Member
Joined
Dec 21, 2005
Messages
43
Hi, Ive looked for a solution on here but I cant seem to get any macro to work. I would like to run a macro that cuts out all the rows in the sheet "GSC: based on the value of "T" in column A and paste those rows to the bottom of the data in the sheet named Term Report. Then for last step, go back and delete the empty rows in the GSC sheet.

So for example, Row 5 in the GSC sheet needs cut because it has a T and pasted into worksheet named Term Report to bottom of sheet.

Ideally I run the macro and it looks for all the T's and moves all those rows to the Term Report at once and also deletes the empty rows that were deleted. Thanks in advance.

Shani
 

Attachments

  • mrexcel.png
    mrexcel.png
    119.8 KB · Views: 18

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
Try this:
VBA Code:
Sub Filter_Me_Please()
'Modified  11/10/2021  10:18:26 PM  EST
Application.ScreenUpdating = False
Dim lastrow As Long
Dim lastrowa As Long
lastrowa = Sheets("Term Report").Cells(Rows.Count, "A").End(xlUp).Row + 1
Dim C As Long
Dim s As Variant
C = 1 ' Column Number Modify this to your need
s = "T" 'Search Value Modify to your need
lastrow = Cells(Rows.Count, C).End(xlUp).Row
'Term Report
With ActiveSheet.Cells(1, C).Resize(lastrow)
    .AutoFilter 1, s
    counter = .Columns(C).SpecialCells(xlCellTypeVisible).Count
    If counter > 1 Then
        .Offset(1).Resize(.Rows.Count - 1).SpecialCells(xlCellTypeVisible).EntireRow.Copy Sheets("Term Report").Cells(lastrowa, 1)
        .Offset(1).Resize(.Rows.Count - 1).SpecialCells(xlCellTypeVisible).EntireRow.Delete
    
    Else
        MsgBox "No values found"
    End If
    .AutoFilter
End With
Application.ScreenUpdating = True
End Sub
 
Upvote 0
@shani, see if the following works for you:

VBA Code:
Sub TestDeleteRows()
'
    Application.ScreenUpdating = False
'
    Dim wsSource As Worksheet, wsDestination As Worksheet
    Dim DestinationRowIncrement As Long
    Dim LastRowB_Destination    As Long
    Dim SavedCelRow             As Long
    Dim Cel                     As Range
    Dim Rng                     As Range
'
    Set wsSource = Sheets("GSC")                                                                        ' <--- Set this to the proper sheet name
    Set wsDestination = Sheets("Term Report")                                                           ' <--- Set this to the proper sheet name
'
    DestinationRowIncrement = 0                                                                         ' Establish DestinationRowIncrement value
'
    LastRowB_Destination = wsDestination.Range("B" & Rows.Count).End(xlUp).Row                          ' Get Last used Row of column B in Destination Sheet
'
    Set Rng = wsSource.Range("A4:A" & wsSource.Range("B" & Rows.Count).End(xlUp).Row)                   ' Set range of column A to search
'
    For Each Cel In Rng                                                                                         ' Loop to check each cell in column A
        If Cel.Value = "T" Then                                                                                 '   If column A cell = 'T' then ...
            SavedCelRow = Cel.Row                                                                               '       Save the row that will be copied/deleted
            wsSource.Rows(Cel.Row).Cut wsDestination.Rows(LastRowB_Destination + DestinationRowIncrement + 1)   '       Cut/paste row to destination sheet
            DestinationRowIncrement = DestinationRowIncrement + 1                                               '       Increment Destination Row Counter
            wsSource.Rows(SavedCelRow).Delete Shift:=xlUp                                                       '       Delete Rowthat was cut from the Source file
        End If
    Next
'
    Application.ScreenUpdating = True
End Sub
 
Last edited:
Upvote 0
Sub TestDeleteRows() ' Application.ScreenUpdating = False ' Dim wsSource As Worksheet, wsDestination As Worksheet Dim DestinationRowIncrement As Long Dim LastRowB_Destination As Long Dim SavedCelRow As Long Dim Cel As Range Dim Rng As Range ' Set wsSource = Sheets("GSC") ' <--- Set this to the proper sheet name Set wsDestination = Sheets("Term Report") ' <--- Set this to the proper sheet name ' DestinationRowIncrement = 0 ' Establish DestinationRowIncrement value ' LastRowB_Destination = wsDestination.Range("B" & Rows.Count).End(xlUp).Row ' Get Last used Row of column B in Destination Sheet ' Set Rng = wsSource.Range("A4:A" & wsSource.Range("B" & Rows.Count).End(xlUp).Row) ' Set range of column A to search ' For Each Cel In Rng ' Loop to check each cell in column A If Cel.Value = "T" Then ' If column A cell = 'T' then ... SavedCelRow = Cel.Row ' Save the row that will be copied/deleted wsSource.Rows(Cel.Row).Cut wsDestination.Rows(LastRowB_Destination + DestinationRowIncrement + 1) ' Cut/paste row to destination sheet DestinationRowIncrement = DestinationRowIncrement + 1 ' Increment Destination Row Counter wsSource.Rows(SavedCelRow).Delete Shift:=xlUp ' Delete Rowthat was cut from the Source file End If Next ' Application.ScreenUpdating = True End Su

This worked but takes a long time to run. I had about 50 rows to move and it ran for awhile. How can I speed the macro up, is that possible? Thanks!!
 
Upvote 0
This worked but takes a long time to run. I had about 50 rows to move and it ran for awhile. How can I speed the macro up, is that possible? Thanks!!
And so what was wrong with the script I supplied in post #2
 
Upvote 0
HI, thanks for your help Im not sure but it did not work.
What did it do? It looks for "T" in column A
every row with "T" in column A gets copied to sheet named "Term Report"
And then all those rows are deleted from active sheet
 
Upvote 0
The only this I did not do was to refer to the sheet name.
I guess I missed sheet name "GSC"
I guess I assumed the script would be run from the sheet named "GSC"
 
Upvote 0
This worked but takes a long time to run. I had about 50 rows to move and it ran for awhile. How can I speed the macro up, is that possible? Thanks!!

This version should be freaky fast:

VBA Code:
Sub MoveDataToDifferentSheetViaArrays()
'
    Application.ScreenUpdating = False                                                                  ' Turn ScreenUpdating off
    Application.Calculation = xlCalculationManual                                                       ' Turn Calculations off
'
    Dim Column_A_Slot           As Long
    Dim DestinationArrayColumn  As Long, DestinationArrayRow    As Long, DestinationNextBlankRow    As Long
    Dim DataStartRow            As Long
    Dim TargetColumnNumber      As Long
    Dim RowsToDelete            As String
    Dim SourceArray             As Variant
    Dim DestinationArray()      As Variant
    Dim wsSource                As Worksheet, wsDestination     As Worksheet
'
          Set wsSource = Sheets("GSC")                                                                  ' <--- Set this to the proper sheet name
     Set wsDestination = Sheets("Term Report")                                                          ' <--- Set this to the proper sheet name
          DataStartRow = 4                                                                              ' <--- Set this to the starting row of data
    TargetColumnNumber = 1                                                                              ' <--- Set this to the column # you are checking
'
    With wsSource.Range("A" & DataStartRow).CurrentRegion                                               ' Source sheet data range
        SourceArray = .Value                                                                            '   Load Source sheet data into SourceArray
'
        For Column_A_Slot = 1 To UBound(SourceArray, 1)                                                 '   Loop for column A
            If SourceArray(Column_A_Slot, TargetColumnNumber) = "T" Then                                '       If cell in column A = 'T' then ...
                DestinationArrayColumn = DestinationArrayColumn + 1                                     '           increment DestinationArrayColumn
'
                ReDim Preserve DestinationArray(1 To UBound(SourceArray, 2), 1 To DestinationArrayColumn)   '       Redim DestinationArray keeping current values
'
                For DestinationArrayRow = 1 To UBound(DestinationArray, 1)
                    DestinationArray(DestinationArrayRow, DestinationArrayColumn) = SourceArray(Column_A_Slot, DestinationArrayRow) ' Save the row containing
'                                                                                                                                   '    'T' into DestinationArray
                Next
'
                RowsToDelete = RowsToDelete & Column_A_Slot + DataStartRow - 1 & ":" & Column_A_Slot + DataStartRow - 1 & ","   ' Save row to delete into string
'
                If Len(RowsToDelete) > 240 Then                                                         ' If Length of RowsToDelete string > 240 then ...
                    RowsToDelete = Left(RowsToDelete, Len(RowsToDelete) - 1)                            ' Remove the the trailing comma at the end of the string
                    wsSource.Range(RowsToDelete).Delete                                                 '   Delete the saved Rows To Delete thus far
                    RowsToDelete = vbNullString                                                         '   Clear the RowsToDelete string
                End If
            End If
        Next
'
    End With

    With wsDestination                                                                                  ' With the Destination sheet ...
        DestinationNextBlankRow = .Range("B" & Rows.Count).End(xlUp).Row + 1                            '   Save next blank row available in Destination sheet
'
'       Display Moved rows from Source sheet in the Destination sheet
        .Range("A" & DestinationNextBlankRow).Resize(UBound(DestinationArray, 2), UBound(DestinationArray, 1)) = Application.Transpose(DestinationArray)
'
        .Columns.AutoFit                                                                                ' Adjust column sizes to fit the Moved rows data
    End With
'
    RowsToDelete = Left(RowsToDelete, Len(RowsToDelete) - 1)                                            ' Remove the the trailing comma at the end of the string
'
    wsSource.Range(RowsToDelete).Delete                                                                 ' Delete any remaining saved Rows To Delete
'
    Application.Calculation = xlCalculationAutomatic                                                    ' Turn Calculations back on
    Application.ScreenUpdating = True                                                                   ' Turn ScreenUpdating back on
End Sub
 
Upvote 0
I did a time trial, counting in my head, and it took about 4 seconds to go through 10k rows. :cool:
 
Upvote 0

Forum statistics

Threads
1,215,063
Messages
6,122,935
Members
449,094
Latest member
teemeren

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