【问题标题】:"Call" macro does not execute but will manually\"Call\" 宏不执行,但会手动执行
【发布时间】:2022-11-03 02:50:26
【问题描述】:

有谁知道为什么我的“调用”其他宏的宏没有运行最后一个调用的宏设置详情?手动/单独推送时成功运行。

Sub MainMacro()

  Application.ScreenUpdating = False
   Call InjectAllSqlsAndRefreshConnections
   Call SetupDashboard
   Call SetupDetails
   Application.ScreenUpdating = True

End Sub

这个 SetupDetails 宏本身的主体有问题吗?我已经摆弄了很多,似乎没有什么可以解决这个问题。

Sub SetupDetails()

Set Details = ThisWorkbook.Sheets("Details")
Set Raw = ThisWorkbook.Sheets("SQL - Bugs w Goals")

Dim x As Integer
Dim CorrectOrder As Variant
Dim i As Variant
Dim tblComp As ListObject
Dim LastRow As Integer

Details.Activate
Details.UsedRange.Clear
If Raw.AutoFilterMode Then Raw.ShowAllData
Raw.UsedRange.Copy
Details.Range("A1").PasteSpecial Paste:=xlPasteValuesAndNumberFormats

With Details.UsedRange
        'Dedupe
        .AutoFilter Field:=23, Criteria1:="0"
        .Offset(1).SpecialCells(xlCellTypeVisible).EntireRow.Delete
        .AutoFilter

        'Delete unneeded columns
        .Range("D:E,G:G, I:K, M:N, O:P,R:U, W:Z, AD:AD, AF:AG,AK:AK").Delete

        'Rename Columns
        .Range("A1").Value = "ID"
        .Range("B1").Value = "Summary"
        .Range("C1").Value = "Status"
        .Range("E1").Value = "Class"
        .Range("H1").Value = "Goals"
        .Range("I1").Value = "Progress Status"
        .Range("J1").Value = "Open/Closed Status"
        .Range("K1").Value = "Blocked Status"
        .Range("M1").Value = "Remaining Time"
        .Range("N1").Value = "Total Time"
        .Range("O1").Value = "Dept"

        Application.CutCopyMode = False
        Details.ListObjects.Add(xlSrcRange, Details.UsedRange, xlYes).Name = "DetailsView"
        Details.ListObjects("DetailsView").TableStyle = "TableStyleLight9"

        'Reorder
        Set tblComp = Details.ListObjects("DetailsView")
        CorrectOrder = Array("Goals", "Dept", "Team", "ID", "Status", "Class", "Summary", "Due Date", "Deadline Stage (Milestone)", "Actual Time", "Remaining Time", "Total Time", "Progress Status", "Open/Closed Status", "Blocked Status")
        On Error Resume Next
        For Each i In CorrectOrder
            Columns(tblComp.ListColumns(i).Range.Column).Cut
            Columns(tblComp.ListColumns.Count + 1).Insert Shift:=xlToRight
        Next i
        On Error GoTo 0


End With

'Formatting
With Details
            .Columns(1).ColumnWidth = 60
            .Columns(1).WrapText = True
            .Columns(7).ColumnWidth = 60
            .Columns(7).WrapText = True
            For x = 1 To .Columns.Count
                Columns(x).EntireColumn.AutoFit
            Next x
            Cells.Select
            Selection.Columns.AutoFit
End With

'Links
LastRow = Details.Cells(Details.Rows.Count, "D").End(xlUp).Row
With Details.UsedRange
                        For x = 2 To LastRow
                            Cells(x, "D").Activate
                            .Hyperlinks.Add Anchor:=ActiveCell, Address:="URL HERE" & ActiveCell.Text, TextToDisplay:=ActiveCell.Text
                            Next x
    End With
    
    Sheets("Report").Activate
    
    End Sub

【问题讨论】:

  • 我想这在外面是不可能说的。您是否使用 F8 或断点调试过代码?潜艇不是全部执行(听起来不太可能)吗?上一个例程是否成功返回或者可能已经停止代码?
  • 可能发生的因素有很多,例如,将项目放在不同的模块中或 private 有一些东西。我的建议是使用Application.Run "Module#.MacroName" 而不是Calllearn.microsoft.com/en-us/office/vba/api/excel.application.run
  • 假设宏在同一个工作簿中,则无需在此处使用 Application.Run。甚至Call 这些天也被弃用了:你可以只使用SetupDetails 而没有Call
  • 你能描述一下“不运行”和“这个问题”吗?
  • 我的第一个想法是,如果前两个宏中的任何一个正在使用异步调用,那么在调用第三个宏时可能还没有更新“详细信息”。它可能就像在 then 函数之间添加延迟或检查以确保它们已完成一样简单。

标签: excel vba


【解决方案1】:

我能够弄清楚这一点!原来是宏InjectAllSqlsAndRefreshConnections,这听起来就像是在继续之前没有完成数据刷新。因此,之后任何依赖于刷新数据的宏看起来都没有变化,因为它仍在使用未刷新的数据。

通过在数据查询连接属性中关闭启用后台刷新解决了这个问题。这样,它会强制查询在继续处理之前完成。

Image

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-02-07
    • 2017-08-26
    • 2011-11-09
    • 1970-01-01
    • 1970-01-01
    • 2013-10-18
    • 2023-03-22
    相关资源
    最近更新 更多