Option Explicit
'https://simpleexcelvba.com/category/sap/
Sub MàJPrix()
' Variables objet pour SAP
Dim App, Connection, Session, Wscript
Dim SapGui As Object
' Variables autre
Dim dLig As Long, Lig As Long, sTmp As String
' Demander à lutilisateur avant lancement
If MsgBox("Vous devez être sur le menu général de SAP !" & vbCr & vbCr _
& "Avant de continuer, est-ce bien le cas ?", vbQuestion + vbYesNo, "ATTENTION...") = vbNo Then Exit Sub
' Minimiser cette fenêtre
ThisWorkbook.Windows.Application.WindowState = xlMinimized
' Suite pour SAP
If Not IsObject(App) Then
Set SapGui = GetObject("SAPGUI")
Set App = SapGui.GetScriptingEngine
End If
If Not IsObject(Connection) Then
Set Connection = App.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 App, "on"
End If
Session.findById("wnd[0]").maximize
' Avec la feuille nommée
With Sheets("Feuil1")
' Dernière ligne rempliede la feuil1e
dLig = .Range("A" & Rows.Count).End(xlUp).Row
' Pour chaque ligne
For Lig = 2 To dLig
sTmp = .Range("C" & Lig) ' Récupérer le code article
Session.findById("wnd[0]/usr/ctxtIF_MATNR-LOW").Text = sTmp
Session.findById("wnd[0]/tbar[1]/btn[8]").press
Session.findById("wnd[0]/usr/chk[1,8]").Selected = True
Session.findById("wnd[0]/tbar[1]/btn[7]").press
Session.findById("wnd[0]/tbar[1]/btn[8]").press
Session.findById("wnd[1]/usr/tblSAPLV14ATCTRL_D0102/ctxtVAKE-DATAB[0,0]").caretPosition = 8
Session.findById("wnd[1]").sendVKey 2
sTmp = .Range("I" & Lig) ' Récupérer le prix
Session.findById("wnd[0]/usr/tblSAPMV13ATCTRL_D0201/txtKONP-KBETR[2,0]").Text = sTmp
Session.findById("wnd[0]/usr/tblSAPMV13ATCTRL_D0201/txtKONP-KBETR[2,0]").SetFocus
Session.findById("wnd[0]/usr/tblSAPMV13ATCTRL_D0201/txtKONP-KBETR[2,0]").caretPosition = 16
Session.findById("wnd[0]/tbar[0]/btn[11]").press
Session.findById("wnd[0]/tbar[0]/btn[3]").press
Next Lig
End With
' Effacer la variable objet
Set Session = Nothing: Set Connection = Nothing: Set App = Nothing: Set SapGui = Nothing
' Maximise ce classeur
ThisWorkbook.Windows.Application.WindowState = xlMaximized
End Sub