Sheets are not visible after the macro runs like it should be.

jondavis1987

Active Member
Joined
Dec 31, 2015
Messages
443
Office Version
  1. 2019
Platform
  1. Windows
I have a macro where I hide sheets "Core Report" and "Joints" pretty early. This is the end of the macro. After exporting it as a PDF i would like the sheets to be visible again. When I run this they stay hidden. I've also tried moving the sheets visible code before the End With


VBA Code:
   With srcWB
        fName = srcWB.Sheets("A").Range("A!F19").Value
        
        
           Sheets(Array("A", "Sheet2")).Select
    ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
            "C:\Users\jdavis\Dropbox\Quality Control\Asphalt\Asphalt Reports\" & fName, _
        openafterpublish:=True, ignoreprintareas:=False


    End With
   
    
    Sheets("Core Report").Visible = True
    Sheets("Joints").Visible = True
       End If
   
End Sub
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
If that's the case, it tells me that the procedure didn't encounter an error and it didn't get past your Exit Sub statement. So the ActiveWorkbook.ExportAsFixedFormat (exporting all sheets) that occurred before it gets executed and then it exits the sub. The ActiveSheet.ExportAsFixedFormat (exporting only selected sheets) that occurs after the Exit Sub statement never gets executed.

I would suggest that you step through your code by pressing the F8 key, and see which lines of code gets executed. You should find that if there are no errors that ActiveWorkbook.ExportAsFixedFormat gets executed and then it exits the sub. And so all sheets get exported. The ActiveWorksheet.ExportAsFixedFormat never gets executed.

So you may have to re-think your logic.
 
Upvote 0
if that didn't get executed then why does it still generate a PDF? I'll certainly give that a try, I'm certainly not that experienced in vba. I just don't understand how it could never get executed and still have a pdf generate.
 
Upvote 0
ActiveWorkbook.ExportAsFixedFormat that occurs before Exit Sub gets executed, that's why it generates a PDF. ActiveSheet.ExportAsFixedFormat that occurs after Exit Sub doesn't get executed unless your code encounters an error. The former exports all sheets, whereas the latter only the selected sheets. Try stepping through your code and you should see for yourself.
 
Upvote 0
Solution
I gotcha now. I completely forgot that was in the error code. I feel incredibly dumb now
 
Upvote 0

Forum statistics

Threads
1,215,064
Messages
6,122,937
Members
449,094
Latest member
teemeren

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