cancel
Showing results for 
Search instead for 
Did you mean: 

Transfer Data from Excel 365 to MM02

Former Member
0 Kudos

Hello,

I have set up a bunch of misc. material master records in MM02. I have a spreadsheet of 3 columns. I want SAP to call up the material number in the first column, execute the command, enter the two remaining values in two fields, and then save and repeat for the entire list. I can record a macro that does this, but it only does it for one record. I looked on a few sites and found VBA that does what I am looking for, but I can't get it to work.

The code below is what my current script looks like. I need to replace the 3 values with columns A, B, and C, and also need SAP to continue to do this until a blank cell is reached. I'd appreciate any help I could get. Thanks.

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

session.findById("wnd[0]/usr/ctxtRMMG1-MATNR").text = "1117962"

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

session.findById("wnd[1]").sendVKey 0

session.findById("wnd[1]").sendVKey 0

session.findById("wnd[0]/usr/tabsTABSPR1/tabpSP22/ssubTABFRA1:SAPLMGMM:2000/subSUB3:SAPLMGD1:2734/ctxtMLGT-LGPLA").text = "C62"

session.findById("wnd[0]/usr/tabsTABSPR1/tabpSP22/ssubTABFRA1:SAPLMGMM:2000/subSUB1:SAPLMGD1:1006/txtMAKT-MAKTX").text = "SPARK PLUG,ECLIPSE,16160"

session.findById("wnd[0]/usr/tabsTABSPR1/tabpSP22/ssubTABFRA1:SAPLMGMM:2000/subSUB3:SAPLMGD1:2734/ctxtMLGT-LGPLA").setFocus

session.findById("wnd[0]/usr/tabsTABSPR1/tabpSP22/ssubTABFRA1:SAPLMGMM:2000/subSUB3:SAPLMGD1:2734/ctxtMLGT-LGPLA").caretPosition = 3

session.findById("wnd[0]/tbar[0]/btn[11]").press

Accepted Solutions (0)

Answers (1)

Answers (1)

holger_khn
Contributor
0 Kudos

Hello.

First Step: Identify last used cell in column 'A'

Dim lastRow As Long


lastRow = ActiveSheet.Range("A" & Rows.Count).End(xlUp).Row

Second Step: Make a FOR-Loop

Dim lngCounter as Long

Dim strMATNR as String

Dim strLGPLA as String

Dim strMAKTX as String

FOR lngCounter = 2 To lastRow

strMATNR = ActiveSheet.Range("A" & Rows.Count).Value

strLGPLA = ActiveSheet.Range("B" & Rows.Count).Value

strMAKTX = ActiveSheet.Range("C" & Rows.Count).Value

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

Session.FindById("wnd[0]/tbar[0]/okcd").Text = "/nMM02"

session.findById("wnd[0]/usr/ctxtRMMG1-MATNR").text = strMATNR

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

session.findById("wnd[1]").sendVKey 0

session.findById("wnd[1]").sendVKey 0

session.findById("wnd[0]/usr/tabsTABSPR1/tabpSP22/ssubTABFRA1:SAPLMGMM:2000/subSUB3:SAPLMGD1:2734/ctxtMLGT-LGPLA").text = strLGPLA

session.findById("wnd[0]/usr/tabsTABSPR1/tabpSP22/ssubTABFRA1:SAPLMGMM:2000/subSUB1:SAPLMGD1:1006/txtMAKT-MAKTX").text = strMAKTX

session.findById("wnd[0]/tbar[0]/btn[11]").press

NEXT

Please Keep in mind that you Need to touch all relevant fields during recording. If some of this fields are pre-filled this will not in recording. If you remove pre-filled value from field and enter again this will recorded.

I have just take your code and add line for Transaction call as this is mandatory for processing.

Hope this give you some idea. I recommend not testing in production System until you know everything work as expected. May you have a test System available for testing template.

Former Member
0 Kudos

Thanks for the response. I cut and pasted your code into mine, but received an "expected end of statement" error. I am very new to this. I've attached a shorter script with the same need - call up an Excel spreadsheet and enter data from a column until the last value. Could you take a look at this and let me know what I'm doing incorrectly? I'd love to be able to use the code you wrote for several actions I need to perform involving Excel spreadsheets.