VBA On error isn't working

Tashkkaa

New Member
Joined
Feb 21, 2024
Messages
4
Hi, I'm trying to make macro that works with SAP. I would like to use On Error function for two options of pop up windows that appear. If it's not possible to execute 'session.findById("wnd[1]/usr/btnSPOP-OPTION1").press' the program should run 'session.findById("wnd[1]/usr/btnSPOP-VAROPTION2").press' instead. Unfortunately the macro stops working and I get error on the 'session.findById("wnd[1]/usr/btnSPOP-VAROPTION2").press' field. Do you know what could be the issue?


VBA Code:
For i = 2 To lastRow

docNumber = ThisWorkbook.Sheets("Sheet1").Range("A" & i).Value
selectedCC = ThisWorkbook.Sheets("Sheet1").Range("B" & i).Value
fiscalYear = ThisWorkbook.Sheets("Sheet1").Range("C" & i).Value
reversalReason = ThisWorkbook.Sheets("Sheet1").Range("E" & i).Value
reversalDate = ThisWorkbook.Sheets("Sheet1").Range("F" & i).Value
reversalPeriod = ThisWorkbook.Sheets("Sheet1").Range("G" & i).Value

   
   

    session.findById("wnd[0]/usr/txtRF05R-AUGBL").Text = docNumber
    session.findById("wnd[0]/usr/ctxtRF05R-BUKRS").Text = selectedCC
    session.findById("wnd[0]/usr/txtRF05R-GJAHR").Text = fiscalYear
    session.findById("wnd[0]/usr/txtRF05R-GJAHR").SetFocus
    session.findById("wnd[0]/usr/txtRF05R-GJAHR").caretPosition = 4
    session.findById("wnd[0]/tbar[0]/btn[11]").press
   
    On Error GoTo errHandler
   
    session.findById("wnd[1]/usr/btnSPOP-OPTION1").press
   

errHandler:
    session.findById("wnd[1]/usr/btnSPOP-VAROPTION2").press
   
    On Error GoTo -1
   

    session.findById("wnd[1]/usr/ctxtRF05R-STGRD").Text = reversalReason
    session.findById("wnd[1]/usr/ctxtRF05R-BUDAT").Text = reversalDate
    session.findById("wnd[1]/usr/txtRF05R-MONAT").Text = reverselPeriod
    session.findById("wnd[1]/usr/txtRF05R-MONAT").SetFocus
    session.findById("wnd[1]/usr/txtRF05R-MONAT").caretPosition = 2
    session.findById("wnd[1]/tbar[0]/btn[0]").press
    session.findById("wnd[1]/tbar[0]/btn[0]").press
    session.findById("wnd[1]/tbar[0]/btn[0]").press
   

Next i
 
Last edited by a moderator:

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
As a general rule, if you are using On Error Goto -1, your code is badly written. ;)

You could simply use On Error Resume Next:

Code:
On Error Resume Next

session.findById("wnd[1]/usr/btnSPOP-OPTION1").press
If err.number <> 0 then
   err.clear
   session.findById("wnd[1]/usr/btnSPOP-VAROPTION2").press
end If
On Error GoTo 0
 
Upvote 0

Forum statistics

Threads
1,221,310
Messages
6,159,176
Members
451,543
Latest member
cesymcox

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