Home > Enterprise >  VBA Issue with GoTo in macro
VBA Issue with GoTo in macro

Time:02-24

I have a macro, which I would like after the last msgbox and after clicking "YES" to go next part of macro (open file), but when i clicked YES Macro stops working without error, as if I didn't know what to do next.


Dim data_wb2 As Workbook
Beep
CarryOn = MsgBox("Czy Chcesz uzupełnić plik CMR - LRJ_MINIMAL?", vbYesNo, "Czy kontynuować?")
If CarryOn = vbYes Then
Else 
GoTo Jump
MyFolder = "xxxxx"
MyFile = Dir(MyFolder & "\CMR - LRJ_MINIMAL_*.xlsx")
If MyFile <> "" Then
Set data_wb2 = Workbooks.Open(MyFolder & "\" & MyFile, UpdateLinks:=0)
Dim PvtKraje As PivotTable
Dim data_wb As Workbook
Jump:
Beep
CarryOn = MsgBox("Czy Chcesz uzupełnić plik xxxx_po krajach?", vbYesNo, "Czy kontynuować?")
If CarryOn = vbYes Then
Else
GoTo Jump_Sheet

MyFolderKRAJ = "xxxx"
MyFile = Dir(MyFolder & "\xxx_mm_yyyy_podział po krajach*.xlsx")
If MyFile <> "" Then
Set data_wb = Workbooks.Open(MyFolder & "\" & MyFile, UpdateLinks:=0)

CodePudding user response:

Your problem is not 100% clear for me but if you want to open the file after clicking 'yes' on the msgBox you do not need to work with jump functions. I would avoid jump functions in general because it leads to confusing code. You also need to end the If statement using End If. Please also avoid using the same bool name CarryOn two times in one If statement

Pls try the following code:

CarryOn1 = MsgBox("Czy Chcesz uzupełnić plik CMR - LRJ_MINIMAL?", vbYesNo, "Czy kontynuować?")
If CarryOn1 = vbYes Then
   Beep
   CarryOn2 = MsgBox("Czy Chcesz uzupełnić plik xxxx_po krajach?", vbYesNo, "Czy kontynuować?")
   If CarryOn2 = vbYes Then
      MyFolder = "xxxxx"
      MyFile = Dir(MyFolder & "\CMR - LRJ_MINIMAL_*.xlsx")
      If MyFile <> "" Then
         Set data_wb2 = Workbooks.Open(MyFolder & "\" & MyFile, UpdateLinks:=0)
      End If
   Else
      GoTo Jump_Sheet
   End If
Else
   GoTo Jump_Sheet
End If
     
  • Related