Vba 在调试模式下运行宏可以正常工作,但在我单击按钮运行时不能正常工作

Vba 在调试模式下运行宏可以正常工作,但在我单击按钮运行时不能正常工作,vba,excel,Vba,Excel,我有以下代码,基本上是从文件夹中的一些文件复制数据库并粘贴到我的工作簿中 它应该在开始之前清理所有东西,当我从控制台运行时,点击F8并查看它时,它会清理所有东西,但是当我单击我分配给宏的按钮时,它不会在得到新的基础之前清理旧的基础,然后我会得到旧的数据,然后是它下面的新数据 你知道是什么引起的吗 谢谢大家! Sub Atualizar_B_Un_Time() Application.ScreenUpdating = False 'speed up macro execution

我有以下代码,基本上是从文件夹中的一些文件复制数据库并粘贴到我的工作簿中

它应该在开始之前清理所有东西,当我从控制台运行时,点击F8并查看它时,它会清理所有东西,但是当我单击我分配给宏的按钮时,它不会在得到新的基础之前清理旧的基础,然后我会得到旧的数据,然后是它下面的新数据

你知道是什么引起的吗

谢谢大家!

Sub Atualizar_B_Un_Time()

    Application.ScreenUpdating = False  'speed up macro execution
    Application.EnableEvents = False    'turn off other macros for now
    Application.DisplayAlerts = False   'turn off system messages for now


    Dim base_5 As Workbook
    Dim plan_5 As Worksheet
    Dim aux As String
    Dim caminho As String
    Dim nome_arquivo_5 As String
    Dim destino_5 As Worksheet
    Dim dia As String

    Set destino_5 = ThisWorkbook.Worksheets("B_Un_Time")
    caminho = Application.ActiveWorkbook.Path
    nome_arquivo_5 = Dir(caminho & "\IC_Reports_AgentUnavailableTime*.xlsx")


    destino_5.Range("H2:L" & Cells(Rows.Count, "I").End(xlUp).Row).UnMerge
    destino_5.Range("F2:F" & Cells(Rows.Count, "F").End(xlUp).Row).ClearContents
    destino_5.Range("H2:L" & Cells(Rows.Count, "I").End(xlUp).Row).ClearContents


    Do While nome_arquivo_5 <> ""
        aux = caminho & "\" & nome_arquivo_5
        Set base_5 = Workbooks.Open(aux, Local:=True)
        Set plan_5 = base_5.Sheets(1)


        dia = Mid(nome_arquivo_5, InStr(nome_arquivo_5, "-") + 1, 2)


        plan_5.Range("A2:E" & plan_5.Cells(Rows.Count, "B").End(xlUp).Row).Copy _
            Destination:=destino_5.Range("H" & (destino_5.Cells(Rows.Count, "I").End(xlUp).Row + 1))


        destino_5.Range("F" & (destino_5.Cells(Rows.Count, "F").End(xlUp).Row + 1) & ":" & "F" & _
            (destino_5.Cells(Rows.Count, "I").End(xlUp).Row)).Value = Format(Now, "mm/") & dia & Format(Now, "/yyyy")


        base_5.Close savechanges:=False


        nome_arquivo_5 = Dir
    Loop


    If IsEmpty(destino_5.Range("A" & destino_5.Cells(Rows.Count, "I").End(xlUp).Row)) Then
    destino_5.Range("A2:E2").Copy Destination:=destino_5.Range("A" & (destino_5.Cells(Rows.Count, "A").End(xlUp).Row + 1) _
        & ":" & "E" & destino_5.Cells(Rows.Count, "I").End(xlUp).Row)
    destino_5.Range("G2").Copy Destination:=destino_5.Range("G" & (destino_5.Cells(Rows.Count, "G").End(xlUp).Row + 1) & ":" & _
        "G" & destino_5.Cells(Rows.Count, "I").End(xlUp).Row)


    ElseIf Not IsEmpty(destino_5.Range("A" & (destino_5.Cells(Rows.Count, "I").End(xlUp).Row + 1))) Then
    destino_5.Rows((destino_5.Cells(Rows.Count, "I").End(xlUp).Row + 1) & ":" & destino_5.Cells(Rows.Count, "A") _
        .End(xlUp).Row).EntireRow.Delete
    End If


    destino_5.Cells.Font.Name = "Calibri"
    destino_5.Cells.Font.Size = 8
    destino_5.Rows.RowHeight = 11.25

    Application.DisplayAlerts = True    'turn system alerts back on
    Application.EnableEvents = True     'turn other macros back on
    Application.ScreenUpdating = True   'refreshes the screen

