Using UsedRange copies empty rows at the end

DJhuffman

New Member
Joined
Apr 16, 2019
Messages
25
Here's a bit of a puzzler. I built a code to do the following steps:

1. Apply a filter
2. Copy visible cells
3. Paste the visible cells to a new sheet
4. Rename the new sheet
5. Migrate back to the old sheet and delete the visible rows (thus removing them from the unfiltered data)

Trouble is, when the process is done, the active range (as determined by Ctrl+Shift+End) is showing row 1,000,000 (when I only copied over a few thousand rows). Is there something I can do to prevent this from happening?

Here's a sample of the code. Any help is always appreciated.

Code:
Sub lite360()


    ActiveSheet.UsedRange.AutoFilter Field:=11, Criteria1:="-"
    Selection.SpecialCells(xlCellTypeVisible).Select
    Selection.Copy
    Sheets.Add After:=ActiveSheet
    ActiveSheet.Paste
    Selection.Columns.AutoFit
    Sheets("Sheet2").Select
    Sheets("Sheet2").Name = "ZERO BALANCE"
    Sheets("Sheet1").Select
    Range("A2").Select
    Range(Selection, ActiveCell.SpecialCells(xlLastCell)).Select
    Application.CutCopyMode = False
    Selection.EntireRow.Delete
    
    ActiveSheet.UsedRange.AutoFilter Field:=4
    ActiveSheet.UsedRange.AutoFilter Field:=11
    ActiveSheet.UsedRange.AutoFilter Field:=13, Criteria1:="O"
    Range("A1").Select
    Selection.SpecialCells(xlCellTypeVisible).Select
    Selection.Copy
    Sheets.Add After:=ActiveSheet
    ActiveSheet.Paste
    Selection.Columns.AutoFit
    Sheets("Sheet3").Select
    Sheets("Sheet3").Name = "OP"
    Sheets("Sheet1").Select
    Range("A2").Select
    Range(Selection, ActiveCell.SpecialCells(xlLastCell)).Select
    Application.CutCopyMode = False
    Selection.EntireRow.Delete

End Sub
 

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.
This is the problem
Change these lines:


Code:
    'Selection.SpecialCells(xlCellTypeVisible).Select
    'Selection.Copy


By:
Code:
    ActiveSheet.AutoFilter.Range.Copy

Then continue your code, but I do not understand what to do, because I do not know the sheets, I understand that the filter starts on the "Sheet1" and the result you paste on a new sheet, but I do not understand what you do with the "Sheet2".

Try the change and tell me.
 
Upvote 0
I'm a bit speechless at how much faster that made the processing: from 2 minutes to five seconds.
The process isn't perfected yet, but this is an amazing improvement. The new sheets are generating just fine.
At this point, the problem seems to be that if there are no rows of data to delete from a prior move, the process delete's the top row (which throws off subsequent steps).

The following code is how things stand now (with your substitutions)

Code:
Sub lite360()


    ActiveSheet.UsedRange.AutoFilter Field:=11, Criteria1:="-"
    ActiveSheet.AutoFilter.Range.Copy
    Sheets.Add After:=ActiveSheet
    ActiveSheet.Paste
    Selection.Columns.AutoFit
    Sheets("Sheet2").Select
    Sheets("Sheet2").Name = "ZERO BALANCE"
    Sheets("Sheet1").Select
    Range("A2").Select
    Range(Selection, ActiveCell.SpecialCells(xlLastCell)).Select
    Application.CutCopyMode = False
    Selection.EntireRow.Delete
    
    ActiveSheet.UsedRange.AutoFilter Field:=4
    ActiveSheet.UsedRange.AutoFilter Field:=11
    ActiveSheet.UsedRange.AutoFilter Field:=13, Criteria1:="O"
    Range("A1").Select
    ActiveSheet.AutoFilter.Range.Copy
    Sheets.Add After:=ActiveSheet
    ActiveSheet.Paste
    Selection.Columns.AutoFit
    Sheets("Sheet3").Select
    Sheets("Sheet3").Name = "OP"
    Sheets("Sheet1").Select
    Range("A2").Select
    Range(Selection, ActiveCell.SpecialCells(xlLastCell)).Select   <--- The problem seems to be here
    Application.CutCopyMode = False
    Selection.EntireRow.Delete

The first set of instructions works fine, moving filtered items to the new tab, renaming the new sheet, and deleting them from the original sheet. However, for the second set of instructions, there is technically no data to move other than the first row (which heads all the sheets). The macro is able to move the top row, but when it goes back to the original sheet, it selects the top row to delete (thus breaking down the sequence). If there is a more efficient way to clear the visible rows from the data while maintaining the top row, I'm open to suggestions.

Thank you for your time.
 
Upvote 0
before copying, check if the last row with data is equal to 1
 
Upvote 0
Is there a way I can incorporate that into the macro? I presume this would be a case of:

If last row with data = 1, Then
Copy row 1 to new spreadsheet
Return to original spreadsheet
Else
Run the macro how I originally was operating
End If

I'd probably have to incorporate that If Else for each set of data to be moved, which is not an issue. But how would I go about getting the macro to check if the last row with data = 1?
 
Upvote 0
Try this

Code:
    ActiveSheet.UsedRange.AutoFilter Field:=11, Criteria1:="-"
    ActiveSheet.AutoFilter.Range.Copy
    lr = ActiveSheet.Range("A" & rows.count).end(xlup).row
    if lr = 1 then 

       msgbox "[COLOR=#333333]no data to move"
[/COLOR]
    else

      Sheets.Add After:=ActiveSheet
      ActiveSheet.Paste
      Selection.Columns.AutoFit
      Sheets("Sheet2").Select
      Sheets("Sheet2").Name = "ZERO BALANCE"
      Sheets("Sheet1").Select
      Range("A2").Select
      Range(Selection, ActiveCell.SpecialCells(xlLastCell)).Select
      Application.CutCopyMode = False
      Selection.EntireRow.Delete

    end if    


    ActiveSheet.UsedRange.AutoFilter Field:=4    ActiveSheet.UsedRange.AutoFilter Field:=11
    ActiveSheet.UsedRange.AutoFilter Field:=13, Criteria1:="O"
    Range("A1").Select
    ActiveSheet.AutoFilter.Range.Copy
    Sheets.Add After:=ActiveSheet
    ActiveSheet.Paste
    Selection.Columns.AutoFit
    Sheets("Sheet3").Select
    Sheets("Sheet3").Name = "OP"
    Sheets("Sheet1").Select
    Range("A2").Select
    Range(Selection, ActiveCell.SpecialCells(xlLastCell)).Select   <--- The problem seems to be here
    Application.CutCopyMode = False
    Selection.EntireRow.Delete
 
Upvote 0
Solution
Ok, now we're talking! I had to tinker with the else if instructions slightly to apply separate instructions to each case, but your instructions were spot on. Thank you so much for your patience and help!
 
Upvote 0
I'm glad to help you. Thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,214,982
Messages
6,122,581
Members
449,089
Latest member
Motoracer88

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