Sub Export()
If Not IsObject(Application1) Then
Set SapGuiAuto = GetObject("SAPGUI")
Set Application1 = SapGuiAuto.GetScriptingEngine
End If
If Not IsObject(Connection) Then
Set Connection = Application1.Children(0)
End If
If Not IsObject(session) Then
Set session = Connection.Children(0)
End If '
Dim lastrow As Long
Dim sht As Worksheet
Dim var As Variant
Set sht = ThisWorkbook.Worksheets("sheet1")
For Each var In Split("A B C")
lastrow = sht.Cells(sht.Rows.Count, var).End(xlUp).Row
Call ExportRoutine(session, sht, var, lastrow)
Next
Set sht = Nothing
End Sub
Private Sub ExportRoutine(ByRef session As Object, ByRef sht As Worksheet, Byval column As String, Byval lastrow As Long)
sht.Range(colum & "2:" & column & lastrow).Copy
session.findById("wnd[0]/tbar[0]/okcd").Text = "/NFBL1N"
session.findById("wnd[0]").sendVKey 0
session.findById("wnd[0]/usr/ctxtKD_BUKRS-LOW").Text = "BK01"
session.findById("wnd[0]/usr/ctxtKD_BUKRS-LOW").SetFocus
session.findById("wnd[0]/usr/ctxtKD_BUKRS-LOW").caretPosition = 4
session.findById("wnd[0]/usr/btn%_KD_LIFNR_%_APP_%-VALU_PUSH").press
session.findById("wnd[1]/tbar[0]/btn[24]").press
session.findById("wnd[1]/tbar[0]/btn[8]").press
session.findById("wnd[0]/tbar[1]/btn[8]").press
session.findById("wnd[0]/mbar/menu[0]/menu[3]/menu[1]").Select
session.findById("wnd[1]/usr/cmbG_LISTBOX").SetFocus
session.findById("wnd[1]/tbar[0]/btn[0]").press
session.findById("wnd[1]/usr/ctxtDY_PATH").Text = "D:\SAP\Data\"
session.findById("wnd[1]/usr/ctxtDY_FILENAME").Text = "Export Data Range " & column & ".xlsx"
session.findById("wnd[1]/usr/ctxtDY_FILENAME").caretPosition = 36
session.findById("wnd[1]/tbar[0]/btn[0]").press
End Sub