Macro errors out when run automatically, but no problem when step-by-step

Yamezz

Active Member
Joined
Nov 22, 2006
Messages
364
Office Version
  1. 2019
I have a large macro that begins by copying a range to a Temp sheet and removing the duplicates to begin processing the data.
If I run the macro with an assigned button (or F5) it errors out due to encountering blank cells in the Temp sheets after the remove duplicates operation. Looking in the Temp sheet after the code stops, I see that there are 10 blank lines (or cells - I can't tell due to only column A having data at this stage) inserted about 3/4 of the way down my list.
If I put a break point on the remove duplicates line, the code stops and shows me the list that was copied to the clipboard, which correctly contains no blanks at all, and the pasted list on the Temp sheet also contains no blanks at all. If I press F8 on the remove duplicates line, the list is condensed just as it should be, with no blanks.

To summarise: the following line ends up with blanks in the list if pressing F5, but correctly has no blanks if pressing F8
VBA Code:
Xero.Sheets("Temp").Range("$A$1:A" & Cells(Rows.Count, "A").End(xlUp).Row).RemoveDuplicates Columns:=1, Header:=xlNo

Attached is a screenshot of the input and outputs.
I have been using this macro for months and never encountered this problem, but now it occurs on both my Win10/Excel 2019 machine and my Win11/365 machine.

What is going wrong?
 

Attachments

  • Screenshot 2024-08-13 202401.png
    Screenshot 2024-08-13 202401.png
    46.8 KB · Views: 20

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).
Does it make any difference if you do this:
(this explicitly qualifies the .Cells(Rows.Count,"A") )

VBA Code:
With Xero.Sheets("Temp")
    .Range("$A$1:A" & .Cells(Rows.Count, "A").End(xlUp).Row).RemoveDuplicates Columns:=1, Header:=xlNo
End With
 
Upvote 0
Solution

Forum statistics

Threads
1,221,310
Messages
6,159,176
Members
451,543
Latest member
cesymcox

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