Usedrange adding range that is not used

Afro_Cookie

New Member
Joined
Mar 17, 2020
Messages
40
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

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.

Joe4

MrExcel MVP, Junior Admin
Joined
Aug 1, 2002
Messages
57,510
Office Version
  1. 365
Platform
  1. Windows
"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.
 

Afro_Cookie

New Member
Joined
Mar 17, 2020
Messages
40
Office Version
  1. 365
Platform
  1. Windows
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.
 

Joe4

MrExcel MVP, Junior Admin
Joined
Aug 1, 2002
Messages
57,510
Office Version
  1. 365
Platform
  1. Windows
You are welcome.
 

Watch MrExcel Video

Forum statistics

Threads
1,132,685
Messages
5,654,748
Members
418,149
Latest member
amamiche67

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
Top