运行此宏(使用AutomationWhere)时,VBA几个小时不响应

发布于 2025-01-31 13:29:57 字数 3057 浏览 3 评论 0原文

我正在使用SAP下载的一些下载报告。

我需要通过一列过滤此报告,然后将此过滤后的报告复制到我的另一个文件 工作共享驱动器。

有时,当我尝试在我的生产服务器上运行这个宏观波纹管时,Excel会冻结数小时(4小时或更长时间),然后我需要停止所有内容并重新开始。

在运行此宏之前,您知道我该怎么做才能“清洁” Excel?也许可以清除缓存或代码中的文章?


Sub PasteReportZFIInflowCheck()

' This macro will open the downloaded ZFI report from SAP, filter to only InflowCheck and copy to the final report at Sharedrive


    ' Disable Alerts, ScreenUpdating
    Application.DisplayAlerts = False
    Application.ScreenUpdating = False
    

    ' Set Variables
    Try = 0
    Set ReportPath = Range("A1")
    Set Report = Range("B1")
    Set ResultPath = Range("A2")
    Set Result = Range("B2")
    Set PasteCell = Range("A3")
    Set SheetName = Range("C2")
    Dim wb As Workbook
    
    
' Main Task
MainTask:
    
    Do While Try < 3
    On Error GoTo ErrorHandler
    
    ' SAP Report Download (Filter and Copy)
    Workbooks.Open Filename:=ReportPath
    
    'Replace . to ,
    Columns("AH:BB").Select
    Selection.Replace What:=".", Replacement:=",", LookAt:=xlPart, _
        SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
        ReplaceFormat:=False
        
    'Filter
    Range("A1").Select
    Selection.AutoFilter
    ActiveSheet.Range("$A$1:$BC$999999").AutoFilter Field:=1, Criteria1:="1"
         
    'Check if we have any Data
    i = 1
    j = 1
    Do While Sheets("Sheet1").Cells(i, j) <> Empty
    i = i + 1
    Cells(i, j).Select
    Value = ActiveCell
        If Value = "1" Then
        CheckEmpty = 1
        End If
    Loop
    
    'Have data
    If CheckEmpty = 1 Then
    
    ' Copy all data
    Range("A1").Select
    Selection.Offset(1, 0).Select ' seta para baixo
    Range(Selection, Selection.End(xlDown)).Select
    Range(Selection, Selection.End(xlToRight)).Select
    Selection.Copy
        
    
    ' Open Result sheet
    Workbooks.Open Filename:=ResultPath
    ActiveWorkbook.Worksheets("Check 01 a 31_SAP").Activate
       
    
    'Look for last empty line
    Range(PasteCell).Select
    i = 2
    j = ActiveCell.Column
    Do While Sheets("Check 01 a 31_SAP").Cells(i, j) <> Empty
    i = i + 1
    Loop
    Cells(i, j).Select

    'Paste the report
    ActiveSheet.Paste
    
    End If
    

    'Save and close Result workbook
    ActiveWorkbook.Close SaveChanges:=True
    

    'Close others workbooks
    ActiveWorkbook.Worksheets("Sheet1").Activate
    ActiveWorkbook.Close SaveChanges:=False
    
    'Cancel Loop
    Try = 3
    Loop
    
    ' Clear Copied data
    Application.CutCopyMode = False
    Application.CutCopyMode = True
        

    ' Enable ScreenUpdating
     Application.ScreenUpdating = True
    
    
    Application.Quit
    
    Exit Sub
    
    
ErrorHandler:
    On Error GoTo -1
    Try = Try + 1
    
    ' close all workbooks
    ActiveWorkbook.Worksheets("Check 01 a 31_SAP").Activate
    ActiveWorkbook.Close SaveChanges:=False
    ActiveWorkbook.Worksheets("Sheet1").Activate
    ActiveWorkbook.Close SaveChanges:=False
    
    GoTo MainTask
    
End Sub

I'm working with some downloaded reports from SAP.

I need to filter this reports by one column and copy this filtered report to another file in my
work share drive.

Sometimes when I tried to run this macro bellow at my production server, the excel gets freeze for hours (4 hours or more), then I need to stop everything and start it again.

Do you know what can I do to "clean" the excel before run this macro? Maybe something to clear cache, or somenthing in the code?


Sub PasteReportZFIInflowCheck()

' This macro will open the downloaded ZFI report from SAP, filter to only InflowCheck and copy to the final report at Sharedrive


    ' Disable Alerts, ScreenUpdating
    Application.DisplayAlerts = False
    Application.ScreenUpdating = False
    

    ' Set Variables
    Try = 0
    Set ReportPath = Range("A1")
    Set Report = Range("B1")
    Set ResultPath = Range("A2")
    Set Result = Range("B2")
    Set PasteCell = Range("A3")
    Set SheetName = Range("C2")
    Dim wb As Workbook
    
    
' Main Task
MainTask:
    
    Do While Try < 3
    On Error GoTo ErrorHandler
    
    ' SAP Report Download (Filter and Copy)
    Workbooks.Open Filename:=ReportPath
    
    'Replace . to ,
    Columns("AH:BB").Select
    Selection.Replace What:=".", Replacement:=",", LookAt:=xlPart, _
        SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
        ReplaceFormat:=False
        
    'Filter
    Range("A1").Select
    Selection.AutoFilter
    ActiveSheet.Range("$A$1:$BC$999999").AutoFilter Field:=1, Criteria1:="1"
         
    'Check if we have any Data
    i = 1
    j = 1
    Do While Sheets("Sheet1").Cells(i, j) <> Empty
    i = i + 1
    Cells(i, j).Select
    Value = ActiveCell
        If Value = "1" Then
        CheckEmpty = 1
        End If
    Loop
    
    'Have data
    If CheckEmpty = 1 Then
    
    ' Copy all data
    Range("A1").Select
    Selection.Offset(1, 0).Select ' seta para baixo
    Range(Selection, Selection.End(xlDown)).Select
    Range(Selection, Selection.End(xlToRight)).Select
    Selection.Copy
        
    
    ' Open Result sheet
    Workbooks.Open Filename:=ResultPath
    ActiveWorkbook.Worksheets("Check 01 a 31_SAP").Activate
       
    
    'Look for last empty line
    Range(PasteCell).Select
    i = 2
    j = ActiveCell.Column
    Do While Sheets("Check 01 a 31_SAP").Cells(i, j) <> Empty
    i = i + 1
    Loop
    Cells(i, j).Select

    'Paste the report
    ActiveSheet.Paste
    
    End If
    

    'Save and close Result workbook
    ActiveWorkbook.Close SaveChanges:=True
    

    'Close others workbooks
    ActiveWorkbook.Worksheets("Sheet1").Activate
    ActiveWorkbook.Close SaveChanges:=False
    
    'Cancel Loop
    Try = 3
    Loop
    
    ' Clear Copied data
    Application.CutCopyMode = False
    Application.CutCopyMode = True
        

    ' Enable ScreenUpdating
     Application.ScreenUpdating = True
    
    
    Application.Quit
    
    Exit Sub
    
    
ErrorHandler:
    On Error GoTo -1
    Try = Try + 1
    
    ' close all workbooks
    ActiveWorkbook.Worksheets("Check 01 a 31_SAP").Activate
    ActiveWorkbook.Close SaveChanges:=False
    ActiveWorkbook.Worksheets("Sheet1").Activate
    ActiveWorkbook.Close SaveChanges:=False
    
    GoTo MainTask
    
End Sub

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文