简体   繁体   中英

Excel VBA Macro to run on a hidden Sheet

I have pivot table connected to an outside data set. I'm trying to have the sheet/tab name dynamically update based off of the max date value in the date range of the pivot. I was able to get this to work, but it changes if the dates are filtered within the pivot. I put the same data/pivot on another tab that just displays the max date value and displays it in a cell "C1"

=CONCATENATE("AS OF"," 
",TEXT(GETPIVOTDATA("CALL_DATE",MAX_TABLE!A1),"MM.DD.YYYY"))

This labels Sheet1 as "AS OF 08.15.2018"

My issue is that i do not want the max_table tab to be visible so i am hiding it. As a result the macro is not running until i unhide the sheet. Once it is unhidden it updates. I attempted to unhide, run the macro and then hide again but it is not working as intended. with the below it is not running on its own, but rather runs once i unhide the sheet and then hides the sheet after.

Private Sub Worksheet_SelectionChange(ByVal Target As Excel.Range)
Application.ScreenUpdating = False
Sheets("MAX_TABLE").Visible = True
Sheets("MAX_TABLE").PivotTables("MAX").PivotCache.Refresh
Set Target = Sheets("MAX_TABLE").Range("C1")
If Target = "" Then Exit Sub
Sheet1.Name = Left(Target, 31)
Sheets("MAX_TABLE").Visible = False
Application.ScreenUpdating = True
Exit Sub
End Sub

my issue/question wasnt really the problem. i had the wrong sub selected. It was connected to worksheet_selectionchange, so it would never run because i would always have to click on the hidden sheet to get it to trigger. i used sub worksheet_change which isnt necessarily the best one for this purpose, but it works because the hidden sheet only updates one time which causes the macro to run as intended.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM