PDA

View Full Version : How to copy values from multiple cells into one row in SAP



Liwiusky
03-04-2021, 07:32 AM
Hello people,

I have some troubles with my code.

I want to copy data from multiple cells and put it in a row with space between, on SAP text field (BSEG-SGTXT) but my code is replacing the previously value with the next one :(

Could you please help me with that?

I have made the text red where is the problem.

Here is my code:

Private Sub CommandButton1_Click()


Set SapGuiAuto = GetObject("SAPGUI")
Set Sapplication = SapGuiAuto.GetScriptingEngine
Set Connection = Sapplication.Children(0)
Set session = Connection.Children(0)


Dim x As Integer, y As Integer



x = 2
Sheets(1).Cells(x, 2).Select
On Error Resume Next
Do While Selection.Value <> ""


'first steps
session.FindById("wnd[0]/tbar[0]/okcd").Text = "/nf-32"
session.FindById("wnd[0]").SendVKey 0
session.FindById("wnd[0]/usr/sub:SAPMF05A:0131/radRF05A-XPOS1[2,0]").Select
session.FindById("wnd[0]/usr/ctxtRF05A-AGKON").Text = Sheets(1).Cells(x, 1).Value
session.FindById("wnd[0]/usr/ctxtBKPF-BUDAT").Text = Sheets(1).Range("L2").Value
session.FindById("wnd[0]/usr/txtBKPF-MONAT").Text = Sheets(1).Range("N2").Value
session.FindById("wnd[0]/usr/ctxtBKPF-BUKRS").Text = Sheets(1).Range("O2").Value
session.FindById("wnd[0]/usr/ctxtBKPF-WAERS").Text = Sheets(1).Range("M2").Value
session.FindById("wnd[0]/tbar[1]/btn[16]").Press




'insert documents
Sheets(1).Cells(x, 2).Select
On Error Resume Next
Do While Selection.Value <> ""
session.FindById("wnd[0]/usr/sub:SAPMF05A:0731/txtRF05A-SEL01[0,0]").Text = Selection.Value
session.FindById("wnd[0]").SendVKey 0
Selection.Offset(1, 0).Select
Loop




session.FindById("wnd[0]/tbar[1]/btn[16]").Press
session.FindById("wnd[0]/tbar[1]/btn[16]").Press
session.FindById("wnd[0]/usr/tabsTS/tabpMAIN/ssubPAGE:SAPDF05X:6102/btnICON_SELECT_ALL").Press
session.FindById("wnd[0]/usr/tabsTS/tabpMAIN/ssubPAGE:SAPDF05X:6102/btnIC_Z+").Press
session.FindById("wnd[0]/usr/tabsTS/tabpMAIN/ssubPAGE:SAPDF05X:6102/txtRF05A-ABPOS").Text = session.FindById("wnd[0]/usr/tabsTS/tabpMAIN/ssubPAGE:SAPDF05X:6102/txtRF05A-ANZPO").Text
session.FindById("wnd[0]/usr/tabsTS/tabpMAIN/ssubPAGE:SAPDF05X:6102/txtRF05A-ABPOS").SetFocus
session.FindById("wnd[0]/usr/tabsTS/tabpMAIN/ssubPAGE:SAPDF05X:6102/txtRF05A-ABPOS").CaretPosition = 1
session.FindById("wnd[0]").SendVKey 0




y = session.FindById("wnd[0]/usr/tabsTS/tabpMAIN/ssubPAGE:SAPDF05X:6102/txtRF05A-DIFFB").Text



If y = 0 Then ' 121 clearing
session.FindById("wnd[0]").SendVKey 11
End If



If y < 0 Then ' partial
session.FindById("wnd[0]/usr/tabsTS/tabpREST").Select
session.FindById("wnd[0]").SendVKey 2
session.FindById("wnd[0]/mbar/menu[0]/menu[1]").Select
session.FindById("wnd[0]/usr/sub:SAPMF05A:0700/txtRF05A-AZEI1[0,0]").SetFocus
session.FindById("wnd[0]/usr/sub:SAPMF05A:0700/txtRF05A-AZEI1[0,0]").CaretPosition = 33
session.FindById("wnd[0]").SendVKey 2
session.FindById("wnd[1]/usr/txt*BSEG-BUZEI").Text = "001"
session.FindById("wnd[1]/usr/txt*BSEG-BUZEI").CaretPosition = 3
session.FindById("wnd[1]/tbar[0]/btn[13]").Press
Sheets(1).Cells(x, 2).Select
On Error Resume Next
Do While Selection.Value <> ""
session.FindById("wnd[0]/usr/ctxtBSEG-SGTXT").Text = Selection.Value
Selection.Offset(1, 0).Select
session.FindById("wnd[0]/usr/ctxtBSEG-SGTXT").CaretPosition = 24
'session.FindById("wnd[0]").SendVKey 11
session.FindById("wnd[1]/tbar[0]/btn[0]").Press
End If




NextStep:
Sheets(1).Cells(x, 4) = session.FindById("wnd[0]/sbar").Text
Range("A" & ActiveCell.Row).Select
Selection.End(xlDown).Select
x = Selection.Row


Loop


MsgBox "DONE"


End Sub

28054

28055
Thank you for help! :)

SamT
03-06-2021, 11:25 AM
x = 2... always... Until x = Selection.Row

I suggest you change the word "x" to "CurrentRow" so you never forget what is going on in your code

Note also the error in

x = 2
Sheets(1).Cells(x, 2).Select
On Error Resume Next

Do While Selection.Value <> "" 'First = B2, thereafter = B3
'
'

'insert documents
Sheets(1).Cells(x, 2).Select 'B2
'
'
'Selection.Offset(1, 0).Select 'B3
Loop