Moving multiple rows from one sheet to another using AutoFilter to cut and delete

Jakezer

New Member
Joined
Mar 24, 2022
Messages
19
Office Version
  1. 2019
Platform
  1. Windows
Hello,
Few days ago I asked for a way to move rows from one sheet to another, got two methods, one with autofilter and one without,
The no-autofilter one worked for me but on a single row each time since my code is being refreshed continuesly, didnt work on multiple rows en masse, so am tryna use the AutoFilter one suggested by Mr. kevin9999
I edited it a little since my code is being refreshed and so the rows number is variable, also I used cut and delete instead of copy and delete, since cut takes the checkbox with and copy doesnt;

I faced these issues:
- Error highliting in yellow the line where i used cut property
- It cuts all the rows not only the ones containing "0" in range D
- When resizing the rows, the checkboxes below the cutted area get bugged or deformed (their tick box become smaller in appearance but the big checkbox size remain the same),
is there a way to prevent that from happening please? (like avoiding to use resize, or adding application.screen update = false or something ? idk )


VBA Code:
Sub Move()
    Dim ws1 As Worksheet, ws2 As Worksheet, lr As Long
    Set ws1 = Worksheets("Sheet1")
    Set ws2 = Worksheets("Sheet2")
    lr1 = ws1.Cells(Rows.Count, 1).End(xlUp).Row
    lr2 = ws2.Cells(Rows.Count, 1).End(xlUp).Row + 1
 For i = 1 To lr1
    With ws1.Cells(i).CurrentRegion
        .AutoFilter 4, "0"
        .Offset(1).Resize(.Rows.Count - 1, 5).cut ws2.Cells(lr2, 1)    '  Error 1004 (eventhough changes are being applyied)
        .Offset(1).Resize(.Rows.Count - 1).EntireRow.Delete
        .AutoFilter
    End With
    Next i
End Sub
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
Okay, I added Dim lr1 As Long, Dim lr2 As Long like this and now it cuts everything but without the checkboxes, and it leaves them deformed in sheet1

VBA Code:
Sub Move()
    Dim ws1 As Worksheet, ws2 As Worksheet, lr1 As Long, lr2 As Long
    Set ws1 = Worksheets("Sheet1")
    Set ws2 = Worksheets("Sheet2")
    lr1 = ws1.Cells(Rows.Count, 1).End(xlUp).Row
    lr2 = ws2.Cells(Rows.Count, 1).End(xlUp).Row + 1
 For i = 1 To lr1
    With ws1.Cells(i).CurrentRegion
        .AutoFilter 4, "0"
        .Offset(1).Resize(.Rows.Count - 1, 5).cut ws2.Cells(lr2, 1)    '  Error 1004 (eventhough changes are being applyied)
        .Offset(1).Resize(.Rows.Count - 1).EntireRow.Delete
        .AutoFilter
    End With
    Next i
End Sub
 
Upvote 0
The following code has been written assuming your data is exactly as you indicated in This post.

VBA Code:
Option Explicit
Sub jakezer_2()
    Dim ws1 As Worksheet, ws2 As Worksheet
    Dim lr1 As Long, lr2 As Long, i As Long
    Dim c As Range
    Set ws1 = Sheet1
    Set ws2 = Sheet2
    lr1 = ws1.Cells(Rows.Count, 4).End(3).Row
    lr2 = ws2.Cells(Rows.Count, 1).End(3).Row + 1
    
    For i = lr1 To 2 Step -1
        If ws1.Cells(i, 4) = 0 Then
            ws1.Cells(i, 5).Cut ws2.Cells(lr2, 5)
            ws1.Range(ws1.Cells(i, 1), ws1.Cells(i, 4)).Copy ws2.Cells(lr2, 1)
            ws1.Cells(i, 1).EntireRow.Delete
            lr2 = lr2 + 1
        End If
    Next i
End Sub
 
Upvote 0
Solution
Thanks but.. Am sorry if I didnt point this out at the first place but that was just a portion of an constantly-refreshing table, rows number can be changed at any given moment
U wrote .End(3) because I had 4 rows right? How do I code smth that loops through all the rows till the last one that contains data in either of any of the cells A - D pls ? I change it to .End(xlUp) right ? Or add smth like .UsedRows ?
 
Upvote 0
Thanks but.. Am sorry if I didnt point this out at the first place but that was just a portion of an constantly-refreshing table, rows number can be changed at any given moment
U wrote .End(3) because I had 4 rows right? How do I code smth that loops through all the rows till the last one that contains data in either of any of the cells A - D pls ? I change it to .End(xlUp) right ? Or add smth like .UsedRows ?
.End(3) = .End(xlUp)
 
Upvote 0
This is what Sheet 1 looks like before I run the code in post #3
EDIT: Unfortunately, the XL2BB tool is not picking up the checkboxes in column E. I assure you, they are being copied.

Filter and Cut.xlsb
ABCDE
1SubjectDateSenderStateVerification
2a6/3/2022sender1
3b23/3/2022sender0
4c28/3/2022sender1
5d28/3/2022sender2
6a6/3/2022sender1
7b23/3/2022sender0
8c28/3/2022sender1
9d28/3/2022sender2
10
Sheet1


Sheet 2 looks like this before running the code

Filter and Cut.xlsb
ABCDE
1SubjectDateSenderStateVerification
2
Sheet2


After the code is run, Sheet 1 looks like this

Filter and Cut.xlsb
ABCDE
1SubjectDateSenderStateVerification
2a6/3/2022sender1
3c28/3/2022sender1
4d28/3/2022sender2
5a6/3/2022sender1
6c28/3/2022sender1
7d28/3/2022sender2
8
Sheet1


and Sheet 2 looks like this

Filter and Cut.xlsb
ABCDE
1SubjectDateSenderStateVerification
2b23/3/2022sender0
3b23/3/2022sender0
4
Sheet2
 
Upvote 0
Working like a charm, thank you so much bro and sorry if there was any misunderstanding. ❤️
 
Upvote 0

Forum statistics

Threads
1,224,828
Messages
6,181,217
Members
453,024
Latest member
Wingit77

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