Excel VBA – 检查macros使用的任何单元是否已经更新,如果重新运行macros。 维护格式

我有一个Excel工作簿,其中包含工作表中的销售线索列表,由字母分隔(工作表“A”包含以“A”开头的所有线索)。 我在一个单独的工作表上运行下面的macros将所有这些表合并到一个主表(代码在这里find)。

目标:当我改变工作表中的一个单元格(比如工作表A)时,我想要在运行我的macros的主表单上进行更新。 我想我只需要重新运行macros,只要这些单元格中的一个改变了。 有没有办法自动检测macros中的任何数据的变化,然后触发它重新运行?

我已经find了解决办法,如何对一系列单元格或特定单元格执行此操作,而不是在一系列工作表单元格中执行此操作。

Sub CopyRangeFromMultiWorksheets() Dim sh As Worksheet Dim DestSh As Worksheet Dim Last As Long Dim CopyRng As Range With Application .ScreenUpdating = False .EnableEvents = False End With 'Delete the sheet "RDBMergeSheet" if it exist Application.DisplayAlerts = False On Error Resume Next ActiveWorkbook.Worksheets("RDBMergeSheet").Delete On Error GoTo 0 Application.DisplayAlerts = True 'Add a worksheet with the name "RDBMergeSheet" Set DestSh = ActiveWorkbook.Worksheets.Add DestSh.Name = "RDBMergeSheet" 'loop through all worksheets and copy the data to the DestSh For Each sh In ActiveWorkbook.Worksheets If sh.Name <> DestSh.Name Then 'Find the last row with data on the DestSh Last = LastRow(DestSh) 'Fill in the range that you want to copy Set CopyRng = sh.UsedRange 'Test if there enough rows in the DestSh to copy all the data If Last + CopyRng.Rows.Count > DestSh.Rows.Count Then MsgBox "There are not enough rows in the Destsh" GoTo ExitTheSub End If 'This example copies values/formats, if you only want to copy the 'values or want to copy everything look at the example below this macro CopyRng.Copy With DestSh.Cells(Last + 1, "A") .PasteSpecial xlPasteValues .PasteSpecial xlPasteFormats Application.CutCopyMode = False End With 'Optional: This will copy the sheet name in the H column DestSh.Cells(Last + 1, "M").Resize(CopyRng.Rows.Count).Value = sh.Name End If Next ExitTheSub: Application.Goto DestSh.Cells(1) 'AutoFit the column width in the DestSh sheet DestSh.Columns.AutoFit With Application .ScreenUpdating = True .EnableEvents = True End With End Sub 

如果保持格式化将会很好。 每次重新运行macros时,单元格都会resize。