Do something if error handling executed

vostroxe

New Member
Joined
Jul 13, 2018
Messages
29
Code:
Sub FullExecution2()

    Set Connection = GetObject("SAPGUI").GetScriptingEngine.Children(0)
    If Not IsObject(session) Then
   Set SAPsession = Connection.Children(0)
    End If
    With SAPsession


    .findById("wnd[0]").maximize


    Dim objExcel
    Dim objSheet, intRow, i
    Set objExcel = GetObject(, "Excel.Application")
    Set objSheet = objExcel.ActiveWorkbook.ActiveSheet
 
    COL1 = objSheet.Cells(2, 2)
    COL2 = objSheet.Cells(5, 2)
    COL3 = objSheet.Cells(6, 2)
    COL4 = objSheet.Cells(7, 2)
    
'Download SAP FBL5N
.findById("wnd[0]").maximize
.findById("wnd[0]/tbar[0]/okcd").Text = "COL4"
.findById("wnd[0]").sendVKey 0
.findById("wnd[0]/tbar[1]/btn[17]").press
.findById("wnd[1]/tbar[0]/btn[12]").press
.findById("wnd[0]/tbar[1]/btn[17]").press
.findById("wnd[1]/usr/txtV-LOW").Text = "sales rebate"
.findById("wnd[1]/usr/txtV-LOW").caretPosition = 12
.findById("wnd[1]/tbar[0]/btn[8]").press
.findById("wnd[0]/usr/ctxtDD_BUKRS-LOW").Text = COL1
.findById("wnd[0]/usr/ctxtSO_BUDAT-LOW").Text = COL2
.findById("wnd[0]/usr/ctxtSO_BUDAT-HIGH").Text = COL3
.findById("wnd[0]/usr/ctxtSO_BUDAT-HIGH").SetFocus
.findById("wnd[0]/usr/ctxtSO_BUDAT-HIGH").caretPosition = 10
.findById("wnd[0]/tbar[1]/btn[8]").press


On Error GoTo 100


200
.findById("wnd[0]/usr/cntlGRID1/shellcont/shell/shellcont[1]/shell").currentCellRow = -1
.findById("wnd[0]/usr/cntlGRID1/shellcont/shell/shellcont[1]/shell").selectColumn "HKONT"
.findById("wnd[0]/usr/cntlGRID1/shellcont/shell/shellcont[1]/shell").contextMenu
.findById("wnd[0]/usr/cntlGRID1/shellcont/shell/shellcont[1]/shell").selectContextMenuItem "&XXL"
.findById("wnd[1]/usr/radRB_OTHERS").Select
.findById("wnd[1]/usr/cmbG_LISTBOX").SetFocus
.findById("wnd[1]/usr/cmbG_LISTBOX").Key = "08"
.findById("wnd[1]/tbar[0]/btn[0]").press
.findById("wnd[1]/tbar[0]/btn[0]").press
.findById("wnd[1]/usr/subSUBSCREEN_STEPLOOP:SAPLSPO5:0150/sub:SAPLSPO5:0150/radSPOPLI-SELFLAG[0,0]").Select
.findById("wnd[1]/tbar[0]/btn[0]").press
.findById("wnd[1]/tbar[0]/btn[0]").press


End With
    
MsgBox "Work Completed"


Exit Sub


100
With SAPsession


'Switch List
.findById("wnd[0]").maximize
.findById("wnd[0]/mbar/menu[5]/menu[8]").Select


Resume 200


Resume 300


300
'Switch List
.findById("wnd[0]").maximize
.findById("wnd[0]/mbar/menu[5]/menu[8]").Select


End With


End Sub
Referring to my codes above, I require "300" to be run only if "On Error GoTo 100" executed. Currently, if the error occurred, it will run until the MsgBox only. Appreciate your help to solve my issue.
 

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
.
I can't test your code here but my thoughts are :

Where you have "100", edit that to: 100: (NOTE: there is a colon after the 100)

As the last line in the section 100:, include the command "GoTo 300"


Code:
100:
With SAPsession
GoTo 300

'- more code

300:

'- more code
 
Last edited:
Upvote 0
Code:
Sub Macro1()

'Filter


On Error GoTo 100


ActiveSheet.Range("$A$1:$B$9").AutoFilter Field:=3, Criteria1:="2"


On Error GoTo 100


200
ActiveSheet.Range("$A$1:$B$9").AutoFilter Field:=2, Criteria1:="2"


MsgBox "Work Completed"
    
Exit Sub
    
100
Cells.Replace What:="7", Replacement:="2", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False


Resume 200


GoTo 300


300
    Cells.Replace What:="Bob", Replacement:="Ian", LookAt:=xlPart, _
        SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
        ReplaceFormat:=False


End Sub
i have made a simple code to reflect the same scenario. I still cant get 300 to execute if error 100 occured.
 
Upvote 0
Before you get to 300 you have
Code:
Resume 200
& at the end of the 200 section you have
Code:
Exit Sub
That's why you'll never get to 300
 
