userform将公共变量传递给另一个宏 - 无限循环

时间:2018-01-24 15:33:49

标签: vba public doevents

以下代码旨在:

1)按下命令按钮CmdFile_Click

,沿着文件夹.xlsb文件滚动一段时间。

2)显示所需文件后,点击CmdSI_ClickCmdNo_Click

选择或放弃该文件

3)选择文件后,按CmdVai_Click

运行宏....(做某事)..

在第1步到第2步之间,该流程可以使用公共变量MACROSTOPDoEvents

问题在所有工作完成后上升,需要选择另一个文件才能工作。

我无法选择它因为vba遇到无限循环而我必须按 ctrl + break , 按 ctrl + alt + del 停止宏或最差打开任务管理器向导 我认为这些问题位于箭头代码行中 感谢您的宝贵帮助和可用性

Public MACROSTOP As Boolean, Nomefile As String, Nomefolder As String '<----

Private Sub CmdFile_Click()
'Dim Nomefile As String, Nomefolder As String
If TxtMacro.Value = "" And txtMese.Value = "" Then
MsgBox ("INSERIRE MACRO E MESE")
MsgBox ("PER MACRO SITUAZIONE (3) E FIFO (4), IL MESE è 0")
Exit Sub
End If

Nomefolder = ActiveWorkbook.Path & "\"
Nomefile = Dir(Nomefolder & "\*.xlsb")

While Nomefile <> ""
Label1.Caption = Nomefile
MACROSTOP = True  '<-----------------
Do While MACROSTOP '<-----------------
    DoEvents '<-----------------
Loop  '<-----------------

Nomefile = Dir
Wend

UserForm1.Hide
'Exit Sub

End Sub

Private Sub CmdNo_Click()
MACROSTOP = False
End Sub
Private Sub CmdSI_Click()
CmdSI.Caption = "V"
'Call CmdVai(Nomefile, Nomefolder)
End Sub

Private Sub CmdVai_Click()
MACROSTOP = False

Dim Fileaperto As Workbook
'Nomefolder = ActiveWorkbook.Path & "\"
'Nomefile = Dir(Nomefolder & "\*.xlsb")

A = TxtMacro.Value
B = txtMese.Value

Set Fileaperto = Workbooks.Open(Nomefolder & Nomefile)

'-----------------------MACRO 1= SOMMA AGGREGATO MENSILE; 2= SITUAZIONE; 3= FIFO

If A > 3 Or A < 1 Then
    MsgBox ("VALORE SCELTA MACRO ERRATA")
    Exit Sub
ElseIf A = 1 Then  'prima MACRO
' ------------------------MESE: NUMERO DA 1 A 12
    If B = "" Then
        MsgBox ("INSERIRE MESE")
        Exit Sub
    ElseIf B < 1 Or B > 12 Then
        MsgBox ("VALORE MESE ERRATO")
        Exit Sub
    Else: B = B + 1
            If B = 2 Then
                Worksheets(B).Select
                Application.run "'" & Nomefile & "'!listaIdprodotto"
                Application.run "'" & Nomefile & "'!sommaSe"
                Application.DisplayAlerts = False
                ActiveWindow.Close savechanges:=True
            ElseIf B = 3 Then
                Worksheets(B).Select
                Application.run "'" & Nomefile & "'!listaIdprodottoFebb"
                Application.run "'" & Nomefile & "'!sommaSeFeb"
                Application.DisplayAlerts = False
                ActiveWindow.Close savechanges:=True
            ElseIf B = 4 Then
                Worksheets(B).Select
                Application.run "'" & Nomefile & "'!listaIdprodottoMarz"
                Application.run "'" & Nomefile & "'!sommaSeMarz"
                Application.DisplayAlerts = False
                ActiveWindow.Close savechanges:=True

    FROM B=5 TO 12 OMITTED TO SAVE SPACE                

            ElseIf B = 13 Then
                Worksheets(B).Select
                Application.run "'" & Nomefile & "'!listaIdprodottoDic"
                Application.run "'" & Nomefile & "'!sommaSeDic"
                Application.DisplayAlerts = False
                ActiveWindow.Close savechanges:=True
            End If
    End If
ElseIf A = 2 Then 'seconda macro
txtMese.Locked = True
Worksheets(14).Select
Application.run "'" & Nomefile & "'!EsportaAsituazione"
Application.run "'" & Nomefile & "'!REFRESH"
Application.DisplayAlerts = False
ActiveWindow.Close
ElseIf A = 3 Then  'terza macro
txtMese.Locked = True
Worksheets(15).Select
Application.run "'" & Nomefile & "'!FIFO"
Application.DisplayAlerts = False
ActiveWindow.Close
End If
MACROSTOP = False '<-----------------
Unload Me    '<------------------
'Exit Sub
End Sub

Private Sub UserForm_Click()

End Sub

0 个答案:

没有答案