VBA SAP HOW TO LOOP

nburaq

Board Regular
Joined
Apr 2, 2021
Messages
222
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
Hi Gents,
I am trying to change a value in a sap table with the data provided in the excel list if the value in sap table is DEPT-MECH. Note that group and counter values are used to enter the table and Work Center is the data to be replaced if rows include DEPT-MECH. I know it requires a loop to check the [Row Number, Column number] and column number will always be constant like [2,1]. If anyone can help me I will be so glad. Thanks for your help and concern.

pS: I assume loop is required in session.findById("wnd[0]/usr/tblSAPLCPDITCTRL_3400/ctxtPLPOD-ARBPL[2,1]").text = COL3 line

VBA Code:
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

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

Rem ADDED BY EXCEL ***************************************

Dim objExcel
Dim objSheet, i
Dim x
Set objExcel = GetObject(, "Excel.Application")
Set objSheet = objExcel.ActiveWorkbook.ActiveSheet

For i = 2 To objSheet.UsedRange.Rows.Count
COL1 = Trim(CStr(objSheet.Cells(i, 1).Value)) 'Group
COL2 = Trim(CStr(objSheet.Cells(i, 2).Value)) 'Counter
COL3=  Trim(CStr(objSheet.Cells(i, 3).Value)) 'WorkCenter


Rem ADDED BY EXCEL ****************************************


session.findById("wnd[0]/tbar[0]/okcd").text = "/NIA06"
session.findById("wnd[0]").sendVKey 0
session.findById("wnd[0]/usr/ctxtRC271-PLNNR").text = COL1       'Group
session.findById("wnd[0]/usr/txtRC271-PLNAL").text = COL2        'Counter
session.findById("wnd[0]/usr/txtRC271-PLNAL").setFocus
session.findById("wnd[0]/usr/txtRC271-PLNAL").caretPosition = 1
session.findById("wnd[0]").sendVKey 0

session.findById("wnd[0]/usr/tblSAPLCPDITCTRL_3400/ctxtPLPOD-ARBPL[2,1]").text = COL3  'WorkCenter will be replaced with DEPT-ELEC if the workcenter is DEPT-MECH only

session.findById("wnd[0]/usr/tblSAPLCPDITCTRL_3400/ctxtPLPOD-ARBPL[2,1]").setFocus
session.findById("wnd[0]/usr/tblSAPLCPDITCTRL_3400/ctxtPLPOD-ARBPL[2,1]").caretPosition = 8
session.findById("wnd[0]").sendVKey 0
session.findById("wnd[0]").sendVKey 0
session.findById("wnd[0]").sendVKey 0
session.findById("wnd[0]/tbar[0]/btn[0]").press
session.findById("wnd[0]/tbar[0]/btn[11]").press

objExcel.Cells(i, 4).Value = session.findById("wnd[0]/sbar").Text 'SystStat

Rem FINALIZATION CONTROL CHECK *****************************
aux=COL1 & " " & COL2
CreateObject("WScript.Shell").run("cmd /c @echo %date% %time% " & aux & " >> C:\Local\Script\CreationLog.txt")
Next
msgbox "Process Completed"
 

Attachments

  • Screenshot 2021-09-13 145232.png
    Screenshot 2021-09-13 145232.png
    3 KB · Views: 28

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off

Forum statistics

Threads
1,215,241
Messages
6,123,823
Members
449,127
Latest member
Cyko

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