Upvote 0
.
I created a simple MsgBox scenario to track the logic of your macro.

Code:
Option Explicit


Sub Macro1()


'Filter




On Error GoTo 100
MsgBox "This is a simulated error."
GoTo 100


200:
MsgBox "I did not go to 100, this is 200."
Exit Sub
    
100:
MsgBox "This is 100. There was an error."




GoTo 200




GoTo 300




300:
MsgBox "This is 300."




End Sub

I also copied the macro code to NOTEPAD so it could be displayed side-by-side with the running macro as it shows the different MsgBox's. If you do the same there, you see that your macro should perform as
expected. However, are you not seeing the result expect because there is no error on your worksheet ? Again, I can not replicate your workbook here so I don't really know what it is (data) that you are working with.
 
Upvote 0
I have to put
Code:
Exit Sub
. Otherwise after
Code:
MsgBox "Work Completed"
, it will caught up in the loop.
 
Upvote 0
Try
Code:
Sub FullExecution2()
   [COLOR=#0000ff]Dim Flg As Boolean[/COLOR]
    Set Connection = GetObject("SAPGUI").GetScriptingEngine.Children(0)
    If Not IsObject(session) Then
   Set SAPsession = Connection.Children(0)
    End If
    With SAPsession


    .findById("wnd[0]").maximize


    Dim objExcel
    Dim objSheet, intRow, i
    Set objExcel = GetObject(, "Excel.Application")
    Set objSheet = objExcel.ActiveWorkbook.ActiveSheet
 
    col1 = objSheet.Cells(2, 2)
    Col2 = objSheet.Cells(5, 2)
    COL3 = objSheet.Cells(6, 2)
    COL4 = objSheet.Cells(7, 2)
    
'Download SAP FBL5N
.findById("wnd[0]").maximize
.findById("wnd[0]/tbar[0]/okcd").Text = "COL4"
.findById("wnd[0]").sendVKey 0
.findById("wnd[0]/tbar[1]/btn[17]").press
.findById("wnd[1]/tbar[0]/btn[12]").press
.findById("wnd[0]/tbar[1]/btn[17]").press
.findById("wnd[1]/usr/txtV-LOW").Text = "sales rebate"
.findById("wnd[1]/usr/txtV-LOW").caretPosition = 12
.findById("wnd[1]/tbar[0]/btn[8]").press
.findById("wnd[0]/usr/ctxtDD_BUKRS-LOW").Text = col1
.findById("wnd[0]/usr/ctxtSO_BUDAT-LOW").Text = Col2
.findById("wnd[0]/usr/ctxtSO_BUDAT-HIGH").Text = COL3
.findById("wnd[0]/usr/ctxtSO_BUDAT-HIGH").SetFocus
.findById("wnd[0]/usr/ctxtSO_BUDAT-HIGH").caretPosition = 10
.findById("wnd[0]/tbar[1]/btn[8]").press


On Error GoTo 100


200
.findById("wnd[0]/usr/cntlGRID1/shellcont/shell/shellcont[1]/shell").currentCellRow = -1
.findById("wnd[0]/usr/cntlGRID1/shellcont/shell/shellcont[1]/shell").selectColumn "HKONT"
.findById("wnd[0]/usr/cntlGRID1/shellcont/shell/shellcont[1]/shell").contextMenu
.findById("wnd[0]/usr/cntlGRID1/shellcont/shell/shellcont[1]/shell").selectContextMenuItem "&XXL"
.findById("wnd[1]/usr/radRB_OTHERS").Select
.findById("wnd[1]/usr/cmbG_LISTBOX").SetFocus
.findById("wnd[1]/usr/cmbG_LISTBOX").key = "08"
.findById("wnd[1]/tbar[0]/btn[0]").press
.findById("wnd[1]/tbar[0]/btn[0]").press
.findById("wnd[1]/usr/subSUBSCREEN_STEPLOOP:SAPLSPO5:0150/sub:SAPLSPO5:0150/radSPOPLI-SELFLAG[0,0]").Select
.findById("wnd[1]/tbar[0]/btn[0]").press
.findById("wnd[1]/tbar[0]/btn[0]").press


End With
[COLOR=#0000ff]If Flg Then GoTo 300[/COLOR]
MsgBox "Work Completed"


Exit Sub


100
[COLOR=#0000ff]Flg = True[/COLOR]
With SAPsession


'Switch List
.findById("wnd[0]").maximize
.findById("wnd[0]/mbar/menu[5]/menu[8]").Select


Resume 200




300
'Switch List
.findById("wnd[0]").maximize
.findById("wnd[0]/mbar/menu[5]/menu[8]").Select


End With


End Sub
 
Upvote 0
i don't know how do you figure it out. it simply worked! i have been try to solve it for a month. Thank you very much!
 
Upvote 0
Glad we could help & thanks for the feedback
 
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