Macro skipping entire blocks of code when run the first time

indyman

New Member
Joined
Apr 14, 2021
Messages
47
Office Version
  1. 2019
Platform
  1. Windows
I have an app that creates 3 reports: Income, Expense, and Budget. Each has 2 buttons (1 for line, 1 for columns) that calls macro1 with parameters. Each report is \displayed on separate sheets. The buttons are customUI ribbon buttons, but I don't think that matters because they all just call the macro1.
Macro1 (721 lines) creates a chart then calls Macro2 (436 lines) with several parameters to create a tale below the chart. There is a lot of shared logic, thus just using the 2 macros.

My issue is with macro2. The first time it runs for the Income Report, it misses an entire block of code. Subsequent runs are fine. Unless I run one of the other reports and then rerun the Income Report, which will then again miss the same code. The other reports run flawlessly.

VBA Code:
'wsName is passes as a parameter from macro1
'Header created
'Create rows for data subset 1 (4 rows)
'this is the part that gets missed on the first run
Case wsName = "Income Report" (this the only report that gets the additional 2 subsets)
    'add rows for data subset 2 (3 rows)
    'add rows for data subset 3 (2 rows)
Case wsName = "Expense Report" 
...
End Select
'create and format totals row

On the first run, I get the first data set only, with the totals row directly below summing what is in the 1st 4 rows. Clicking the same button again with no data changes gets the expected all 3 data subsets. However, if I put a breakpoint just about anywhere before the block that gets missed, and then F5 it, I get all the data I am expecting on the first run. If I duplicate the macro2 call at the end of macro1 for the Income Report, I get the proper results.

I have run VBA Code Cleaner 5.0 on it. I have built in delays. I was experiencing some unexplainable intermittent crashes with the ribbon as well. So I have also created a new WB copying all the sheets and code files to it, with no resolution. Although it seems as though the ribbon issues have gone away for now. Any ideas of what else I can try? I could certainly separate macro2 into 3 subs, but prefer the code proficiency if possible. I have been at this for hours and was hoping someone else had experienced something like this?
 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
Well if I could delete my post, I would. It was a bonehead mistake. I noticed one of the other reports did not add the word "Totals". Upon stepping through I saw that I had an unqualified range reference. I just had to had my sheet name variable to it. And sure enough that was the issue causing the above as I did not have a qualified range when I set my range variables to determine last line of the subsets. The 2nd time it was run , it was on the proper sheet and worked. Hopefully someone else can learn from my mistakes.
 
Upvote 0
Solution

Forum statistics

Threads
1,215,032
Messages
6,122,772
Members
449,095
Latest member
m_smith_solihull

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