Macro stops after message box appear

Minx_Minxy

New Member
Joined
Jul 13, 2015
Messages
22
Office Version
  1. 365
Platform
  1. Windows
Hi I have the code below that is not working. I have two problems, it runs for the first row of my file then the message box pops up and then it stops and then if I run again, it restarts from the top and it replaces the numbers on column E with new numbers.

1) How do I get the macro to re-initiate after the message box pops up and the user manually fixes the error?
2) How do I have the macro not restarting from the top and replacing the numbers that it already has pasted in column E? i.e.: Continue from where it left off


VBA Code:
Sub Create()
Dim application
If Not IsObject(application) Then
   Set SapGuiAuto = GetObject("SAPGUI")
   Set application = SapGuiAuto.GetScriptingEngine
End If
If Not IsObject(Connection) Then
   Set Connection = application.Children(0)
End If
If Not IsObject(session) Then
   Set session = Connection.Children(0)
End If
If IsObject(WScript) Then
   WScript.ConnectObject session, "on"
   WScript.ConnectObject application, "on"
End If


Dim lastrow As Variant

Dim i As Long

lastrow = ActiveSheet.Cells(ActiveSheet.Rows.Count, "A").End(xlUp).Row

For i = 2 To lastrow

On Error GoTo message
session.findById("wnd[0]/tbar[0]/okcd").Text = "/niw21"
session.findById("wnd[0]").sendVKey 0
session.findById("wnd[0]/usr/ctxtRIWO00-QMART").Text = "m7"
session.findById("wnd[0]").sendVKey 0
session.findById("wnd[0]/usr/subSCREEN_1:SAPLIQS0:1050/subNOTIF_TYPE:SAPLIQS0:1052/txtVIQMEL-QMTXT").Text = Range("a" & i).Value 'Text value
session.findById("wnd[0]/usr/tabsTAB_GROUP_10/tabp10\TAB01/ssubSUB_GROUP_10:SAPLIQS0:7235/subCUSTOM_SCREEN:SAPLIQS0:7212/subSUBSCREEN_1:SAPLIQS0:7322/subOBJEKT:SAPLIWO1:0100/ctxtRIWO1-TPLNR").Text = Range("b" & i).Value 'FuncLoc
session.findById("wnd[0]/usr/tabsTAB_GROUP_10/tabp10\TAB01/ssubSUB_GROUP_10:SAPLIQS0:7235/subCUSTOM_SCREEN:SAPLIQS0:7212/subSUBSCREEN_1:SAPLIQS0:7322/subOBJEKT:SAPLIWO1:0100/ctxtRIWO1-EQUNR").Text = Range("c" & i).Value 'Equipment
session.findById("wnd[0]/usr/tabsTAB_GROUP_10/tabp10\TAB01/ssubSUB_GROUP_10:SAPLIQS0:7235/subCUSTOM_SCREEN:SAPLIQS0:7212/subSUBSCREEN_4:SAPLIQS0:7715/cntlTEXT/shellcont/shell").Text = Range("d" & i).Value 'Description
session.findById("wnd[0]/tbar[0]/btn[11]").press
session.findById("wnd[1]/tbar[0]/btn[0]").press
session.findById("wnd[0]/tbar[0]/okcd").Text = "/niw23"
session.findById("wnd[0]").sendVKey 0
Range("e" & i).Value = session.findById("wnd[0]/usr/ctxtRIWO00-QMNUM").Text 'Get Notification Number


message:
   MsgBox "There is an error, please check your SAP screen, revise the error and press OK"
    i = i + 1
   Exit Sub
   Resume Next
   
Next i

End Sub
 

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
Maybe this way:
VBA Code:
Sub Create()
    Dim application
    If Not IsObject(application) Then
       Set SapGuiAuto = GetObject("SAPGUI")
       Set application = SapGuiAuto.GetScriptingEngine
    End If
    If Not IsObject(Connection) Then
       Set Connection = application.Children(0)
    End If
    If Not IsObject(session) Then
       Set session = Connection.Children(0)
    End If
    If IsObject(WScript) Then
       WScript.ConnectObject session, "on"
       WScript.ConnectObject application, "on"
    End If
   
    Dim lastrow As Variant
    Dim i As Long
   
    lastrow = ActiveSheet.Cells(ActiveSheet.Rows.Count, "A").End(xlUp).Row
   
    On Error GoTo message
   
    For i = 2 To lastrow
        session.findById("wnd[0]/tbar[0]/okcd").Text = "/niw21"
        session.findById("wnd[0]").sendVKey 0
        session.findById("wnd[0]/usr/ctxtRIWO00-QMART").Text = "m7"
        session.findById("wnd[0]").sendVKey 0
        session.findById("wnd[0]/usr/subSCREEN_1:SAPLIQS0:1050/subNOTIF_TYPE:SAPLIQS0:1052/txtVIQMEL-QMTXT").Text = Range("a" & i).Value 'Text value
        session.findById("wnd[0]/usr/tabsTAB_GROUP_10/tabp10\TAB01/ssubSUB_GROUP_10:SAPLIQS0:7235/subCUSTOM_SCREEN:SAPLIQS0:7212/subSUBSCREEN_1:SAPLIQS0:7322/subOBJEKT:SAPLIWO1:0100/ctxtRIWO1-TPLNR").Text = Range("b" & i).Value 'FuncLoc
        session.findById("wnd[0]/usr/tabsTAB_GROUP_10/tabp10\TAB01/ssubSUB_GROUP_10:SAPLIQS0:7235/subCUSTOM_SCREEN:SAPLIQS0:7212/subSUBSCREEN_1:SAPLIQS0:7322/subOBJEKT:SAPLIWO1:0100/ctxtRIWO1-EQUNR").Text = Range("c" & i).Value 'Equipment
        session.findById("wnd[0]/usr/tabsTAB_GROUP_10/tabp10\TAB01/ssubSUB_GROUP_10:SAPLIQS0:7235/subCUSTOM_SCREEN:SAPLIQS0:7212/subSUBSCREEN_4:SAPLIQS0:7715/cntlTEXT/shellcont/shell").Text = Range("d" & i).Value 'Description
        session.findById("wnd[0]/tbar[0]/btn[11]").press
        session.findById("wnd[1]/tbar[0]/btn[0]").press
        session.findById("wnd[0]/tbar[0]/okcd").Text = "/niw23"
        session.findById("wnd[0]").sendVKey 0
        Range("e" & i).Value = session.findById("wnd[0]/usr/ctxtRIWO00-QMNUM").Text 'Get Notification Number
    Next i

