Hello Everyone,
I am a real beginner in the VBA & SAP Scripting.
Below is my VBA Script for changing the asset master data in t-code AS02.
It works really fine but I would like to make it better.
First thing I would like to do is to create a loop which will make possible to change lot of assets numbers which will be filled in the excel sheet Do you have any suggestions ?
The second thing I would like to do is to make visible in the excel (where script is run) the SAP output message after the change is done - for instance “Asset number XXXX has been changed” or if for instance sth went wrong – “Asset number does not belong to selected company code” etc
Thank you in advance for any help.
Regards
MJ
Private Sub CommandButton1_Click()
If Not IsObject(Application1) Then
Set SapGuiAuto = GetObject("SAPGUI")
Set Application1 = SapGuiAuto.GetScriptingEngine
End If
If Not IsObject(Connection) Then
Set Connection = Application1.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 Application1, "on"
End If
Set xclapp = CreateObject("Excel.Application")
Set xclwbk = xclapp.Workbooks.Open("c:\Users\Public\AS02.xlsm")
Set xclsht = xclwbk.Sheets("Sheet1")
For i = 2 To xclapp.ActiveCell.SpecialCells(11).Row
For j = 1 To xclapp.ActiveCell.SpecialCells(11).Column
If j = 1 Then Asset = xclsht.Cells(i, j).Value
If j = 2 Then Company = xclsht.Cells(i, j).Value
If j = 3 Then Zmiana = xclsht.Cells(i, j).Value
Next
session.findById("wnd[0]").maximize
session.findById("wnd[0]/tbar[0]/okcd").Text = "/n as02"
session.findById("wnd[0]").sendVKey 0
session.findById("wnd[0]/usr/ctxtANLA-ANLN1").Text = Asset
session.findById("wnd[0]/usr/ctxtANLA-BUKRS").Text = Company
session.findById("wnd[0]/usr/ctxtANLA-BUKRS").SetFocus
session.findById("wnd[0]/usr/ctxtANLA-BUKRS").caretPosition = 4
session.findById("wnd[0]").sendVKey 0
session.findById("wnd[0]/usr/subTABSTRIP:SAPLATAB:0100/tabsTABSTRIP100/tabpTAB01/ssubSUBSC:SAPLATAB:0200/subAREA1:SAPLAIST:1140/txtANLA-TXA50").Text = Zmiana
session.findById("wnd[0]/usr/subTABSTRIP:SAPLATAB:0100/tabsTABSTRIP100/tabpTAB01/ssubSUBSC:SAPLATAB:0200/subAREA1:SAPLAIST:1140/txtANLA-TXA50").SetFocus
session.findById("wnd[0]/usr/subTABSTRIP:SAPLATAB:0100/tabsTABSTRIP100/tabpTAB01/ssubSUBSC:SAPLATAB:0200/subAREA1:SAPLAIST:1140/txtANLA-TXA50").caretPosition = 3
session.findById("wnd[0]/tbar[0]/btn[11]").press
Next
MsgBox "All " & CStr(xclapp.ActiveCell.SpecialCells(11).Row - 1) & " Excel rows have been processed."
Set xclwbk = Nothing
Set xclsht = Nothing
xclapp.Quit
Set xclapp = Nothing
End Sub