Usedrange adding range that is not used

Afro_Cookie

Board Regular
Joined
Mar 17, 2020
Messages
103
Office Version
  1. 365
Platform
  1. Windows
There's a portion of my code I use to cut and paste the used range from one sheet to another. Recently it's started adding extra lines into the used range which is affecting my table by adding blank lines. This is mildly inconvenient as I have to manually delete the lines, but it's not supposed to add them in the first place.

Any advice on how to correct or tweek my code/trouble shoot this?

This is just a portion of the code.
VBA Code:
' Remove Duplicates

    ActiveSheet.Range("A:I").RemoveDuplicates Columns:=Array(4, 6, 7), Header:=xlNo

'paste used range after ducplicates are removed

    ActiveSheet.UsedRange.Select
    
    Selection.Cut
    Sheets("abcd").Select
    Range("A" & Cells.Rows.Count).End(xlUp).Offset(1, 0).Select
    ActiveSheet.Paste

So right now that code should cut and paste 6 lines, but it is taking 10 instead and I have no idea why.
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
"UsedRange" is not all that reliable. If you delete rows, like your code is doing, it does NOT reset the UserRange.
There are other ways to find the last cell. See: Find last row, column or last cell

If there are no completely blank rows/columns in the middle of your data, you may be able to use CurrentRegion, i.e.
ActiveSheet.Range("A1").CurrentRegion.Select
Otherwise, use one of the other methods mentioned in the link.
 
Upvote 0
Switched the UsedRange to CurrentRegion. Worked perfectly. Thanks.

"UsedRange" is not all that reliable. If you delete rows, like your code is doing, it does NOT reset the UserRange.
There are other ways to find the last cell. See: Find last row, column or last cell

If there are no completely blank rows/columns in the middle of your data, you may be able to use CurrentRegion, i.e.
ActiveSheet.Range("A1").CurrentRegion.Select
Otherwise, use one of the other methods mentioned in the link.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,177
Members
453,021
Latest member
Justyna P

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