title: How to insert (flexible) data into S/4HANA from cells in an Excel file with VBA code?
body: <p>I currently have this VBA Code to insert a dataset (which is in an Excel file) into SAP S/4HANA. What happens now with the code is that it has set the values that I have entered, however, I would like the code to go to the Excel cell and read the value that is in the cell so that the insertion of the data in SAP is flexible.</p>
  <p>On line 35 where the text appears, this line does not always work as I want... Because in SAP opens type Word file to insert the text.</p>
  <p>The date in the text should fetch the month and year from cell <code>I1</code>.
  I added comments in the code says where the data is in Excel.</p>
  <pre class="lang-vb prettyprint-override"><code>Sub services()
  
  Set SapGuiAuto = GetObject(&quot;SAPGUI&quot;)
  Set objGui = SapGuiAuto.GetScriptingEngine
  Set objConn = objGui.Children(0)
  Set session = objConn.Children(0)
  
  session.findById(&quot;wnd[0]&quot;).maximize
  session.findById(&quot;wnd[0]/tbar[0]/okcd&quot;).Text = &quot;FB70&quot;
  session.findById(&quot;wnd[0]&quot;).sendVKey 0
  session.findById(&quot;wnd[1]/usr/ctxtBKPF-BUKRS&quot;).Text = &quot;2222&quot;
  session.findById(&quot;wnd[1]/usr/ctxtBKPF-BUKRS&quot;).caretPosition = 4
  session.findById(&quot;wnd[1]/tbar[0]/btn[0]&quot;).press
  'Below value from CELL B4
  session.findById(&quot;wnd[0]/usr/tabsTS/tabpMAIN/ssubPAGE:SAPLFDCB:0510/ctxtINVFO-ACCNT&quot;).Text = &quot;1234567&quot;
  session.findById(&quot;wnd[0]/usr/tabsTS/tabpMAIN/ssubPAGE:SAPLFDCB:0510/ctxtINVFO-BLDAT&quot;).SetFocus
  session.findById(&quot;wnd[0]/usr/tabsTS/tabpMAIN/ssubPAGE:SAPLFDCB:0510/ctxtINVFO-BLDAT&quot;).caretPosition = 0
  session.findById(&quot;wnd[0]&quot;).sendVKey 4
  session.findById(&quot;wnd[1]/tbar[0]/btn[0]&quot;).press
  'Below value from CELL I4
  session.findById(&quot;wnd[0]/usr/tabsTS/tabpMAIN/ssubPAGE:SAPLFDCB:0510/txtINVFO-WRBTR&quot;).Text = &quot;100.00&quot;
  session.findById(&quot;wnd[0]/usr/tabsTS/tabpMAIN/ssubPAGE:SAPLFDCB:0510/chkINVFO-XMWST&quot;).SetFocus
  session.findById(&quot;wnd[0]/usr/tabsTS/tabpMAIN/ssubPAGE:SAPLFDCB:0510/chkINVFO-XMWST&quot;).Selected = True
  session.findById(&quot;wnd[0]/usr/tabsTS/tabpMAIN/ssubPAGE:SAPLFDCB:0510/cmbINVFO-MWSKZ&quot;).SetFocus
  session.findById(&quot;wnd[0]/usr/tabsTS/tabpMAIN/ssubPAGE:SAPLFDCB:0510/cmbINVFO-MWSKZ&quot;).Key = &quot;J3&quot;
  'Below value from CELL G4
  session.findById(&quot;wnd[0]/usr/subITEMS:SAPLFSKB:0100/tblSAPLFSKBTABLE/ctxtACGL_ITEM-HKONT[1,0]&quot;).Text = &quot;25875000&quot;
  'Below value from CELL I4
  session.findById(&quot;wnd[0]/usr/subITEMS:SAPLFSKB:0100/tblSAPLFSKBTABLE/txtACGL_ITEM-WRBTR[4,0]&quot;).Text = &quot;100.00&quot;
  'Below value from CELL H4
  session.findById(&quot;wnd[0]/usr/subITEMS:SAPLFSKB:0100/tblSAPLFSKBTABLE/ctxtACGL_ITEM-KOSTL[17,0]&quot;).Text = &quot;3782000&quot;
  session.findById(&quot;wnd[0]/usr/subITEMS:SAPLFSKB:0100/tblSAPLFSKBTABLE/btnACGL_ITEM-EOBJ[35,0]&quot;).SetFocus
  session.findById(&quot;wnd[0]/usr/subITEMS:SAPLFSKB:0100/tblSAPLFSKBTABLE/btnACGL_ITEM-EOBJ[35,0]&quot;).press
  'Below value from CELL B4
  session.findById(&quot;wnd[1]/usr/sub:SAPLKEAK:0400/ctxtRKEAK-FIELD[0,24]&quot;).Text = &quot;1234567&quot;
  session.findById(&quot;wnd[1]/usr/sub:SAPLKEAK:0400/ctxtRKEAK-FIELD[0,24]&quot;).caretPosition = 7
  session.findById(&quot;wnd[1]/tbar[0]/btn[0]&quot;).press
  session.findById(&quot;wnd[0]/usr/subITEMS:SAPLFSKB:0100/tblSAPLFSKBTABLE/btnACGL_ITEM-LTEXT[12,0]&quot;).SetFocus
  session.findById(&quot;wnd[0]/usr/subITEMS:SAPLFSKB:0100/tblSAPLFSKBTABLE/btnACGL_ITEM-LTEXT[12,0]&quot;).press
  session.findById(&quot;wnd[1]/usr/subNOTICE:SAPLEENO:1001/btnEENO_TEXTE-EDITOR&quot;).press
  'Below values: The date is in cell I1 (format: month/yyyy) and the text is in cell J4
  session.findById(&quot;wnd[0]/usr/cntlSCMSW_CONTAINER_2102/shellcont/shell&quot;).setDocument 1, _
  &quot;Services about health and security for the month of april/2022&quot;   
  session.findById(&quot;wnd[0]/tbar[0]/btn[3]&quot;).press
  session.findById(&quot;wnd[1]/tbar[0]/btn[5]&quot;).press
  session.findById(&quot;wnd[0]/tbar[0]/btn[11]&quot;).press
  
  End Sub
  </code></pre>
  <p>Many thanks!</p>
  <p>Print:
  <a href="https://i.stack.imgur.com/VksaG.png" rel="nofollow noreferrer">enter image description here</a></p>