End Sub
Sub-Atualizar_B_Un_时间()
Application.ScreenUpdate=False“加快宏执行速度”
Application.EnableEvents=False“暂时关闭其他宏”
Application.DisplayAlerts=False“暂时关闭系统消息”
Dim base_5作为工作簿
Dim平面图5作为工作表
作为字符串的Dim aux
Dim Camino作为字符串
Dim nome_arquivo_5作为字符串
第5号图纸作为工作表
直径如细绳
Set destino_5=此工作簿。工作表(“B_Un_Time”)
caminho=Application.ActiveWorkbook.Path
nome_arquivo_5=Dir(caminho&“\IC_报告\u AgentUnavailableTime*.xlsx”)
destino_5.范围(“H2:L”和单元格(Rows.Count,“I”).结束(xlUp.Row).取消合并
destino_5.范围(“F2:F”和单元格(Rows.Count,“F”).结束(xlUp.Row).ClearContents
destino_5.范围(“H2:L”和单元格(Rows.Count,“I”).结束(xlUp.Row).ClearContents
当诺姆·阿奎沃5“的时候
aux=Camino&“\”和nome\u arquivo\u 5
Set base_5=工作簿。打开(辅助,本地:=真)
设置平面图5=基础图5。图纸(1)
直径=中(仪表5号仪表(仪表5号仪表,“-”)+1,2)
平面图5.范围(“A2:E”和平面图5.单元格(Rows.Count,“B”)。结束(xlUp.Row)。复制_
目的地:=destino_5.范围(“H”和(destino_5.单元格(Rows.Count,“I”).End(xlUp).Row+1))
destino_5.范围(“F”和(destino_5.单元格(Rows.Count,“F”)结束(xlUp.Row+1)和“:”和“F”和_
(destino_5.单元格(Rows.Count,“I”).End(xlUp.Row)).Value=格式(现在是“mm/”)&直径和格式(现在是“/yyyy”)
base_5.Close savechanges:=False
nome_arquivo_5=Dir
环
如果为空(destino_5.Range(“A”和destino_5.Cells(Rows.Count,“I”).End(xlUp.Row)),则
destino_5.范围(“A2:E2”)。复制目的地:=destino_5.范围(“A”)和(destino_5.单元格(Rows.Count,“A”)。结束(xlUp)。行+1)_
&“:”&“E”&destino_5.单元格(Rows.Count,“I”).End(xlUp.Row)
destino_5.Range(“G2”).复制目标:=destino_5.Range(“G”)和(destino_5.Cells(Rows.Count,“G”).End(xlUp).Row+1)和“&”_
“G”和destino_5.单元格(Rows.Count,“I”).End(xlUp.Row)
否则,如果不是空的(destino_5.范围(“A”&(destino_5.单元格(Rows.Count,“I”).End(xlUp.Row+1)),则
destino_5.行((destino_5.单元格(Rows.Count,“I”).End(xlUp.Row+1)&“&destino_5.单元格(Rows.Count,“A”)_
.End(xlUp).Row).EntireRow.Delete
如果结束
destino_5.Cells.Font.Name=“Calibri”
destino_5.Cells.Font.Size=8
destino_5.Rows.RowHeight=11.25
Application.DisplayAlerts=True“重新打开系统警报”
Application.EnableEvents=True“重新打开其他宏”
Application.ScreenUpdate=True'刷新屏幕
端接头

可能是因为您没有在任何地方添加工作表引用。并因此引用活动图纸。尝试这样修改该部分(注意点):


不起作用。使用With块引用或直接引用到行中都给了我同样的问题=/ooooops,没有看到单元格前的点,甚至你告诉我要注意点。。非常感谢<代码>目标5.范围(“H2:L”和单元格(Rows.Count,“I”)。结束(xlUp).Row)。如果不是
单元格,则取消合并
目标5
活动工作表(rows.count
可能是指activesheet@Davesexcel哇,就是这样!!我怎么可能看不见?!非常感谢。事实上,你刚才跟我说了和SJR一样的事情,但我以前没有看到。谢谢你们两个!可能是重复的
With destino_5
    .Range("H2:L" & .Cells(.Rows.Count, "I").End(xlUp).Row).UnMerge
    .Range("F2:F" & .Cells(.Rows.Count, "F").End(xlUp).Row).ClearContents
    .Range("H2:L" & .Cells(.Rows.Count, "I").End(xlUp).Row).ClearContents
End With