简体   繁体   English

需要在excel中的数据刷新上运行VBA宏

[英]Need to run a VBA Macro on data refresh in excel

I am attempting to prompt a macro to run on a data refresh. 我试图提示宏运行数据刷新。 I have the macro that needs to be run build, but I am having an issue with the new values not being used since the macros embedded in the sheet are called using ActiveX ComboBoxs. 我有需要运行构建的宏,但是由于使用ActiveX ComboBoxs调用工作表中嵌入的宏,因此我没有使用新值的问题。

I am finding several instances where people refer to AfterRefresh and BeforeRefresh, but I think I am misunderstanding how this would take effect and call a macro. 我发现了几个人们引用AfterRefresh和BeforeRefresh的情况,但我认为我误解了这将如何生效并调用一个宏。

I currently am running ComboBoxs so I have multiple instances of 我目前正在运行ComboBoxs,所以我有多个实例

Private Sub ComboBox22_Change()
'do stuff
End Sub.

but I need the 'do stuff' to occur upon a data refresh, including refreshes that happen automatically and upon sheet open. 但我需要在数据刷新时发生'do stuff',包括自动发生并在打开表格时刷新。

I don't want to tie the refresh to a specific box because the items that are refreshed are not dependent on any one instance of data change. 我不想将刷新绑定到特定的框,因为刷新的项不依赖于任何一个数据更改实例。

Any help is greatly appreciated. 任何帮助是极大的赞赏。

Thank you. 谢谢。

Maybe a worksheet change event would help in this situation. 在这种情况下,工作表更改事件可能会有所帮助。

Right Click the sheet tab, select "View Code", Select "Worksheet" then "Change." 右键单击工作表选项卡,选择“查看代码”,选择“工作表”,然后选择“更改”。

Code will automatically kick in when a specific range of cells has been changed. 当特定范围的单元格被更改时,代码将自动启动。

右键单击工作表标签在此输入图像描述

Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Count > 1 Then Exit Sub    ' this stops code error if more than one cell is changed at once

    If Not Application.Intersect(Target, Me.Range("A1:C10")) Is Nothing Then    ' indicates the Target range
        MsgBox "You have changed " & Target.Address & " to " & Target
    End If

End Sub

You could also use Worksheet_pivottableupdate event to run the macro. 您还可以使用Worksheet_pivottableupdate事件来运行宏。 You set it up in a similar way to davesexcel answer above. 你设置它与上面的davesexcel答案类似。

The connection in question may not be a pivot table but you can use a small and fast pivot table as a trigger. 有问题的连接可能不是数据透视表,但您可以使用小而快速的数据透视表作为触发器。

Set the pivot table to update at the same time as your connection (eg set to self refresh every 5 minutes or on workbook open). 将数据透视表设置为在连接的同时更新(例如,设置为每5分钟自刷新一次或在工作簿打开时)。

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

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