Consulting

Results 1 to 2 of 2

Thread: How to copy values from multiple cells into one row in SAP

  1. #1
    VBAX Newbie
    Joined
    Mar 2021
    Posts
    1
    Location

    Question How to copy values from multiple cells into one row in SAP

    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

    11.jpg

    hrr.jpg
    Thank you for help!
    Last edited by Liwiusky; 03-04-2021 at 08:11 AM.

  2. #2
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    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
    Last edited by SamT; 03-06-2021 at 11:43 AM.
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •