请帮我如何将 3 个 VBA 变成 1 个 VBA 。导出数据范围 C.xlsx 完成后下一步...
接下来的问题是如何让导出数据的名称在列中带有值:
A1 = 导出数据范围 C.xlsx
A2 = 导出数据范围 D.xlsx
A3=导出数据范围E.xlsx
vba代码下方
Sub ExportC()
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
Dim sht As Worksheet
Set sht = ThisWorkbook.Worksheets("sheet1")
LastRow = sht.Range("C2").CurrentRegion.Rows.Count
sht.Range("C2:C" & 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 C.xlsx"
session.findById("wnd[1]/usr/ctxtDY_FILENAME").caretPosition = 36
session.findById("wnd[1]/tbar[0]/btn[0]").press
End sub
Sub ExportD()
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
Dim sht As Worksheet
Set sht = ThisWorkbook.Worksheets("sheet1")
LastRow = sht.Range("D2").CurrentRegion.Rows.Count
sht.Range("D2:D" & 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 D.xlsx"
session.findById("wnd[1]/usr/ctxtDY_FILENAME").caretPosition = 36
session.findById("wnd[1]/tbar[0]/btn[0]").press
End sub
Sub ExportE()
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
Dim sht As Worksheet
Set sht = ThisWorkbook.Worksheets("sheet1")
LastRow = sht.Range("E2").CurrentRegion.Rows.Count
sht.Range("E2:E" & 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 E.xlsx"
session.findById("wnd[1]/usr/ctxtDY_FILENAME").caretPosition = 36
session.findById("wnd[1]/tbar[0]/btn[0]").press
End Sub
现在我需要运行 vba 了
在这种情况下,也许对你来说最快的方法就是编写一个 Sub
Main
并调用你已经拥有的其他三个 sub。
Sub Main()
ExportC
ExportD
ExportE
End Sub
对于其他问题,只需将
session.findById("wnd[1]/usr/ctxtDY_FILENAME").Text = "Export Data Range C.xlsx"
和其他子项中的 session.findById("wnd[1]/usr/ctxtDY_FILENAME").Text = Range("A1").value
替换为 ExportC
即可。