简体   繁体   English

在隐藏的工作表上运行的 Excel VBA 宏

[英]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"我将相同的数据/枢轴放在另一个选项卡上,该选项卡仅显示最大日期值并将其显示在单元格“C1”中

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

This labels Sheet1 as "AS OF 08.15.2018"这将 Sheet1 标记为“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.我的问题是我不希望 max_table 选项卡可见,因此我将其隐藏。 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.它连接到 worksheet_selectionchange,所以它永远不会运行,因为我总是必须点击隐藏的工作表才能触发它。 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.我使用了 sub worksheet_change ,它不一定是最好的,但它的工作原理是隐藏的工作表只更新一次,这会导致宏按预期运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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