Exit Sub
message:
    MsgBox "There is an error, please check your SAP screen, revise the error and press OK"
    Resume
'    i = i + 1
'   Exit Sub
'   Resume Next

End Sub
 
Upvote 1
Solution
Maybe this way:
VBA Code:
Sub Create()
    Dim application
    If Not IsObject(application) Then
       Set SapGuiAuto = GetObject("SAPGUI")
       Set application = SapGuiAuto.GetScriptingEngine
    End If
    If Not IsObject(Connection) Then
       Set Connection = application.Children(0)
    End If
    If Not IsObject(session) Then
       Set session = Connection.Children(0)
    End If
    If IsObject(WScript) Then
       WScript.ConnectObject session, "on"
       WScript.ConnectObject application, "on"
    End If
  
    Dim lastrow As Variant
    Dim i As Long
  
    lastrow = ActiveSheet.Cells(ActiveSheet.Rows.Count, "A").End(xlUp).Row
  
    On Error GoTo message
  
    For i = 2 To lastrow
        session.findById("wnd[0]/tbar[0]/okcd").Text = "/niw21"
        session.findById("wnd[0]").sendVKey 0
        session.findById("wnd[0]/usr/ctxtRIWO00-QMART").Text = "m7"
        session.findById("wnd[0]").sendVKey 0
        session.findById("wnd[0]/usr/subSCREEN_1:SAPLIQS0:1050/subNOTIF_TYPE:SAPLIQS0:1052/txtVIQMEL-QMTXT").Text = Range("a" & i).Value 'Text value
        session.findById("wnd[0]/usr/tabsTAB_GROUP_10/tabp10\TAB01/ssubSUB_GROUP_10:SAPLIQS0:7235/subCUSTOM_SCREEN:SAPLIQS0:7212/subSUBSCREEN_1:SAPLIQS0:7322/subOBJEKT:SAPLIWO1:0100/ctxtRIWO1-TPLNR").Text = Range("b" & i).Value 'FuncLoc
        session.findById("wnd[0]/usr/tabsTAB_GROUP_10/tabp10\TAB01/ssubSUB_GROUP_10:SAPLIQS0:7235/subCUSTOM_SCREEN:SAPLIQS0:7212/subSUBSCREEN_1:SAPLIQS0:7322/subOBJEKT:SAPLIWO1:0100/ctxtRIWO1-EQUNR").Text = Range("c" & i).Value 'Equipment
        session.findById("wnd[0]/usr/tabsTAB_GROUP_10/tabp10\TAB01/ssubSUB_GROUP_10:SAPLIQS0:7235/subCUSTOM_SCREEN:SAPLIQS0:7212/subSUBSCREEN_4:SAPLIQS0:7715/cntlTEXT/shellcont/shell").Text = Range("d" & i).Value 'Description
        session.findById("wnd[0]/tbar[0]/btn[11]").press
        session.findById("wnd[1]/tbar[0]/btn[0]").press
        session.findById("wnd[0]/tbar[0]/okcd").Text = "/niw23"
        session.findById("wnd[0]").sendVKey 0
        Range("e" & i).Value = session.findById("wnd[0]/usr/ctxtRIWO00-QMNUM").Text 'Get Notification Number
    Next i

Exit Sub
message:
    MsgBox "There is an error, please check your SAP screen, revise the error and press OK"
    Resume
'    i = i + 1
'   Exit Sub
'   Resume Next

End Sub

Oh my! Was it that easy??:eek: Thank you so so so so so so so mych ;)
 
Upvote 0

Forum statistics

Threads
1,220,965
Messages
6,157,119
Members
451,398
Latest member
rjsteward

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top