当前位置:   article > 正文

【SAP GUI 脚本 VBA】_sap脚本

sap脚本

目录

启用 SAP脚本

Tracker

Excel启用VBA

对象声明

用法

TEXT文本

Press点击

Key选择

Selected复选框

判断字段是否存在

VerticalScrollbar 滑动滚动条

Enter

粘贴剪贴板

读取shell

读取shell[1]

VBS登入SAP

 实例

函数-提取Tcode 

 登入开发区

CO03

MM03

CS15

TEST

KS13

KSH1

KSH2

KSH3

FS00

SM30

Tcode

CKM3N

FB02

KSU1

KSU2

KSU3

KSV1、KSV2、KSV3

与其他方式对比



启用 SAP脚本

1.使用前“脚本录制和回放”的功能是要开启状态。如果没开启是要找管理员开启。

2.点击后红色按钮开启录制

 

3.此时可以在SAP里进行手动操作,可以记录下用户操作的脚本。

录制完之后可以点击关闭。再点击“更多”。

4.可以把这个Script1.vbs这个复制到桌面,把后缀名改成txt

 如下是进入MM03查询了某个料号的脚本。

Tracker

进入SAP后,启用Tracker,点击这个 图标。可以查询程式里字段的ID。 

如物料的ID是

wnd[0]/usr/tabsTABSPR1/tabpSP01/ssubTABFRA1:SAPLMGMM:2004/subSUB1:SAPLMGD1:1002/ctxtRMMG1-MATNR

Excel启用VBA

勾选“开发工具”

点击“宏安全性”

点击“启用所有宏”。关闭EXCEL再打开即可。

点击“Visual Basic”

进入后点击插入

点击插入模块

在编辑界面输入SUB,命名程序后回车

可以复制录制脚本的代码进去,点击执行即可

录制的这个部分是VBS的内容,不能在EXCEL里执行,要改下。

对象声明

改成的这个如果没进入SAP的话会报错,并要调试。

  1. Dim SapGuiAuto As Object, AppSap As Object, Connection As Object, session As Object
  2. Set SapGuiAuto = GetObject("SAPGUI")
  3. Set AppSap = SapGuiAuto.GetScriptingEngine
  4. Set Connection = AppSap.Children(0)
  5. Set session = Connection.Children(0)

 如果没进SAP的话,改成MsgBox提醒错误

  1. Dim SapGuiAuto As Object, AppSap As Object, Connection As Object, session As Object
  2. On Error Resume Next
  3. Set SapGuiAuto = GetObject("SAPGUI")
  4. Set AppSap = SapGuiAuto.GetScriptingEngine
  5. Set Connection = AppSap.Children(0)
  6. Set session = Connection.Children(0)
  7. If Err > 0 Then
  8. MsgBox "请检查是否登入SAP", vbExclamation
  9. Exit Sub
  10. End If
  11. On Error GoTo 0

定义函数直接调用,更方便

  1. Public session As Object
  2. Function MyConnectSAP() As Boolean
  3. Dim SapGuiAuto As Object, AppSap As Object, Connection As Object
  4. On Error Resume Next
  5. Set SapGuiAuto = GetObject("SAPGUI")
  6. Set AppSap = SapGuiAuto.GetScriptingEngine
  7. Set Connection = AppSap.Children(0)
  8. Set session = Connection.Children(0)
  9. If Err > 0 Then
  10. MsgBox "请检查是否登入SAP", vbExclamation
  11. MyConnectSAP = True
  12. Else
  13. MyConnectSAP = False
  14. End If
  15. Set SapGuiAuto = Nothing
  16. Set AppSap = Nothing
  17. Set Connection = Nothing
  18. End Function

用法

TEXT文本

在栏位里输入文本,例如

session.findById("wnd[0]/tbar[0]/okcd").Text = "/NCKM3N"

Press点击

点击,例如:

session.findById("wnd[0]/tbar[1]/btn[13]").press

Key选择

选择,例如:

session.findById("wnd[0]/usr/cmbMLKEY-CURTP").Key = "10"

Selected复选框

可以操作复选框,TRUE表示勾选,FALSE表示不勾选

session.findById("wnd[0]/usr/chkPA_XKONS").Selected = False

判断字段是否存在

如下是判断某个字段确实存在,删去Not表示判断某个字段确实不存在

  1. If Not session.findById("wnd[2]/usr/txtMESSTXT1", False) Is Nothing Then
  2. End If

VerticalScrollbar 滑动滚动条

16代表一次滑动16个栏位

session.findById("wnd[1]/usr/tblSAPLMGMMTC_VIEW").verticalScrollbar.Position = 16

Enter

输入Enter键

session.findById("wnd[0]").sendVKey 0

粘贴剪贴板

先声明了字典d,在Excel中取值(此处省略了这个部分),然后通过“多项选择”,除去重复值后,粘贴到剪贴板中

  1. Dim objData As New MSForms.DataObject, d As Object
  2. Dim objData As New MSForms.DataObject
  3. With session
  4. .findById("wnd[0]/usr/btn%_SO_WERKS_%_APP_%-VALU_PUSH").press '点击
  5. objData.SetText Join(d.keys, Chr(13) & Chr(10))
  6. objData.PutInClipboard '复制到剪贴板中
  7. .findById("wnd[1]/tbar[0]/btn[16]").press '删除整个选择
  8. .findById("wnd[1]/tbar[0]/btn[24]").press '自剪切板上载
  9. .findById("wnd[1]/tbar[0]/btn[8]").press '点击
  10. d.RemoveAll '删除
  11. End With

读取shell

  1. '读取shell时不同于text,要通过循环取值
  2. '把取到的shell赋值给Table
  3. 'Table.RowCount表示总行数
  4. 'Table.ColumnCount表示总列数
  5. 'Table.ColumnOrder可以取列名
  6. 'Table.getcellvalue 可以取表的值
  7. '例如此处把取到的Table传到了数组arr里,然后在读取到Excel中
  8. Dim x As Integer, y As Integer, k As Integer, arr(), Title()
  9. ReDim arr(1 To 100000, 1 To 15)
  10. ReDim Title(1 To 15)
  11. With session
  12. Set Table = .findById("wnd[0]/usr/cntlGRID1/shellcont/shell/shellcont[1]/shell")
  13. Set Columns = Table.ColumnOrder() '取列
  14. For x = 0 To Table.RowCount() - 1 'Table.RowCount表示为总行数
  15. k = k + 1
  16. For y = 0 To Table.ColumnCount() - 1
  17. arr(k, y + 1) = Table.getcellvalue(x, CStr(Columns(y))) '取值
  18. Next y
  19. Next x
  20. For y = 0 To Table.ColumnCount() - 1
  21. Title(y + 1) = CStr(Columns(y)) 'Columns返回标题文本
  22. Next y
  23. End With

读取shell[1]

  1. '读取shell[1]里隐藏的内容时需要打开节点
  2. 'Table.GetAllNodeKeys 表示所有的节点,返回值是数字
  3. 'Table.expandNode 打开节点
  4. 'Table.GetAllNodeKeys.Count 表示总节点数
  5. 'Table.getitemtext 可以获取内容
  6. Dim Table As Object, GetNodeK As Object, arr(), i As Integer, x
  7. '进入程式获取节点
  8. With session
  9. .findById("wnd[0]").maximize
  10. .findById("wnd[0]/tbar[0]/okcd").Text = "/NFS00"
  11. .findById("wnd[0]").sendVKey 0 'Enter
  12. Set Table = .findById("wnd[0]/shellcont/shell/shellcont[1]/shell[1]")
  13. Set GetNodeK = Table.GetAllNodeKeys '得到所有的节点
  14. End With
  15. '打开所有节点
  16. For x = GetNodeK.Count - 1 To 0 Step -1
  17. Table.expandNode GetNodeK.Item(x)
  18. Next x
  19. '重新读取shell[1]
  20. Set Table = session.findById("wnd[0]/shellcont/shell/shellcont[1]/shell[1]")
  21. Set GetNodeK = Table.GetAllNodeKeys '得到所有的节点
  22. For x = 0 To GetNodeK.Count - 1
  23. i = i + 1
  24. ReDim Preserve arr(1 To i)
  25. arr(i) = GetNodeK.Item(x) '节点
  26. arr(i) = Table.getitemtext(arr(i), "&Hierarchy") '获得内容
  27. Next x

VBS登入SAP

 VBS登入SAP开发区110

这个能不能成功运行,主要还是靠SendKeys操作键盘,让SAP弹出输入密码的界面,网络延迟,或者SAP Logon不是当时的选择的状态的话都有可能登不上。

  1. Dim wsh
  2. Set wsh = CreateObject("Wscript.shell")
  3. '如果路径中带空格需要用chr(34)&"path"& chr(34)包起来
  4. wsh.Run Chr(34) & "C:\Program Files (x86)\SAP\FrontEnd\SAPgui\saplogon.exe" & Chr(34)
  5. wscript.sleep 500
  6. wsh.SendKeys "~"
  7. wscript.sleep 2000
  8. If Not IsObject(Application) Then
  9. Set SapGuiAuto = GetObject("SAPGUI")
  10. Set Application = SapGuiAuto.GetScriptingEngine
  11. End If
  12. If Not IsObject(Connection) Then
  13. Set Connection = Application.Children(0)
  14. End If
  15. If Not IsObject(session) Then
  16. Set session = Connection.Children(0)
  17. End If
  18. If IsObject(wscript) Then
  19. wscript.ConnectObject session, "on"
  20. wscript.ConnectObject Application, "on"
  21. End If
  22. With session
  23. .findById("wnd[0]").maximize
  24. .findById("wnd[0]/usr/txtRSYST-MANDT").Text = "110"
  25. .findById("wnd[0]/usr/txtRSYST-BNAME").Text = "C10089213"
  26. .findById("wnd[0]/usr/pwdRSYST-BCODE").Text = "" '密码
  27. .findById("wnd[0]").sendVKey 0
  28. End With

 实例

函数-提取Tcode 

  1. Function MyGetSAPtCode() As String
  2. If MyConnectSAP() Then Exit Function
  3. Application.Volatile
  4. MyGetSAPtCode = session.findById("wnd[0]/sbar/pane[1]").Text
  5. Set session = Nothing
  6. End Function

 登入开发区

  1. Sub 登入110()
  2. Shell "C:\Program Files (x86)\SAP\FrontEnd\SAPgui\saplogon.exe", vbNormalFocus
  3. Application.Wait (Now() + TimeValue("00:00:02"))
  4. SendKeys "~"
  5. Application.Wait (Now() + TimeValue("00:00:04")) '如果系统反应不过来的话后面会赋值不到,有必要的话可以延长时间
  6. Dim SapGuiAuto As Object, AppSap As Object, Connection As Object, session As Object
  7. Set SapGuiAuto = GetObject("SAPGUI")
  8. Set AppSap = SapGuiAuto.GetScriptingEngine
  9. Set Connection = AppSap.Children(0)
  10. Set session = Connection.Children(0)
  11. With session
  12. .findById("wnd[0]").maximize
  13. .findById("wnd[0]/usr/txtRSYST-MANDT").Text = "110"
  14. .findById("wnd[0]/usr/txtRSYST-BNAME").Text = "C10089213"
  15. .findById("wnd[0]/usr/pwdRSYST-BCODE").Text = "" '密码
  16. .findById("wnd[0]").sendVKey 0
  17. End With
  18. End Sub

CO03

CO03中批量查询研发工单的信息

  1. Sub CO03_显示_结算规则()
  2. Dim iMg As VbMsgBoxStyle
  3. iMg = MsgBox("是否显示CO03?" & Chr(10) & " " & Chr(10), vbYesNo, "CO03")
  4. If iMg = 7 Then Exit Sub
  5. Dim SapGuiAuto As Object, AppSap As Object, Connection As Object, session As Object
  6. Set SapGuiAuto = GetObject("SAPGUI")
  7. Set AppSap = SapGuiAuto.GetScriptingEngine
  8. Set Connection = AppSap.Children(0)
  9. Set session = Connection.Children(0)
  10. Dim x As Integer, y As Integer, sr As String, rg As Range, arr1(), i As Integer, bl As Boolean
  11. Dim Table As Object, Columns As Object
  12. ReDim arr2(1 To 1000, 1 To 10)
  13. sr = "CO03"
  14. Set rg = ThisWorkbook.Sheets(sr).UsedRange.Find("*")
  15. If rg Is Nothing Then
  16. MsgBox "错误!表【" & sr & "】中无数据!"
  17. Exit Sub
  18. End If
  19. arr1 = rg.CurrentRegion.Value
  20. With session
  21. For x = 2 To UBound(arr1)
  22. If arr1(x, 1) = "" Then Exit For
  23. .findById("wnd[0]").maximize
  24. .findById("wnd[0]/tbar[0]/okcd").Text = "/NCO03"
  25. .findById("wnd[0]").sendVKey 0 'Enter
  26. .findById("wnd[0]/usr/ctxtCAUFVD-AUFNR").Text = arr1(x, 1) '工单
  27. .findById("wnd[0]").sendVKey 0 '
  28. arr2(x - 1, 1) = "'" & .findById("wnd[0]/usr/ctxtCAUFVD-WERKS").Text '工厂
  29. .findById("wnd[0]/usr/tabsTABSTRIP_0115/tabpVERW").Select '管理
  30. arr2(x - 1, 2) = .findById("wnd[0]/usr/tabsTABSTRIP_0115/tabpVERW/ssubSUBSCR_0115:SAPLCOKO1:0170/txtCAUFVD-ERNAM").Text '创建
  31. arr2(x - 1, 3) = .findById("wnd[0]/usr/tabsTABSTRIP_0115/tabpVERW/ssubSUBSCR_0115:SAPLCOKO1:0170/txtCAUFVD-AENAM").Text '更改
  32. .findById("wnd[0]/mbar/menu[4]/menu[3]").Select '结算规则
  33. arr2(x - 1, 4) = .findById("wnd[0]/usr/tblSAPLKOBSTC_RULES/ctxtCOBRB-KONTY[0,1]").Text 'CTR
  34. .findById("wnd[0]").sendVKey 2 '进入结算规则里
  35. arr2(x - 1, 5) = .findById("wnd[0]/usr/subBLOCK1:SAPLKOBS:0200/txtCOBR_INFO-OBJ_TEXT").Text ' 工单说明
  36. arr2(x - 1, 6) = .findById("wnd[0]/usr/subBLOCK2:SAPLKACB:1014/ctxtCOBL-KOSTL").Text '成本中心
  37. arr2(x - 1, 7) = .findById("wnd[0]/usr/subBLOCK2:SAPLKACB:1014/ctxtCOBL-PS_POSID").Text 'WBS元素
  38. arr2(x - 1, 8) = .findById("wnd[0]/usr/subBLOCK2:SAPLKACB:1014/ctxtCOBL-SAKNR").Text '总账科目
  39. arr2(x - 1, 9) = .findById("wnd[0]/usr/subBLOCK2:SAPLKACB:1014/ctxtCOBL-PRCTR").Text '利润中心
  40. arr2(x - 1, 10) = .findById("wnd[0]/usr/txtCOBRB-PROZS").Text '百分比
  41. Next x
  42. End With
  43. With ThisWorkbook.Sheets("CO03")
  44. .AutoFilterMode = False
  45. With .Cells(1, 2)
  46. .Resize(1, UBound(arr2, 2)).EntireColumn.ClearContents
  47. .Resize(1, UBound(arr2, 2)) = Split("工厂;创建人;更改人;CTR;工单说明;成本中心;WBS元素;总账科目;利润中心;百分比", ";")
  48. .Cells(2, 1).Resize(UBound(arr1), UBound(arr2, 2)) = arr2
  49. End With
  50. End With
  51. End Sub

MM03

MM03查询标估价等

  1. Sub MM03_显示物料()
  2. Dim iMg As VbMsgBoxStyle
  3. iMg = MsgBox("是否显示物料?" & Chr(10) & " " & Chr(10), vbYesNo, "MM03")
  4. If iMg = 7 Then Exit Sub
  5. Dim SapGuiAuto As Object, AppSap As Object, Connection As Object, session As Object
  6. Set SapGuiAuto = GetObject("SAPGUI")
  7. Set AppSap = SapGuiAuto.GetScriptingEngine
  8. Set Connection = AppSap.Children(0)
  9. Set session = Connection.Children(0)
  10. Dim x As Integer, y As Integer, sr As String, rg As Range, arr1(), k As Integer, i As Integer, j As Integer, bl As Boolean
  11. Dim Table As Object, Columns As Object
  12. ReDim arr2(1 To 10000, 1 To 20)
  13. sr = "MM03"
  14. Set rg = ThisWorkbook.Sheets(sr).UsedRange.Find("*")
  15. If rg Is Nothing Then
  16. MsgBox "错误!表【" & sr & "】中无数据!"
  17. Exit Sub
  18. End If
  19. arr1 = rg.CurrentRegion.Value
  20. bl = False
  21. With session
  22. For x = 2 To UBound(arr1)
  23. If arr1(x, 1) = "" Then Exit For
  24. .findById("wnd[0]").maximize
  25. .findById("wnd[0]/tbar[0]/okcd").Text = "/NMM03"
  26. .findById("wnd[0]").sendVKey 0 'Enter
  27. .findById("wnd[0]/usr/ctxtRMMG1-MATNR").Text = arr1(x, 2) '查询物料
  28. .findById("wnd[0]").sendVKey 0
  29. i = 0
  30. j = 0
  31. Do
  32. i = i + 1
  33. sr = "wnd[1]/usr/tblSAPLMGMMTC_VIEW/txtMSICHTAUSW-DYTXT[0," & i & "]"
  34. If .findById(sr, False) Is Nothing Then
  35. bl = True
  36. Exit Do
  37. Else
  38. If .findById(sr).Text = "会计 1" Then
  39. .findById("wnd[1]/usr/tblSAPLMGMMTC_VIEW").getAbsoluteRow(j * 16 + i).Selected = True
  40. .findById("wnd[1]/tbar[0]/btn[0]").press
  41. Exit Do
  42. End If
  43. End If
  44. If i Mod 16 = 0 Then '选择视图最大有16个栏位, 超过要下滑滚动条
  45. .findById("wnd[1]/usr/tblSAPLMGMMTC_VIEW").verticalScrollbar.Position = 16
  46. i = 0
  47. j = j + 1
  48. End If
  49. Loop
  50. If .findById("wnd[0]/sbar/pane[0]").Text <> "" Then '物料查不到下面会有一个警告冒出来
  51. bl = True
  52. Else
  53. sr = "wnd[2]/tbar[0]/btn[0]"
  54. If Not session.findById(sr, False) Is Nothing Then '测试区没有这个错误提示,正式区有
  55. .findById(sr).press '输入工厂前有个错误提示要确定
  56. End If
  57. .findById("wnd[1]/usr/ctxtRMMG1-WERKS").Text = arr1(x, 1)
  58. .findById("wnd[1]/tbar[0]/btn[0]").press
  59. If Not .findById("wnd[2]/usr/txtMESSTXT1", False) Is Nothing Then '查不到某个工厂的物料会有个警告
  60. bl = True
  61. Else
  62. arr2(x - 1, 4) = .findById("wnd[0]/usr/tabsTABSPR1/tabpSP25/ssubTABFRA1:SAPLMGMM:2000/subSUB2:SAPLMGD1:2800/subSUB1:SAPLCKMMAT:0010/tabsTABS/tabpPPLF").Text '会计期间
  63. arr2(x - 1, 5) = .findById("wnd[0]/usr/tabsTABSPR1/tabpSP25/ssubTABFRA1:SAPLMGMM:2000/subSUB2:SAPLMGD1:2800/subSUB1:SAPLCKMMAT:0010/tabsTABS/tabpPPLF/ssubSUBML:SAPLCKMMAT:0100/subSUBCURR:SAPLCKMMAT:0200/txtCKMMAT_DISPLAY-STPRS_1").Text '公司代码货币 标准价格
  64. arr2(x - 1, 6) = .findById("wnd[0]/usr/tabsTABSPR1/tabpSP25/ssubTABFRA1:SAPLMGMM:2000/subSUB2:SAPLMGD1:2800/subSUB1:SAPLCKMMAT:0010/tabsTABS/tabpPPLF/ssubSUBML:SAPLCKMMAT:0100/subSUBCURR:SAPLCKMMAT:0200/txtCKMMAT_DISPLAY-PEINH_1").Text '公司代码货币 价格单位
  65. arr2(x - 1, 7) = .findById("wnd[0]/usr/tabsTABSPR1/tabpSP25/ssubTABFRA1:SAPLMGMM:2000/subSUB2:SAPLMGD1:2800/subSUB1:SAPLCKMMAT:0010/tabsTABS/tabpPPLF/ssubSUBML:SAPLCKMMAT:0100/subSUBCURR:SAPLCKMMAT:0200/txtCKMMAT_DISPLAY-STPRS_2").Text '集团公司记帐货币,利润中心评估
  66. arr2(x - 1, 8) = .findById("wnd[0]/usr/tabsTABSPR1/tabpSP25/ssubTABFRA1:SAPLMGMM:2000/subSUB2:SAPLMGD1:2800/subSUB1:SAPLCKMMAT:0010/tabsTABS/tabpPPLF/ssubSUBML:SAPLCKMMAT:0100/subSUBCURR:SAPLCKMMAT:0200/txtCKMMAT_DISPLAY-PEINH_2").Text '集团公司记帐货币,利润中心评估 价格单位
  67. .findById("wnd[0]/usr/tabsTABSPR1/tabpSP28").Select '成本核算2
  68. arr2(x - 1, 1) = "'" & .findById("wnd[0]/usr/tabsTABSPR1/tabpSP28/ssubTABFRA1:SAPLMGMM:2000/subSUB1:SAPLMGD1:1009/ctxtRMMG1-WERKS").Text '工厂
  69. arr2(x - 1, 2) = .findById("wnd[0]/usr/tabsTABSPR1/tabpSP28/ssubTABFRA1:SAPLMGMM:2000/subSUB1:SAPLMGD1:1009/ctxtRMMG1-MATNR").Text '物料
  70. arr2(x - 1, 3) = .findById("wnd[0]/usr/tabsTABSPR1/tabpSP28/ssubTABFRA1:SAPLMGMM:2000/subSUB1:SAPLMGD1:1009/txtMAKT-MAKTX").Text '描述
  71. arr2(x - 1, 9) = .findById("wnd[0]/usr/tabsTABSPR1/tabpSP28/ssubTABFRA1:SAPLMGMM:2000/subSUB2:SAPLMGD1:2953/txtMBEW-PDATL").Text '会计年度
  72. arr2(x - 1, 10) = .findById("wnd[0]/usr/tabsTABSPR1/tabpSP28/ssubTABFRA1:SAPLMGMM:2000/subSUB2:SAPLMGD1:2953/txtMBEW-PPRDL").Text '期间
  73. arr2(x - 1, 11) = .findById("wnd[0]/usr/tabsTABSPR1/tabpSP28/ssubTABFRA1:SAPLMGMM:2000/subSUB4:SAPLMGD1:2902/ctxtMBEW-BKLAS").Text '评估类
  74. arr2(x - 1, 12) = .findById("wnd[0]/usr/tabsTABSPR1/tabpSP28/ssubTABFRA1:SAPLMGMM:2000/subSUB4:SAPLMGD1:2902/ctxtMBEW-VPRSV").Text '价格控制
  75. arr2(x - 1, 13) = .findById("wnd[0]/usr/tabsTABSPR1/tabpSP28/ssubTABFRA1:SAPLMGMM:2000/subSUB4:SAPLMGD1:2902/txtMBEW-PEINH").Text '价格单位
  76. arr2(x - 1, 14) = .findById("wnd[0]/usr/tabsTABSPR1/tabpSP28/ssubTABFRA1:SAPLMGMM:2000/subSUB2:SAPLMGD1:2953/txtMBEW-LPLPR").Text '计划价格
  77. arr2(x - 1, 15) = .findById("wnd[0]/usr/tabsTABSPR1/tabpSP28/ssubTABFRA1:SAPLMGMM:2000/subSUB2:SAPLMGD1:2953/txtMBEW-STPRS").Text '标准价格
  78. arr2(x - 1, 16) = .findById("wnd[0]/usr/tabsTABSPR1/tabpSP28/ssubTABFRA1:SAPLMGMM:2000/subSUB3:SAPLMGD1:2952/txtMBEW-ZPLP1").Text '计划价格1
  79. arr2(x - 1, 17) = .findById("wnd[0]/usr/tabsTABSPR1/tabpSP28/ssubTABFRA1:SAPLMGMM:2000/subSUB3:SAPLMGD1:2952/ctxtMBEW-ZPLD1").Text '计划价格日期1
  80. arr2(x - 1, 18) = .findById("wnd[0]/usr/tabsTABSPR1/tabpSP28/ssubTABFRA1:SAPLMGMM:2000/subSUB2:SAPLMGD1:2953/txtMBEW-PPRDZ").Text '将来期间
  81. arr2(x - 1, 19) = .findById("wnd[0]/usr/tabsTABSPR1/tabpSP28/ssubTABFRA1:SAPLMGMM:2000/subSUB2:SAPLMGD1:2953/txtMBEW-PDATZ").Text '将来年份
  82. arr2(x - 1, 20) = .findById("wnd[0]/usr/tabsTABSPR1/tabpSP28/ssubTABFRA1:SAPLMGMM:2000/subSUB2:SAPLMGD1:2953/txtMBEW-ZPLPR").Text '将来价格
  83. End If
  84. End If
  85. Next x
  86. End With
  87. With ThisWorkbook.Sheets("MM03")
  88. .AutoFilterMode = False
  89. With .Cells(1, 3)
  90. .Resize(1, UBound(arr2, 2)).EntireColumn.ClearContents
  91. .Resize(1, UBound(arr2, 2)) = Split("工厂;物料;描述;会计期间;公司标准价;公司价格单位;利润中心标准价;利润中心价格单位;会计年度;期间;评估类;价格控制;价格单位;计划价格;标准价格;计划价格1;计划价格日期1;将来期间;将来年份;将来价格", ";")
  92. .Cells(2, 1).Resize(UBound(arr1), UBound(arr2, 2)) = arr2
  93. End With
  94. End With
  95. If bl Then
  96. MsgBox "注意!有物料没查到!"
  97. Else
  98. MsgBox "成功"
  99. End If
  100. End Sub

CS15

CS15查询多个料号的BOM

  1. Sub CS15_单层反查清单_多层()
  2. Dim iMg As VbMsgBoxStyle
  3. iMg = MsgBox("是否显示 CS15?" & Chr(10) & " " & Chr(10), vbYesNo, "CS15 - 单层反查清单")
  4. If iMg = 7 Then Exit Sub
  5. Dim SapGuiAuto As Object, AppSap As Object, Connection As Object, session As Object
  6. Set SapGuiAuto = GetObject("SAPGUI")
  7. Set AppSap = SapGuiAuto.GetScriptingEngine
  8. Set Connection = AppSap.Children(0)
  9. Set session = Connection.Children(0)
  10. Dim x As Integer, y As Integer, z As Integer, sr As String, rg As Range, arr1(), k As Integer, brr(), bl As Boolean
  11. Dim Table As Object, Columns As Object
  12. ReDim arr2(1 To 100000, 1 To 15) '此处要提前知道列数,并且加了一列 'Table.ColumnCount()
  13. ReDim brr(1 To 15)
  14. sr = "CS15"
  15. Set rg = ThisWorkbook.Sheets(sr).UsedRange.Find("*")
  16. If rg Is Nothing Then
  17. MsgBox "错误!表【" & sr & "】中无数据!"
  18. Exit Sub
  19. End If
  20. arr1 = rg.CurrentRegion.Value
  21. brr(1) = "物料"
  22. With session
  23. For z = 2 To UBound(arr1)
  24. If arr1(z, 1) = "" Then Exit For
  25. .findById("wnd[0]").maximize
  26. .findById("wnd[0]/tbar[0]/okcd").Text = "/NCS15"
  27. .findById("wnd[0]").sendVKey 0 'Enter
  28. .findById("wnd[0]/usr/ctxtRC29L-DATUV").Text = TheTime(0, "yyyy.mm.dd")
  29. .findById("wnd[0]/usr/ctxtRC29L-MATNR").Text = arr1(z, 2) '物料
  30. .findById("wnd[0]/usr/chkRC29L-DIRKT").Selected = True
  31. .findById("wnd[0]/tbar[1]/btn[5]").press
  32. .findById("wnd[0]/usr/ctxtRC29L-WERKS").Text = arr1(z, 1) '工厂
  33. .findById("wnd[0]/usr/chkRC29L-MEHRS").Selected = True '多层
  34. .findById("wnd[0]/tbar[1]/btn[8]").press
  35. If .findById("wnd[0]/sbar/pane[0]").Text <> "" Then
  36. bl = True
  37. k = k + 1
  38. arr2(k, 1) = arr1(z, 2)
  39. arr2(k, 4) = arr1(z, 1)
  40. arr2(k, 5) = .findById("wnd[0]/sbar/pane[0]").Text
  41. Else
  42. Set Table = .findById("wnd[0]/usr/cntlGRID1/shellcont/shell/shellcont[1]/shell")
  43. Set Columns = Table.ColumnOrder()
  44. For x = 0 To Table.RowCount() - 1
  45. k = k + 1
  46. arr2(k, 1) = arr1(z, 2)
  47. For y = 0 To Table.ColumnCount() - 1
  48. arr2(k, y + 2) = Table.getcellvalue(x, CStr(Columns(y)))
  49. Next y
  50. If x Mod 39 = 0 Then 'bom 测试是每39行后要刷一次屏,否则导出的数据是空白
  51. Table.SetCurrentCell x, CStr(Columns(0))
  52. Table.firstVisibleRow = x
  53. End If
  54. Next x
  55. For y = 0 To Table.ColumnCount() - 1
  56. brr(y + 2) = CStr(Columns(y)) '目前关闭
  57. Next y
  58. End If
  59. Next z
  60. End With
  61. For x = 1 To k
  62. arr2(x, 4) = "'" & arr2(x, 4)
  63. Next x
  64. With ThisWorkbook.Sheets("CS15")
  65. .AutoFilterMode = False
  66. With .Cells(1, 3)
  67. .Resize(1, UBound(arr2, 2)).EntireColumn.ClearContents
  68. .Resize(1, UBound(arr2, 2)) = brr '目前没用
  69. .Resize(1, UBound(arr2, 2)) = Split("物料;级别;物料清单用途;工厂;对象;对象标识;备选物料清单;项目编号;超出需求数量;需求数量;组件计量单位;ResQ excess;重计划数量;基本计量单位;对象描述", ";")
  70. If k > 0 Then .Cells(2, 1).Resize(k, UBound(arr2, 2)) = arr2
  71. End With
  72. End With
  73. If bl Then
  74. MsgBox "注意!有部分没有查到!"
  75. Else
  76. MsgBox "成功"
  77. End If
  78. End Sub

TEST

测试运行,读取Shell

  1. Sub test()
  2. Dim SapGuiAuto As Object, AppSap As Object, Connection As Object, session As Object
  3. Set SapGuiAuto = GetObject("SAPGUI")
  4. Set AppSap = SapGuiAuto.GetScriptingEngine
  5. Set Connection = AppSap.Children(0)
  6. Set session = Connection.Children(0)
  7. Dim Table As Object, Columns As Object
  8. Dim x As Integer, y As Integer, k As Integer, arr(), Title()
  9. ReDim arr(1 To 100000, 1 To 15)
  10. ReDim Title(1 To 15)
  11. With session
  12. Set Table = .findById("wnd[0]/usr/cntlFDBL_BALANCE_CONTAINER/shellcont/shell") '把表shell赋值给Table
  13. Set Columns = Table.ColumnOrder() '取列
  14. For x = 0 To Table.RowCount() - 1 'Table.RowCount表示为总行数
  15. k = k + 1
  16. For y = 0 To Table.ColumnCount() - 1 'Table.ColumnCount表示总列数Table.ColumnCount
  17. arr(k, y + 1) = Table.getcellvalue(x, CStr(Columns(y))) '取值
  18. Next y
  19. Next x
  20. For y = 0 To Table.ColumnCount() - 1
  21. Title(y + 1) = CStr(Columns(y)) 'Columns返回标题文本
  22. Next y
  23. End With
  24. With ThisWorkbook.Sheets("test")
  25. .AutoFilterMode = False
  26. .Cells.ClearContents
  27. .Cells(1, 1).Resize(1, UBound(arr, 2)) = Title
  28. If k > 0 Then .Cells(2, 1).Resize(k, UBound(arr, 2)) = arr
  29. End With
  30. End Sub

KS13

KS13用Excel导出的方式批量读取成本中心

  1. Sub KS13_显示成本中心()
  2. Dim iMg As VbMsgBoxStyle
  3. iMg = MsgBox("是否显示成本中心?" & Chr(10) & " " & Chr(10), vbYesNo, "KS13")
  4. If iMg = 7 Then Exit Sub
  5. Dim SapGuiAuto As Object, AppSap As Object, Connection As Object, session As Object
  6. Set SapGuiAuto = GetObject("SAPGUI")
  7. Set AppSap = SapGuiAuto.GetScriptingEngine
  8. Set Connection = AppSap.Children(0)
  9. Set session = Connection.Children(0)
  10. Dim x As Long, y As Integer, z As Integer, sr As String, rg As Range, bl As Boolean, wb As Workbook, j As Integer
  11. Dim arr1(), arr2(), arr3(), k As Long
  12. ReDim arr3(1 To 100000, 1 To 23)
  13. sr = "KS13"
  14. Set rg = ThisWorkbook.Sheets(sr).UsedRange.Find("*")
  15. If rg Is Nothing Then
  16. MsgBox "错误!表【" & sr & "】中无数据!"
  17. Exit Sub
  18. End If
  19. arr1 = rg.CurrentRegion.Value
  20. Call KillSapPath
  21. With session
  22. For z = 2 To UBound(arr1)
  23. If arr1(z, 1) = "" Then Exit For
  24. .findById("wnd[0]").maximize
  25. .findById("wnd[0]/tbar[0]/okcd").Text = "/NKS13"
  26. .findById("wnd[0]").sendVKey 0 'Enter
  27. .findById("wnd[0]/usr/subKOSTL_SELECTION:SAPLKMS1:0100/radKMAS_D-KZKOSTL").Select
  28. .findById("wnd[0]/usr/subKOSTL_SELECTION:SAPLKMS1:0100/ctxtKMAS_D-KOSTL").Text = "" '成本中心
  29. .findById("wnd[0]/usr/subKOSTL_SELECTION:SAPLKMS1:0100/radKMAS_D-KZVARIANT").Select
  30. .findById("wnd[0]/usr/subKOSTL_SELECTION:SAPLKMS1:0100/ctxtKMAS_D-VARIANT_KS").Text = "" '选择变式
  31. .findById("wnd[0]/usr/ctxtCSKSZ-DATAB_ANFO").Text = TheTime(0, "yyyy.mm.01")
  32. .findById("wnd[0]/usr/subKOSTL_SELECTION:SAPLKMS1:0100/radKMAS_D-KZKOSTLSET").Select
  33. .findById("wnd[0]/usr/subKOSTL_SELECTION:SAPLKMS1:0100/ctxtKMAS_D-KOSTL_SET").Text = arr1(z, 1) '成本中心组
  34. .findById("wnd[0]/tbar[1]/btn[8]").press '执行
  35. bl = True
  36. sr = "wnd[0]/sbar/pane[0]"
  37. If .findById(sr, False) Is Nothing Then
  38. If Right(.findById(sr), 3) <> "不存在" Then
  39. bl = False
  40. End If
  41. End If
  42. If bl Then
  43. If Not .findById("wnd[0]/usr/cntlGRID1/shellcont/shell/shellcont[1]/shell", False) Is Nothing Then
  44. .findById("wnd[0]/usr/cntlGRID1/shellcont/shell/shellcont[1]/shell").contextMenu
  45. .findById("wnd[0]/usr/cntlGRID1/shellcont/shell/shellcont[1]/shell").selectContextMenuItem "&XXL"
  46. .findById("wnd[1]/tbar[0]/btn[0]").press
  47. .findById("wnd[1]/usr/ctxtDY_PATH").Text = SapPath()
  48. j = j + 1 '每次命名的文件不一致
  49. .findById("wnd[1]/usr/ctxtDY_FILENAME").Text = j & ".XLSX"
  50. .findById("wnd[1]/tbar[0]/btn[0]").press
  51. Set wb = Workbooks.Open(SapPath() & "/" & j & ".XLSX") '对文件取值
  52. arr2 = wb.Sheets(1).Range("A1").CurrentRegion.Value
  53. wb.Close
  54. Set wb = Nothing
  55. For x = 2 To UBound(arr2)
  56. k = k + 1
  57. arr3(k, 1) = arr1(z, 1)
  58. For y = 1 To UBound(arr2, 2)
  59. arr3(k, y + 1) = arr2(x, y)
  60. Next y
  61. Next x
  62. End If
  63. End If
  64. Next z
  65. End With
  66. With ThisWorkbook.Sheets("KS13")
  67. .AutoFilterMode = False
  68. With .Cells(1, 2)
  69. .Resize(1, UBound(arr3, 2)).EntireColumn.ClearContents
  70. .Resize(1, UBound(arr3, 2)) = Split("成本中心组;成本中心;部门编码;名称;描述;负责人;部门;利润中心;公司代码;数据线;打印机所在地;货币;CostCtrCat;功能范围;有效期自;有效期至;计划: 次成本(锁标识);计划: 收入(锁标识);计划: 主成本(锁标识);实际: 收入 (锁标识);实际: 主成本(锁标识);实际:次收入 (锁标识);成本核算表", ";")
  71. If k > 0 Then .Cells(2, 1).Resize(k, UBound(arr3, 2)) = arr3
  72. End With
  73. End With
  74. MsgBox "完成!"
  75. End Sub

KSH1

KSH1建立成本中心组

  1. Sub KSH1_创建成本中心组()
  2. Dim iMg As VbMsgBoxStyle
  3. iMg = MsgBox("是否创建成本中心组?" & Chr(10) & " " & Chr(10) & "创建之前要自行检查下是否确实需要创建!", vbYesNo, "KSH1")
  4. If iMg = 7 Then Exit Sub
  5. Dim x As Integer, y As Integer, rg As Range, sr As String, sg As String, v, u, i As Integer, j As Integer, k As Integer
  6. sr = "KSH1"
  7. Set rg = ThisWorkbook.Sheets(sr).UsedRange.Find("*")
  8. If rg Is Nothing Then
  9. MsgBox "错误!表【" & sr & "】中无数据!"
  10. Exit Sub
  11. End If
  12. Dim arr()
  13. arr = rg.CurrentRegion.Value
  14. Dim dZ As Object
  15. Set dZ = CreateObject("scripting.dictionary")
  16. For x = 1 To UBound(arr, 2)
  17. dZ(arr(1, x)) = x
  18. Next x
  19. Dim a As Byte, b As Byte, c As Byte, d As Byte
  20. a = dZ("成本中心组")
  21. b = dZ("成本中心组名称")
  22. c = dZ("成本中心")
  23. d = dZ("成本中心名称")
  24. Dim dic1 As Object, dic2 As Object
  25. Set dic1 = CreateObject("scripting.dictionary")
  26. Set dic2 = CreateObject("scripting.dictionary")
  27. For x = 2 To UBound(arr)
  28. sr = arr(x, a)
  29. sg = arr(x, c)
  30. If Not dic1.exists(sr) Then
  31. Set dic1(sr) = CreateObject("scripting.dictionary")
  32. End If
  33. dic1(sr)(sg) = ""
  34. Next x
  35. For x = 2 To UBound(arr)
  36. sr = arr(x, a)
  37. sg = arr(x, b)
  38. dic2(sr) = sg
  39. Next x
  40. Dim SapGuiAuto As Object, AppSap As Object, Connection As Object, session As Object, Position
  41. Set SapGuiAuto = GetObject("SAPGUI")
  42. Set AppSap = SapGuiAuto.GetScriptingEngine
  43. Set Connection = AppSap.Children(0)
  44. Set session = Connection.Children(0)
  45. With session
  46. For Each v In dic1.keys
  47. .findById("wnd[0]").maximize
  48. .findById("wnd[0]/tbar[0]/okcd").Text = "/NKSH1"
  49. .findById("wnd[0]").sendVKey 0 'Enter
  50. If Not .findById("wnd[1]/usr/sub:SAPLSPO4:0300/ctxtSVALD-VALUE[0,21]", False) Is Nothing Then '检测是否需要输入控制范围
  51. .findById("wnd[1]/usr/sub:SAPLSPO4:0300/ctxtSVALD-VALUE[0,21]").Text = 2688
  52. .findById("wnd[0]").sendVKey 0
  53. End If
  54. .findById("wnd[0]/usr/ctxtGRPDYNP-NAME_COALL").Text = v
  55. .findById("wnd[0]").sendVKey 0
  56. If Not .findById("wnd[1]/usr/btnBUTTON_2", False) Is Nothing Then '检测是否被创建
  57. .findById("wnd[1]/usr/btnBUTTON_2").press
  58. MsgBox "失败!【" & v & "】已经被创建!"
  59. Exit Sub
  60. End If
  61. .findById("wnd[0]/usr/txt[16,0]").Text = dic2(CStr(v))
  62. i = 1 '记录屏幕上的输入框行数,跨页要重置
  63. j = 0 '计算点击“插入成本中心”的次数
  64. k = 0 '计算“竖向滚动条”下拉的频次
  65. Do
  66. j = j + 1
  67. .findById("wnd[0]/tbar[1]/btn[16]").press
  68. Loop Until j = dic1(CStr(v)).Count \ 5 + 1 '点击“插入成本中心”一次可以填五个成本中心
  69. For Each u In dic1(CStr(v)).keys
  70. i = i + 1
  71. .findById("wnd[0]/usr/txt[4," & i & "]").Text = u
  72. If i Mod 29 = 0 Then '测试DELL电脑最大显示的成本中心个数,到46时 i 会报错,要填45;测试华硕电脑是要写29
  73. k = k + 1
  74. .findById("wnd[0]/usr").verticalScrollbar.Position = i * k
  75. i = 0
  76. End If
  77. Next u
  78. .findById("wnd[0]/tbar[0]/btn[11]").press '保存(慎用)
  79. Next v
  80. End With
  81. MsgBox "成功!"
  82. End Sub

KSH2

KSH2修改成本中心组

  1. Sub KSH2_标题()
  2. Dim arr() As String
  3. arr = Split("成本中心组;成本中心组名称;成本中心;成本中心名称", ";")
  4. With ThisWorkbook.Sheets("KSH2")
  5. .AutoFilterMode = False
  6. .Cells(1, 1).Resize(1, UBound(arr) + 1) = arr
  7. End With
  8. End Sub
  9. Sub KSH2_修改成本中心组_重置() '会修改成本中心组名称
  10. Dim iMg As VbMsgBoxStyle
  11. iMg = MsgBox("是否修改成本中心组?" & Chr(10) & " " & Chr(10) & "修改之前要检查成本中心组是否已经创建,否则会出现修改一半进行不下去的情况!", vbYesNo, "KSH2")
  12. If iMg = 7 Then Exit Sub
  13. Dim x As Integer, y As Integer, rg As Range, sr As String, sg As String, v, u, i As Integer, j As Integer, k As Integer
  14. sr = "KSH2"
  15. Set rg = ThisWorkbook.Sheets(sr).UsedRange.Find("*")
  16. If rg Is Nothing Then
  17. MsgBox "错误!表【" & sr & "】中无数据!"
  18. Exit Sub
  19. End If
  20. Dim arr()
  21. arr = rg.CurrentRegion.Value
  22. Dim dZ As Object
  23. Set dZ = CreateObject("scripting.dictionary")
  24. For x = 1 To UBound(arr, 2)
  25. dZ(arr(1, x)) = x
  26. Next x
  27. Dim a As Byte, b As Byte, c As Byte, d As Byte
  28. a = dZ("成本中心组")
  29. b = dZ("成本中心组名称")
  30. c = dZ("成本中心")
  31. d = dZ("成本中心名称")
  32. Dim dic1 As Object, dic2 As Object
  33. Set dic1 = CreateObject("scripting.dictionary")
  34. Set dic2 = CreateObject("scripting.dictionary")
  35. For x = 2 To UBound(arr)
  36. sr = arr(x, a)
  37. sg = arr(x, c)
  38. If Not dic1.exists(sr) Then
  39. Set dic1(sr) = CreateObject("scripting.dictionary")
  40. End If
  41. dic1(sr)(sg) = ""
  42. Next x
  43. For x = 2 To UBound(arr)
  44. sr = arr(x, a)
  45. sg = arr(x, b)
  46. dic2(sr) = sg
  47. Next x
  48. Dim SapGuiAuto As Object, AppSap As Object, Connection As Object, session As Object, Position
  49. Set SapGuiAuto = GetObject("SAPGUI")
  50. Set AppSap = SapGuiAuto.GetScriptingEngine
  51. Set Connection = AppSap.Children(0)
  52. Set session = Connection.Children(0)
  53. With session
  54. For Each v In dic1.keys
  55. .findById("wnd[0]").maximize
  56. .findById("wnd[0]/tbar[0]/okcd").Text = "/NKSH2"
  57. .findById("wnd[0]").sendVKey 0 'Enter
  58. If Not .findById("wnd[1]/usr/sub:SAPLSPO4:0300/ctxtSVALD-VALUE[0,21]", False) Is Nothing Then '检测是否需要输入控制范围
  59. .findById("wnd[1]/usr/sub:SAPLSPO4:0300/ctxtSVALD-VALUE[0,21]").Text = 2688
  60. .findById("wnd[0]").sendVKey 0
  61. End If
  62. .findById("wnd[0]/usr/ctxtGRPDYNP-NAME_COALL").Text = v
  63. .findById("wnd[0]").sendVKey 0
  64. If Not .findById("wnd[1]/usr/btnBUTTON_2", False) Is Nothing Then '检测是否有成本中心组
  65. .findById("wnd[1]/usr/btnBUTTON_2").press
  66. MsgBox "失败!【" & v & "】还没创建!"
  67. Exit Sub
  68. End If
  69. .findById("wnd[0]/usr/txt[16,0]").Text = dic2(CStr(v))
  70. Do '删除组下面所有的成本中心
  71. If .findById("wnd[0]/usr/lbl[4,2]", False) Is Nothing Then Exit Do
  72. .findById("wnd[0]/usr/lbl[4,2]").SetFocus
  73. .findById("wnd[0]/tbar[1]/btn[9]").press
  74. .findById("wnd[0]/tbar[1]/btn[5]").press
  75. Loop
  76. i = 1 '记录屏幕上的输入框行数,跨页要重置
  77. j = 0 '计算点击“插入成本中心”的次数
  78. k = 0 '计算“竖向滚动条”下拉的频次
  79. Do
  80. j = j + 1
  81. .findById("wnd[0]/tbar[1]/btn[16]").press
  82. Loop Until j = dic1(CStr(v)).Count \ 5 + 1 '点击“插入成本中心”一次可以填五个成本中心
  83. For Each u In dic1(CStr(v)).keys
  84. i = i + 1
  85. .findById("wnd[0]/usr/txt[4," & i & "]").Text = u
  86. If i Mod 29 = 0 Then '测试DELL电脑最大显示的成本中心个数,到46时 i 会报错,要填45;测试华硕电脑是要写29
  87. k = k + 1
  88. .findById("wnd[0]/usr").verticalScrollbar.Position = i * k
  89. i = 0
  90. End If
  91. Next u
  92. .findById("wnd[0]/tbar[0]/btn[11]").press '保存(慎用)
  93. Next v
  94. End With
  95. MsgBox "成功!"
  96. End Sub
  97. Sub KSH2_修改成本中心组_新增() '不会改成本中心组名称
  98. Dim iMg As VbMsgBoxStyle
  99. iMg = MsgBox("是否修改成本中心组?" & Chr(10) & " " & Chr(10) & "修改之前要检查成本中心组是否已经创建,否则会出现修改一半进行不下去的情况!", vbYesNo, "KSH2")
  100. If iMg = 7 Then Exit Sub
  101. Dim x As Integer, y As Integer, rg As Range, sr As String, sg As String, v, u, i As Integer, j As Integer, k As Integer
  102. sr = "KSH2"
  103. Set rg = ThisWorkbook.Sheets(sr).UsedRange.Find("*")
  104. If rg Is Nothing Then
  105. MsgBox "错误!表【" & sr & "】中无数据!"
  106. Exit Sub
  107. End If
  108. Dim arr()
  109. arr = rg.CurrentRegion.Value
  110. Dim dZ As Object
  111. Set dZ = CreateObject("scripting.dictionary")
  112. For x = 1 To UBound(arr, 2)
  113. dZ(arr(1, x)) = x
  114. Next x
  115. Dim a As Byte, b As Byte, c As Byte, d As Byte
  116. a = dZ("成本中心组")
  117. b = dZ("成本中心组名称")
  118. c = dZ("成本中心")
  119. d = dZ("成本中心名称")
  120. Dim dic1 As Object
  121. Set dic1 = CreateObject("scripting.dictionary")
  122. For x = 2 To UBound(arr)
  123. sr = arr(x, a)
  124. sg = arr(x, c)
  125. If Not dic1.exists(sr) Then
  126. Set dic1(sr) = CreateObject("scripting.dictionary")
  127. End If
  128. dic1(sr)(sg) = ""
  129. Next x
  130. Dim SapGuiAuto As Object, AppSap As Object, Connection As Object, session As Object, Position
  131. Set SapGuiAuto = GetObject("SAPGUI")
  132. Set AppSap = SapGuiAuto.GetScriptingEngine
  133. Set Connection = AppSap.Children(0)
  134. Set session = Connection.Children(0)
  135. With session
  136. For Each v In dic1.keys
  137. .findById("wnd[0]").maximize
  138. .findById("wnd[0]/tbar[0]/okcd").Text = "/NKSH2"
  139. .findById("wnd[0]").sendVKey 0 'Enter
  140. If Not .findById("wnd[1]/usr/sub:SAPLSPO4:0300/ctxtSVALD-VALUE[0,21]", False) Is Nothing Then '检测是否需要输入控制范围
  141. .findById("wnd[1]/usr/sub:SAPLSPO4:0300/ctxtSVALD-VALUE[0,21]").Text = 2688
  142. .findById("wnd[0]").sendVKey 0
  143. End If
  144. .findById("wnd[0]/usr/ctxtGRPDYNP-NAME_COALL").Text = v
  145. .findById("wnd[0]").sendVKey 0
  146. If Not .findById("wnd[1]/usr/btnBUTTON_2", False) Is Nothing Then '检测是否有成本中心组
  147. .findById("wnd[1]/usr/btnBUTTON_2").press
  148. MsgBox "失败!【" & v & "】还没创建!"
  149. Exit Sub
  150. End If
  151. i = 1 '记录屏幕上的输入框行数,跨页要重置
  152. j = 0 '计算点击“插入成本中心”的次数
  153. k = 0 '计算“竖向滚动条”下拉的频次
  154. Do
  155. j = j + 1
  156. .findById("wnd[0]/tbar[1]/btn[16]").press
  157. Loop Until j = dic1(CStr(v)).Count \ 5 + 1 '点击“插入成本中心”一次可以填五个成本中心
  158. For Each u In dic1(CStr(v)).keys
  159. i = i + 1
  160. .findById("wnd[0]/usr/txt[4," & i & "]").Text = u
  161. If i Mod 29 = 0 Then '测试DELL电脑最大显示的成本中心个数,到46时 i 会报错,要填45;测试华硕电脑是要写29
  162. k = k + 1
  163. .findById("wnd[0]/usr").verticalScrollbar.Position = i * k
  164. i = 0
  165. End If
  166. Next u
  167. .findById("wnd[0]/tbar[0]/btn[11]").press '保存(慎用)
  168. Next v
  169. End With
  170. MsgBox "成功!"
  171. End Sub
  172. Sub KSH2_修改成本中心组_删除() '不会改成本中心组名称
  173. Dim iMg As VbMsgBoxStyle
  174. iMg = MsgBox("是否修改成本中心组?" & Chr(10) & " " & Chr(10) & "修改之前要检查成本中心组是否已经创建,否则会出现修改一半进行不下去的情况!", vbYesNo, "KSH2")
  175. If iMg = 7 Then Exit Sub
  176. Dim x As Integer, y As Integer, rg As Range, sr As String, sg As String, v, u, i As Integer, j As Integer, k As Integer
  177. sr = "KSH2"
  178. Set rg = ThisWorkbook.Sheets(sr).UsedRange.Find("*")
  179. If rg Is Nothing Then
  180. MsgBox "错误!表【" & sr & "】中无数据!"
  181. Exit Sub
  182. End If
  183. Dim arr()
  184. arr = rg.CurrentRegion.Value
  185. Dim dZ As Object
  186. Set dZ = CreateObject("scripting.dictionary")
  187. For x = 1 To UBound(arr, 2)
  188. dZ(arr(1, x)) = x
  189. Next x
  190. Dim a As Byte, b As Byte, c As Byte, d As Byte
  191. a = dZ("成本中心组")
  192. b = dZ("成本中心组名称")
  193. c = dZ("成本中心")
  194. d = dZ("成本中心名称")
  195. Dim dic1 As Object
  196. Set dic1 = CreateObject("scripting.dictionary")
  197. For x = 2 To UBound(arr)
  198. sr = arr(x, a)
  199. sg = arr(x, c)
  200. If Not dic1.exists(sr) Then
  201. Set dic1(sr) = CreateObject("scripting.dictionary")
  202. End If
  203. dic1(sr)(sg) = ""
  204. Next x
  205. Dim SapGuiAuto As Object, AppSap As Object, Connection As Object, session As Object, Position
  206. Set SapGuiAuto = GetObject("SAPGUI")
  207. Set AppSap = SapGuiAuto.GetScriptingEngine
  208. Set Connection = AppSap.Children(0)
  209. Set session = Connection.Children(0)
  210. With session
  211. For Each v In dic1.keys
  212. .findById("wnd[0]").maximize
  213. .findById("wnd[0]/tbar[0]/okcd").Text = "/NKSH2"
  214. .findById("wnd[0]").sendVKey 0 'Enter
  215. If Not .findById("wnd[1]/usr/sub:SAPLSPO4:0300/ctxtSVALD-VALUE[0,21]", False) Is Nothing Then '检测是否需要输入控制范围
  216. .findById("wnd[1]/usr/sub:SAPLSPO4:0300/ctxtSVALD-VALUE[0,21]").Text = 2688
  217. .findById("wnd[0]").sendVKey 0
  218. End If
  219. .findById("wnd[0]/usr/ctxtGRPDYNP-NAME_COALL").Text = v
  220. .findById("wnd[0]").sendVKey 0
  221. If Not .findById("wnd[1]/usr/btnBUTTON_2", False) Is Nothing Then '检测是否有成本中心组
  222. .findById("wnd[1]/usr/btnBUTTON_2").press
  223. MsgBox "失败!【" & v & "】还没创建!"
  224. Exit Sub
  225. End If
  226. i = 1 '记录屏幕上的输入框行数,跨页要重置
  227. j = 0 '计算点击“插入成本中心”的次数
  228. Do
  229. i = i + 1
  230. If .findById("wnd[0]/usr/lbl[4," & i & "]", False) Is Nothing Then Exit Do '没有查到的就退出
  231. If .findById("wnd[0]/usr/lbl[15," & i & "]", False) Is Nothing Then Exit Do '没有查到的就退出
  232. sr = .findById("wnd[0]/usr/lbl[4," & i & "]").Text
  233. If dic1(CStr(v)).exists(sr) Then
  234. .findById("wnd[0]/usr/lbl[4," & i & "]").SetFocus
  235. .findById("wnd[0]/tbar[1]/btn[9]").press
  236. .findById("wnd[0]/tbar[1]/btn[5]").press
  237. i = i - 1
  238. End If
  239. If i Mod 29 = 0 Then '测试DELL电脑最大显示的成本中心个数,到46时 i 会报错,要填45;测试华硕电脑是要写29
  240. j = j + 1
  241. .findById("wnd[0]/usr").verticalScrollbar.Position = i * j
  242. i = 0
  243. End If
  244. Loop
  245. .findById("wnd[0]/tbar[0]/btn[11]").press '保存(慎用)
  246. Next v
  247. End With
  248. MsgBox "成功!"
  249. End Sub

KSH3

KSH3显示成本中心组

  1. Sub KSH3_显示成本中心组()
  2. Dim iMg As VbMsgBoxStyle
  3. iMg = MsgBox("是否显示成本中心组?" & Chr(10) & " " & Chr(10), vbYesNo, "KSH3")
  4. If iMg = 7 Then Exit Sub
  5. Dim x As Integer, sr As String, rg As Range, arr1(), arr2(), brr(), k As Integer, i As Integer, j As Integer, bl As Boolean
  6. ReDim arr2(1 To 100000, 1 To 5)
  7. ReDim brr(1 To 2)
  8. sr = "KSH3"
  9. Set rg = ThisWorkbook.Sheets(sr).UsedRange.Find("*")
  10. If rg Is Nothing Then
  11. MsgBox "错误!表【" & sr & "】中无数据!"
  12. Exit Sub
  13. End If
  14. arr1 = rg.CurrentRegion.Value
  15. Dim SapGuiAuto As Object, AppSap As Object, Connection As Object, session As Object, Position
  16. Set SapGuiAuto = GetObject("SAPGUI")
  17. Set AppSap = SapGuiAuto.GetScriptingEngine
  18. Set Connection = AppSap.Children(0)
  19. Set session = Connection.Children(0)
  20. bl = False
  21. With session
  22. For x = 2 To UBound(arr1)
  23. If arr1(x, 1) <> "" Then
  24. .findById("wnd[0]").maximize
  25. .findById("wnd[0]/tbar[0]/okcd").Text = "/NKSH3" '显示成本中心组
  26. .findById("wnd[0]").sendVKey 0 'Enter
  27. If Not .findById("wnd[1]/usr/sub:SAPLSPO4:0300/ctxtSVALD-VALUE[0,21]", False) Is Nothing Then '检测是否需要输入控制范围
  28. .findById("wnd[1]/usr/sub:SAPLSPO4:0300/ctxtSVALD-VALUE[0,21]").Text = 2688
  29. .findById("wnd[0]").sendVKey 0
  30. End If
  31. .findById("wnd[0]/usr/ctxtGRPDYNP-NAME_COALL").Text = arr1(x, 1) '查询成本中心组
  32. .findById("wnd[0]").sendVKey 0 'Enter
  33. If Not .findById("wnd[1]/usr/btnBUTTON_2", False) Is Nothing Then '检测是否有成本中心组
  34. .findById("wnd[1]/usr/btnBUTTON_2").press
  35. bl = True
  36. Else
  37. brr(1) = .findById("wnd[0]/usr/lbl[0,0]").Text '成本中心组名称
  38. brr(2) = .findById("wnd[0]/usr/lbl[16,0]").Text '成本中心组描述
  39. i = 1 '记录屏幕上的输入框行数,跨页要重置
  40. j = 0 '计算点击“插入成本中心”的次数
  41. Do
  42. i = i + 1
  43. If .findById("wnd[0]/usr/lbl[4," & i & "]", False) Is Nothing Then Exit Do '没有查到的就退出
  44. If .findById("wnd[0]/usr/lbl[15," & i & "]", False) Is Nothing Then Exit Do '没有查到的就退出
  45. k = k + 1
  46. arr2(k, 1) = brr(1)
  47. arr2(k, 2) = brr(2)
  48. arr2(k, 3) = .findById("wnd[0]/usr/lbl[4," & i & "]").Text
  49. arr2(k, 4) = .findById("wnd[0]/usr/lbl[15," & i & "]").Text
  50. If i Mod 29 = 0 Then '测试DELL电脑最大显示的成本中心个数,到46时 i 会报错,要填45;测试华硕电脑是要写29
  51. j = j + 1
  52. .findById("wnd[0]/usr").verticalScrollbar.Position = i * j
  53. i = 0
  54. End If
  55. Loop
  56. End If
  57. End If
  58. Next x
  59. For x = 1 To k
  60. If IsNumeric(Right(arr2(x, 3), 1)) Then
  61. arr2(x, 5) = False
  62. Else
  63. arr2(x, 5) = True
  64. End If
  65. Next x
  66. End With
  67. With ThisWorkbook.Sheets("KSH3")
  68. .AutoFilterMode = False
  69. With .Cells(1, 2)
  70. .Resize(1, UBound(arr2, 2)).EntireColumn.ClearContents
  71. .Resize(1, UBound(arr2, 2)) = Split("成本中心组;成本中心组名称;成本中心;成本中心名称;虚拟否", ";")
  72. If k > 0 Then .Cells(2, 1).Resize(k, UBound(arr2, 2)) = arr2
  73. End With
  74. End With
  75. If bl Then
  76. MsgBox "有成本中心组未查到!"
  77. Else
  78. MsgBox "成功!"
  79. End If
  80. End Sub

FS00

  1. Sub FS00_整理()
  2. Dim iMg As VbMsgBoxStyle
  3. iMg = MsgBox("FS00获取科目!" & Chr(10) & " " & Chr(10), vbYesNo, "FSOO")
  4. If iMg = 7 Then Exit Sub
  5. Dim SapGuiAuto As Object, AppSap As Object, Connection As Object, session As Object
  6. Set SapGuiAuto = GetObject("SAPGUI")
  7. Set AppSap = SapGuiAuto.GetScriptingEngine
  8. Set Connection = AppSap.Children(0)
  9. Set session = Connection.Children(0)
  10. Dim Table As Object, GetNodeK As Object, arr(), i As Integer, x
  11. '进入程式获取节点
  12. With session
  13. .findById("wnd[0]").maximize
  14. .findById("wnd[0]/tbar[0]/okcd").Text = "/NFS00"
  15. .findById("wnd[0]").sendVKey 0 'Enter
  16. Set Table = .findById("wnd[0]/shellcont/shell/shellcont[1]/shell[1]")
  17. Set GetNodeK = Table.GetAllNodeKeys '得到所有的节点
  18. End With
  19. '打开所有节点
  20. For x = GetNodeK.Count - 1 To 0 Step -1
  21. Table.expandNode GetNodeK.Item(x)
  22. Next x
  23. '重新读取shell[1]
  24. Set Table = session.findById("wnd[0]/shellcont/shell/shellcont[1]/shell[1]")
  25. Set GetNodeK = Table.GetAllNodeKeys '得到所有的节点
  26. For x = 0 To GetNodeK.Count - 1
  27. i = i + 1
  28. ReDim Preserve arr(1 To i)
  29. arr(i) = GetNodeK.Item(x) '节点
  30. arr(i) = Table.getitemtext(arr(i), "&Hierarchy") '获得内容
  31. Next x
  32. 'With ThisWorkbook.Sheets("FS00")
  33. ' .AutoFilterMode = False
  34. ' .UsedRange.ClearContents
  35. ' .Cells(1, 1).Resize(i) = Application.Transpose(arr)
  36. 'End With
  37. Dim brr(), v, j As Integer, sr As String
  38. ReDim brr(1 To i, 1 To 4)
  39. For x = 1 To i
  40. If InStr(1, arr(x), " ") = 0 Then
  41. sr = arr(x)
  42. Else
  43. j = j + 1
  44. brr(j, 1) = sr
  45. brr(j, 2) = arr(x)
  46. brr(j, 3) = Split(arr(x), " ")(0)
  47. brr(j, 4) = Trim(Replace(arr(x), brr(j, 3), ""))
  48. End If
  49. Next x
  50. With ThisWorkbook.Sheets("FS00")
  51. .AutoFilterMode = False
  52. .UsedRange.ClearContents
  53. .Cells(1, 1).Resize(1, UBound(brr, 2)) = Split("科目组;科目与科目描述;科目;科目描述", ";")
  54. If j > 0 Then .Cells(2, 1).Resize(j, UBound(brr, 2)) = brr
  55. End With
  56. End Sub

SM30

SM30中,ZTCO0011B用于配置进销存报表,此方法在正式区读取表后又可以再测试区导入进去。

  1. Sub SM30_ZTCO0011B_显示()
  2. Dim iMg As VbMsgBoxStyle
  3. iMg = MsgBox("是否显示ZTCO0011B?" & Chr(10) & " " & Chr(10), vbYesNo, "SM30")
  4. If iMg = 7 Then Exit Sub
  5. Dim x As Integer, y As Integer, sr As String, rg As Range, arr(), k As Integer, i As Integer, j As Integer, bl As Boolean
  6. Dim SapGuiAuto As Object, AppSap As Object, Connection As Object, session As Object, Position
  7. Set SapGuiAuto = GetObject("SAPGUI")
  8. Set AppSap = SapGuiAuto.GetScriptingEngine
  9. Set Connection = AppSap.Children(0)
  10. Set session = Connection.Children(0)
  11. With session
  12. .findById("wnd[0]/usr/tblSAPLZFG_ZTCO0011BTCTRL_ZTCO0011B").verticalScrollbar.Position = 0
  13. sr = .findById("wnd[0]/usr/txtVIM_POSITION_INFO").Text
  14. j = CDbl(Split(sr, "/")(1))
  15. ReDim arr(0 To j, 1 To 6)
  16. i = -1
  17. For x = 0 To j
  18. i = i + 1
  19. arr(x, 1) = .findById("wnd[0]/usr/tblSAPLZFG_ZTCO0011BTCTRL_ZTCO0011B/txtZTCO0011B-BIZCODE[0," & i & "]").Text
  20. arr(x, 2) = .findById("wnd[0]/usr/tblSAPLZFG_ZTCO0011BTCTRL_ZTCO0011B/txtZTCO0011B-BWART[1," & i & "]").Text
  21. arr(x, 3) = .findById("wnd[0]/usr/tblSAPLZFG_ZTCO0011BTCTRL_ZTCO0011B/txtZTCO0011B-BIZTEXT[2," & i & "]").Text
  22. arr(x, 4) = .findById("wnd[0]/usr/tblSAPLZFG_ZTCO0011BTCTRL_ZTCO0011B/cmbZTCO0011B-BIZATTR[3," & i & "]").Text
  23. arr(x, 5) = .findById("wnd[0]/usr/tblSAPLZFG_ZTCO0011BTCTRL_ZTCO0011B/cmbZTCO0011B-SHKZG[4," & i & "]").Text
  24. arr(x, 6) = .findById("wnd[0]/usr/tblSAPLZFG_ZTCO0011BTCTRL_ZTCO0011B/txtZTCO0011B-SOBKZ[5," & i & "]").Text
  25. If i Mod 19 = 0 Then
  26. .findById("wnd[0]/usr/tblSAPLZFG_ZTCO0011BTCTRL_ZTCO0011B").verticalScrollbar.Position = x
  27. i = 0
  28. End If
  29. Next x
  30. End With
  31. With ThisWorkbook.Sheets("SM30_ZTCO0011B")
  32. .AutoFilterMode = False
  33. .Cells.ClearContents
  34. .Cells(1, 1).Resize(1, UBound(arr, 2)) = Split("业务分类代码;MvT;业务分类描述;业务属性;借贷;特殊库存", ";")
  35. .Cells(2, 1).Resize(UBound(arr), UBound(arr, 2)) = arr
  36. End With
  37. End Sub
  38. Sub SM30_ZTCO0011B_导入()
  39. Dim SapGuiAuto As Object, AppSap As Object, Connection As Object, session As Object, Position
  40. Set SapGuiAuto = GetObject("SAPGUI")
  41. Set AppSap = SapGuiAuto.GetScriptingEngine
  42. Set Connection = AppSap.Children(0)
  43. Set session = Connection.Children(0)
  44. Dim d As Object, x As Integer, y As Integer, rg As Range, sr As String, v, u, i As Integer, j As Integer, k As Integer
  45. Set d = CreateObject("scripting.dictionary")
  46. sr = "SM30_ZTCO0011B"
  47. Set rg = ThisWorkbook.Sheets(sr).UsedRange.Find("*")
  48. If rg Is Nothing Then
  49. MsgBox "错误!表【" & sr & "】中无数据!"
  50. Exit Sub
  51. End If
  52. Dim arr()
  53. arr = rg.CurrentRegion.Value
  54. d.Add "出库", "2"
  55. d.Add "入库", "1"
  56. d.Add "借方", "S"
  57. d.Add "贷方", "H"
  58. With session
  59. i = -1
  60. For x = 2 To UBound(arr)
  61. i = i + 1
  62. .findById("wnd[0]/usr/tblSAPLZFG_ZTCO0011BTCTRL_ZTCO0011B/txtZTCO0011B-BIZCODE[0," & i & "]").Text = arr(x, 1)
  63. .findById("wnd[0]/usr/tblSAPLZFG_ZTCO0011BTCTRL_ZTCO0011B/txtZTCO0011B-BWART[1," & i & "]").Text = arr(x, 2)
  64. .findById("wnd[0]/usr/tblSAPLZFG_ZTCO0011BTCTRL_ZTCO0011B/txtZTCO0011B-BIZTEXT[2," & i & "]").Text = arr(x, 3)
  65. If arr(x, 4) <> "" Then .findById("wnd[0]/usr/tblSAPLZFG_ZTCO0011BTCTRL_ZTCO0011B/cmbZTCO0011B-BIZATTR[3," & i & "]").Key = d(arr(x, 4))
  66. If arr(x, 5) <> "" Then .findById("wnd[0]/usr/tblSAPLZFG_ZTCO0011BTCTRL_ZTCO0011B/cmbZTCO0011B-SHKZG[4," & i & "]").Key = d(arr(x, 5))
  67. .findById("wnd[0]/usr/tblSAPLZFG_ZTCO0011BTCTRL_ZTCO0011B/txtZTCO0011B-SOBKZ[5," & i & "]").Text = arr(x, 6)
  68. If i Mod 19 = 0 Then
  69. .findById("wnd[0]/usr/tblSAPLZFG_ZTCO0011BTCTRL_ZTCO0011B").verticalScrollbar.Position = x - 2
  70. i = 0
  71. End If
  72. Next x
  73. End With
  74. End Sub

Tcode

可以新建一个收藏夹,然后获取该收藏夹的节点,维护要插入的事务码,事务码和收藏夹要进行逆序排序

节点文件夹事物码事物文本
F00289PS_1.3_项目预算增加删减流程CJ37项目中的预算补充
F00289PS_1.3_项目预算增加删减流程CJ38项目中的预算返回
F00289PS_1.3_项目预算增加删减流程CJ32改变工程发放
F00289PS_1.3_项目预算增加删减流程CJ33显示项目发行
F00289PS_1.3_项目预算增加删减流程CJ3A改变预算凭证
F00289PS_1.3_项目预算增加删减流程CJ3B显示预算文档
F00289PS_1.2_项目预算编列流程CJ30改变工程项目源预算 
F00289PS_1.2_项目预算编列流程CJ31显示工程项目源预算 
F00289PS_1.2_项目预算编列流程CJ32改变工程发放
F00289PS_1.2_项目预算编列流程CJ33显示项目发行
F00289PS_1.2_项目预算编列流程CJ3A改变预算凭证
F00289PS_1.2_项目预算编列流程CJ3B显示预算文档
F00289PS_1.1_WBS主数据维护流程CJ01生成工作细分结构
F00289PS_1.1_WBS主数据维护流程CJ02更改工作细分结构
F00289PS_1.1_WBS主数据维护流程CJ03显示工作细分结构
F00289PS_1.1_WBS主数据维护流程CJ20N项目构建器 
  1. Sub Tcode_获取节点()
  2. Dim SapGuiAuto As Object, AppSap As Object, Connection As Object, session As Object
  3. Set SapGuiAuto = GetObject("SAPGUI")
  4. Set AppSap = SapGuiAuto.GetScriptingEngine
  5. Set Connection = AppSap.Children(0)
  6. Set session = Connection.Children(0)
  7. Dim Table As Object, GetNodeK As Object, arr(), i As Integer, x As Integer, sr As String
  8. ReDim Title(1 To 10)
  9. '进入程式获取节点
  10. With session
  11. .findById("wnd[0]").maximize
  12. .findById("wnd[0]/tbar[0]/okcd").Text = "/N"
  13. .findById("wnd[0]").sendVKey 0 'Enter
  14. sr = "wnd[0]/usr/btnSTARTBUTTON"
  15. If Not session.findById(sr, False) Is Nothing Then
  16. .findById(sr).press
  17. End If
  18. Set Table = .findById("wnd[0]/usr/cntlIMAGE_CONTAINER/shellcont/shell/shellcont[0]/shell")
  19. Set GetNodeK = Table.GetAllNodeKeys '得到所有的节点
  20. End With
  21. For x = 0 To GetNodeK.Count - 1
  22. i = i + 1
  23. ReDim Preserve arr(1 To i)
  24. arr(i) = GetNodeK.Item(x) '节点
  25. Next x
  26. With ThisWorkbook.Sheets("获取节点")
  27. .AutoFilterMode = False
  28. .UsedRange.ClearContents
  29. .Cells(1, 1).Resize(i) = Application.Transpose(arr)
  30. End With
  31. End Sub
  32. Sub Tcode_插入事物码()
  33. Dim iMg As VbMsgBoxStyle
  34. iMg = MsgBox("插入事务码!" & Chr(10) & " " & Chr(10), vbYesNo, "SAP_快速插入事务码")
  35. If iMg = 7 Then Exit Sub
  36. Dim SapGuiAuto As Object, AppSap As Object, Connection As Object, session As Object
  37. Set SapGuiAuto = GetObject("SAPGUI")
  38. Set AppSap = SapGuiAuto.GetScriptingEngine
  39. Set Connection = AppSap.Children(0)
  40. Set session = Connection.Children(0)
  41. Dim d As Object, x As Integer, rg As Range, k As Integer, s1 As String, s2 As String, v1, v2, v3
  42. Set d = CreateObject("scripting.dictionary")
  43. Dim Table As Object
  44. s1 = "插入事务码"
  45. Set rg = ThisWorkbook.Sheets(s1).UsedRange.Find("*")
  46. If rg Is Nothing Then
  47. MsgBox "错误!表【" & s1 & "】中无数据!"
  48. Exit Sub
  49. End If
  50. Dim arr()
  51. arr = rg.CurrentRegion.Value
  52. For x = 2 To UBound(arr)
  53. s1 = arr(x, 1) '节点
  54. s2 = arr(x, 2) '文件夹名称
  55. If Not d.exists(s1) Then
  56. Set d(s1) = CreateObject("scripting.dictionary")
  57. End If
  58. If Not d(s1).exists(s2) Then
  59. Set d(s1)(s2) = CreateObject("scripting.dictionary")
  60. End If
  61. d(s1)(s2)(arr(x, 3)) = "" 'arr(x, 3) 是事务码
  62. Next x
  63. With session
  64. .findById("wnd[0]").maximize
  65. Set Table = .findById("wnd[0]/usr/cntlIMAGE_CONTAINER/shellcont/shell/shellcont[0]/shell")
  66. For Each v1 In d.keys
  67. For Each v2 In d(v1).keys
  68. Table.selectedNode = v1
  69. Table.nodeContextMenu v1
  70. Table.selectContextMenuItem "XXFOLD" '插入文件夹
  71. .findById("wnd[1]/usr/sub:SAPLSPO4:0300/txtSVALD-VALUE[0,21]").Text = v2
  72. .findById("wnd[1]/tbar[0]/btn[0]").press
  73. For Each v3 In d(v1)(v2).keys
  74. .findById("wnd[0]").maximize
  75. Table.nodeContextMenu NodeKeys(CStr(v1))
  76. Table.selectContextMenuItem "XXADTC" '插入事务码
  77. .findById("wnd[1]/usr/sub:SAPLSPO4:0300/txtSVALD-VALUE[0,21]").Text = v3
  78. .findById("wnd[1]/tbar[0]/btn[0]").press
  79. Next v3
  80. Next v2
  81. Next v1
  82. End With
  83. Set d = Nothing
  84. MsgBox "结束!"
  85. End Sub
  86. Function NodeKeys(s1 As String) As String '例如 要把 F00289 改成 F00290
  87. Dim i As Integer, s2 As String
  88. i = Len(s1)
  89. s2 = CDbl(Right(s1, i - 1)) + 1
  90. NodeKeys = "F" & Application.Rept(0, i - Len(s2) - 1) & s2
  91. End Function

CKM3N

批量查询料号的成本价明细

CKM3N维护查询的数据
工厂料号
202340510G1CMX085065A-Y
  1. Sub CKM3N_显示物料价格_跨月_多料号()
  2. On Error Resume Next
  3. Dim iMg As VbMsgBoxStyle
  4. iMg = MsgBox("是否在正式区显示物料价格?" & Chr(10) & " " & Chr(10), vbYesNo, "CKM3N")
  5. If iMg = 7 Then Exit Sub
  6. Dim SapGuiAuto As Object, AppSap As Object, Connection As Object, session As Object
  7. Set SapGuiAuto = GetObject("SAPGUI")
  8. Set AppSap = SapGuiAuto.GetScriptingEngine
  9. Set Connection = AppSap.Children(0)
  10. Set session = Connection.Children(0)
  11. Dim x As Long, y As Integer, z As Integer, sr As String, rg As Range, arr1(), bl As Boolean, db As Double, k As Long, v
  12. Dim Table As Object, Columns As Object, GetNodeK As Object
  13. ReDim arr2(1 To 100000, 1 To 29)
  14. sr = "CKM3N跨月"
  15. Set rg = ThisWorkbook.Sheets(sr).UsedRange.Find("*")
  16. If rg Is Nothing Then
  17. MsgBox "错误!表【" & sr & "】中无数据!"
  18. Exit Sub
  19. End If
  20. arr1 = rg.CurrentRegion.Value
  21. bl = False
  22. With session
  23. For x = 2 To UBound(arr1)
  24. If arr1(x, 1) = "" Then Exit For
  25. .findById("wnd[0]").maximize
  26. .findById("wnd[0]/tbar[0]/okcd").Text = "/NCKM3N"
  27. .findById("wnd[0]").sendVKey 0 'Enter
  28. .findById("wnd[0]/usr/ctxtMLKEY-WERKS_ML_PRODUCTIVE").Text = arr1(x, 3) '查询工厂
  29. .findById("wnd[0]/usr/ctxtMLKEY-MATNR").Text = arr1(x, 4) '查询物料
  30. .findById("wnd[0]/usr/txtMLKEY-POPER").Text = arr1(x, 2) '月
  31. .findById("wnd[0]/usr/txtMLKEY-BDATJ").Text = arr1(x, 1) '年
  32. .findById("wnd[0]/tbar[1]/btn[13]").press '刷新
  33. .findById("wnd[0]/usr/btn%#AUTOTEXT003").press '折叠选择字段 价格
  34. For Each v In Split("10;32", ";") '10" '公司层面 '"32"'利润中心层面 ';32
  35. .findById("wnd[0]/usr/cmbMLKEY-CURTP").Key = v '货币/评估
  36. sr = "wnd[0]/usr/ssubSUB:SAPLCKM8H:0300/cntlCONTAINER/shellcont/shell/shellcont[1]/shell[1]"
  37. If Not .findById(sr, False) Is Nothing Then
  38. Set Table = .findById(sr)
  39. Set Columns = Table.ColumnOrder()
  40. Set GetNodeK = Table.GetAllNodeKeys '得到所有的节点
  41. For z = 0 To GetNodeK.Count - 1
  42. k = k + 1
  43. For y = 1 To 4
  44. arr2(k, y) = arr1(x, y)
  45. Next y
  46. arr2(k, 5) = .findById("wnd[0]/usr/cmbMLKEY-CURTP").Text '货币/评估
  47. arr2(k, 6) = .findById("wnd[0]/usr/ctxtCKMLCR-VPRSV").Text '价格控制
  48. arr2(k, 7) = .findById("wnd[0]/usr/txtCKMLCR-STPRS").Text '标准价格
  49. arr2(k, 8) = .findById("wnd[0]/usr/txtCKMLCR-PVPRS").Text '定期价格 '正式区是 wnd[0]/usr/txtCKMLCR-PVPRS '测试区是 wnd[0]/usr/txtPVPRS_DYN
  50. arr2(k, 9) = .findById("wnd[0]/usr/txtCKMLCR-PEINH").Text '价格单位
  51. arr2(k, 12) = Table.getitemtext(GetNodeK.Item(z), "&Hierarchy")
  52. For y = 1 To 17
  53. arr2(k, y + 12) = Table.getitemtext(GetNodeK.Item(z), CStr(Columns(y)))
  54. Next y
  55. Next z
  56. End If
  57. Next v
  58. Next x
  59. End With
  60. For x = 1 To k
  61. db = arr2(x, 9) '价格单位
  62. If db <> 0 Then
  63. arr2(x, 10) = arr2(x, 7) / db '标准价=标准价格/价格单位
  64. arr2(x, 11) = arr2(x, 8) / db '实际价=定期价格/价格单位
  65. End If
  66. arr2(x, 13) = CDbl(arr2(x, 13)) '数量
  67. For y = 15 To 29
  68. arr2(x, y) = CDbl(arr2(x, y)) '初级评估等
  69. Next y
  70. arr2(x, 3) = "'" & arr2(x, 3) '工厂
  71. Next x
  72. With ThisWorkbook.Sheets("CKM3N跨月")
  73. .AutoFilterMode = False
  74. With .Cells(1, 5)
  75. .Resize(1, UBound(arr2, 2)).EntireColumn.ClearContents
  76. .Resize(1, UBound(arr2, 2)) = Split("年;月;工厂;物料;货币评估;价格控制;标准价格;定期价格;价格单位;标准价;实际价;类别;数量;数量单位;初级评估;价格差异;汇率差异;实际值;价格;公司间利润;直接材料;直接人工;间接人员薪资及福利;模具及治工具;资产摊提及能源消耗;耗品及杂项消耗;其他费用;委外加工;成本构成总和", ";")
  77. If k > 0 Then .Cells(2, 1).Resize(k, UBound(arr2, 2)) = arr2
  78. End With
  79. End With
  80. End Sub

CKM3N自行输入料号查询,只能开一个屏,否则会报错,可以自己打开节点,看想要的内容

  1. Sub CKM3N_显示物料价格_明细_单月单笔()
  2. Dim SapGuiAuto As Object, AppSap As Object, Connection As Object, session As Object
  3. Set SapGuiAuto = GetObject("SAPGUI")
  4. Set AppSap = SapGuiAuto.GetScriptingEngine
  5. Set Connection = AppSap.Children(0)
  6. Set session = Connection.Children(0)
  7. Dim x As Integer, y As Integer, z As Integer, sr As String, rg As Range, arr1(), k As Integer, brr(), bl As Boolean
  8. Dim Table As Object, Columns As Object, GetNodeK As Object
  9. ReDim arr2(1 To 100000, 1 To 18) '此处要提前知道列数,并且加了一列 'Table.ColumnCount()
  10. With session
  11. Set Table = .findById("wnd[0]/usr/ssubSUB:SAPLCKM8H:0300/cntlCONTAINER/shellcont/shell/shellcont[1]/shell[1]")
  12. Set Columns = Table.ColumnOrder()
  13. Set GetNodeK = Table.GetAllNodeKeys '得到所有的节点
  14. For x = 0 To GetNodeK.Count - 1
  15. k = k + 1
  16. arr2(k, 1) = Table.getitemtext(GetNodeK.Item(x), "&Hierarchy")
  17. For y = 1 To 17 'Table.ColumnCount() - 1
  18. arr2(k, y + 1) = Table.getitemtext(GetNodeK.Item(x), CStr(Columns(y)))
  19. Next y
  20. Next x
  21. For x = 1 To k
  22. For y = 2 To UBound(arr2, 2)
  23. If y <> 3 Then
  24. If arr2(x, y) = "" Then
  25. arr2(x, y) = 0
  26. Else
  27. arr2(x, y) = CDbl(arr2(x, y))
  28. End If
  29. End If
  30. Next y
  31. Next x
  32. End With
  33. With ThisWorkbook.Sheets("CKM3N明细")
  34. .AutoFilterMode = False
  35. .UsedRange.ClearContents
  36. .Cells(1, 1).Resize(1, UBound(arr2, 2)) = Split("类别;数量;数量单位;初级评估;价格差异;汇率差异;实际值;价格;公司间利润;直接材料;直接人工;间接人员薪资及福利;模具及治工具;资产摊提及能源消耗;耗品及杂项消耗;其他费用;委外加工;成本构成总和", ";")
  37. If k > 0 Then .Cells(2, 1).Resize(k, UBound(arr2, 2)) = arr2
  38. End With
  39. End Sub

FB02

FB02批量修改凭证文本的摘要

  1. Sub FB02_修改凭证文本栏位()
  2. Dim iMg As VbMsgBoxStyle
  3. iMg = MsgBox("是否修改凭证文本栏位?" & Chr(10) & " " & Chr(10), vbYesNo, "FB02")
  4. If iMg = 7 Then Exit Sub
  5. Dim SapGuiAuto As Object, AppSap As Object, Connection As Object, session As Object
  6. Set SapGuiAuto = GetObject("SAPGUI")
  7. Set AppSap = SapGuiAuto.GetScriptingEngine
  8. Set Connection = AppSap.Children(0)
  9. Set session = Connection.Children(0)
  10. Dim Table As Object, Columns As Object
  11. Dim arr1(), x As Integer, y As Integer, z As Integer, sr As String, rg As Range
  12. sr = "FB02"
  13. Set rg = ThisWorkbook.Sheets(sr).UsedRange.Find("*")
  14. If rg Is Nothing Then
  15. MsgBox "错误!表【" & sr & "】中无数据!"
  16. Exit Sub
  17. End If
  18. arr1 = rg.CurrentRegion.Value
  19. With session
  20. For z = 2 To UBound(arr1)
  21. .findById("wnd[0]").maximize
  22. .findById("wnd[0]/tbar[0]/okcd").Text = "/NFB02" '修改凭证
  23. .findById("wnd[0]").sendVKey 0
  24. .findById("wnd[0]/usr/txtRF05L-BELNR").Text = arr1(z, 3) '凭证编号
  25. .findById("wnd[0]/usr/ctxtRF05L-BUKRS").Text = arr1(z, 2) '公司代码
  26. .findById("wnd[0]/usr/txtRF05L-GJAHR").Text = arr1(z, 1) '会计年度
  27. .findById("wnd[0]").sendVKey 0
  28. '.findById("wnd[0]/usr/cntlCTRL_CONTAINERBSEG/shellcont/shell").selectColumn "SGTXT" '选中“文本”栏位
  29. '.findById("wnd[0]/usr/cntlCTRL_CONTAINERBSEG/shellcont/shell").pressToolbarButton "&SORT_DSC" '排序
  30. '.findById("wnd[0]/tbar[1]/btn[25]").press '更改模式
  31. Set Table = .findById("wnd[0]/usr/cntlCTRL_CONTAINERBSEG/shellcont/shell")
  32. Set Columns = Table.ColumnOrder()
  33. For x = 0 To Table.RowCount() - 1
  34. If Table.getcellvalue(x, "SGTXT") = arr1(z, 4) Then '原文本
  35. Table.SetCurrentCell x, "KTONR"
  36. Table.doubleClickCurrentCell '双击
  37. .findById("wnd[0]/usr/ctxtBSEG-SGTXT").Text = arr1(z, 5) '更改后文本
  38. .findById("wnd[0]/tbar[0]/btn[3]").press '返回
  39. End If
  40. If x Mod 14 = 0 Then '屏幕上显示的最大行数,根据电脑的不同可能有变
  41. Table.SetCurrentCell x, CStr(Columns(0))
  42. Table.firstVisibleRow = x
  43. End If
  44. Next x
  45. .findById("wnd[0]/tbar[0]/btn[11]").press '保存
  46. Next z
  47. End With
  48. End Sub

KSU1

可以创建分摊规则,这里主要还是用成本中心分摊,其他栏位情况没考虑

  1. Sub KSU1_标题()
  2. Dim arr() As String
  3. arr = Split("循环名;循环名描述;开始时间;结束时间;段名;段名描述;发送者成本中心从;发送者成本中心至;发送者成本中心组;接收方成本中心从;接收方成本中心至;接收方成本中心组", ";")
  4. With ThisWorkbook.Sheets("KSU1")
  5. .AutoFilterMode = False
  6. .Cells(1, 1).Resize(1, UBound(arr) + 1) = arr
  7. End With
  8. End Sub
  9. Sub KSU1_创建实际分摊()
  10. Dim iMg As VbMsgBoxStyle
  11. iMg = MsgBox("是否创建实际分摊?" & Chr(10) & " " & Chr(10), vbYesNo, "KSU1")
  12. If iMg = 7 Then Exit Sub
  13. Dim SapGuiAuto As Object, AppSap As Object, Connection As Object, session As Object
  14. Set SapGuiAuto = GetObject("SAPGUI")
  15. Set AppSap = SapGuiAuto.GetScriptingEngine
  16. Set Connection = AppSap.Children(0)
  17. Set session = Connection.Children(0)
  18. Dim x As Integer, sr As String, rg As Range, arr1()
  19. sr = "KSU1"
  20. Set rg = ThisWorkbook.Sheets(sr).UsedRange.Find("*")
  21. If rg Is Nothing Then
  22. MsgBox "错误!表【" & sr & "】中无数据!"
  23. Exit Sub
  24. End If
  25. arr1 = rg.CurrentRegion.Value
  26. With session
  27. .findById("wnd[0]").maximize
  28. .findById("wnd[0]/tbar[0]/okcd").Text = "/NKSU1" '创建实际分摊
  29. .findById("wnd[0]").sendVKey 0 'Enter
  30. .findById("wnd[0]/usr/ctxtRKAL1-KSCYC").Text = arr1(2, 1) '循环名
  31. .findById("wnd[0]/usr/ctxtT811C-SDATE").Text = arr1(2, 3) '开始时间
  32. .findById("wnd[0]").sendVKey 0 'Enter
  33. .findById("wnd[0]/usr/ctxtT811C-EDATE").Text = arr1(2, 4) '结束时间
  34. .findById("wnd[0]/usr/txtRKAL1-CTXT").Text = arr1(2, 2) '循环名描述
  35. For x = 2 To UBound(arr1)
  36. .findById("wnd[0]/tbar[1]/btn[20]").press '增加段
  37. .findById("wnd[0]/usr/txtKGALS-NAME").Text = arr1(x, 5) '段名
  38. .findById("wnd[0]/usr/txtKGALS-TXT").Text = arr1(x, 6) '段名描述
  39. .findById("wnd[0]/usr/tabsSEG_TABSTRIP/tabpSQHD/ssubSUB1:SAPMKAL1:0307/ctxtKGALS-ABSCH").Text = "Z3" '分配结构
  40. .findById("wnd[0]/usr/tabsSEG_TABSTRIP/tabpSQHD/ssubSUB1:SAPMKAL1:0307/cmbRKAL1-RCDATAFLG").Key = "7" '可变部分类型
  41. .findById("wnd[0]/usr/tabsSEG_TABSTRIP/tabpOBJS").Select '发送方/接收方
  42. .findById("wnd[0]/usr/tabsSEG_TABSTRIP/tabpOBJS/ssubSUB1:SAPMKAL1:0306/sub:SAPMKAL1:0306/ctxtKGALK-SETID[4,67]").Text = "ZZ00" '成本要素组 '修改
  43. .findById("wnd[0]/usr/tabsSEG_TABSTRIP/tabpOBJS/ssubSUB1:SAPMKAL1:0306/sub:SAPMKAL1:0306/ctxtKGALK-VALMIN[1,17]").Text = arr1(x, 7) '发送者成本中心从
  44. .findById("wnd[0]/usr/tabsSEG_TABSTRIP/tabpOBJS/ssubSUB1:SAPMKAL1:0306/sub:SAPMKAL1:0306/ctxtKGALK-VALMAX[1,42]").Text = arr1(x, 8) '发送者成本中心至
  45. .findById("wnd[0]/usr/tabsSEG_TABSTRIP/tabpOBJS/ssubSUB1:SAPMKAL1:0306/sub:SAPMKAL1:0306/ctxtKGALK-SETID[1,67]").Text = arr1(x, 9) '发送者成本中心组
  46. .findById("wnd[0]/usr/tabsSEG_TABSTRIP/tabpOBJS/ssubSUB1:SAPMKAL1:0306/sub:SAPMKAL1:0306/ctxtKGALK-VALMIN[7,17]").Text = arr1(x, 10) '接收方成本中心从
  47. .findById("wnd[0]/usr/tabsSEG_TABSTRIP/tabpOBJS/ssubSUB1:SAPMKAL1:0306/sub:SAPMKAL1:0306/ctxtKGALK-VALMAX[7,42]").Text = arr1(x, 11) '接收方成本中心至
  48. .findById("wnd[0]/usr/tabsSEG_TABSTRIP/tabpOBJS/ssubSUB1:SAPMKAL1:0306/sub:SAPMKAL1:0306/ctxtKGALK-SETID[7,67]").Text = arr1(x, 12) '接收方成本中心组
  49. .findById("wnd[0]/usr/tabsSEG_TABSTRIP/tabpSEND").Select '发送方值
  50. .findById("wnd[0]/usr/tabsSEG_TABSTRIP/tabpSEND/ssubSUB1:SAPMKAL1:0451/sub:SAPMKAL1:0451/ctxtKGALK-VALMIN[0,17]").Text = "0" '版本
  51. .findById("wnd[0]/usr/tabsSEG_TABSTRIP/tabpRECE").Select '接收方追踪因素
  52. .findById("wnd[0]/usr/tabsSEG_TABSTRIP/tabpRECE/ssubSUB1:SAPMKAL1:0461/sub:SAPMKAL1:0461/ctxtKGALK-VALMIN[0,17]").Text = "ZZ01" '活动类型:从
  53. .findById("wnd[0]/usr/tabsSEG_TABSTRIP/tabpRECE/ssubSUB1:SAPMKAL1:0461/sub:SAPMKAL1:0461/ctxtKGALK-VALMAX[0,42]").Text = "ZZ06" '活动类型:到
  54. .findById("wnd[0]/usr/tabsSEG_TABSTRIP/tabpRWFC").Select '参考加权因素
  55. Next x
  56. End With
  57. MsgBox "运行成功!"
  58. End Sub

KSU2

修改已经创建的分摊规则

  1. Sub KSU2_标题()
  2. Dim arr() As String
  3. arr = Split("查询循环名;查询开始时间;修改结束时间;修改循环名描述;修改段名;修改段名描述;修改发送者成本中心从;修改发送者成本中心至;修改发送者成本中心组;修改接收方成本中心从;修改接收方成本中心至;修改接收方成本中心组", ";")
  4. With ThisWorkbook.Sheets("KSU2")
  5. .AutoFilterMode = False
  6. .Cells(1, 1).Resize(1, UBound(arr) + 1) = arr
  7. End With
  8. End Sub
  9. Sub KSU2_修改实际分摊()
  10. Dim iMg As VbMsgBoxStyle
  11. iMg = MsgBox("是否修改实际分摊?" & Chr(10) & " " & Chr(10), vbYesNo, "KSU2")
  12. If iMg = 7 Then Exit Sub
  13. Dim SapGuiAuto As Object, AppSap As Object, Connection As Object, session As Object
  14. Set SapGuiAuto = GetObject("SAPGUI")
  15. Set AppSap = SapGuiAuto.GetScriptingEngine
  16. Set Connection = AppSap.Children(0)
  17. Set session = Connection.Children(0)
  18. Dim x As Integer, sr As String, rg As Range, arr1(), arr2(), brr(), k As Long, iMax As Integer, i As Integer, j As Integer
  19. ReDim arr2(1 To 100000, 1 To 18)
  20. ReDim brr(1 To 3)
  21. sr = "KSU2"
  22. Set rg = ThisWorkbook.Sheets(sr).UsedRange.Find("*")
  23. If rg Is Nothing Then
  24. MsgBox "错误!表【" & sr & "】中无数据!"
  25. Exit Sub
  26. End If
  27. arr1 = rg.CurrentRegion.Value
  28. With session
  29. .findById("wnd[0]").maximize
  30. .findById("wnd[0]/tbar[0]/okcd").Text = "/NKSU2" '修改实际分配
  31. .findById("wnd[0]").sendVKey 0 'Enter
  32. .findById("wnd[0]/usr/ctxtRKAL1-KSCYC").Text = arr1(2, 1) '循环名
  33. .findById("wnd[0]/usr/ctxtT811C-SDATE").Text = arr1(2, 2) '开始日期
  34. .findById("wnd[0]").sendVKey 0 'Enter
  35. .findById("wnd[0]/usr/ctxtT811C-EDATE").Text = arr1(2, 3) '结束时间
  36. .findById("wnd[0]/usr/txtRKAL1-CTXT").Text = arr1(2, 4) '循环名描述
  37. .findById("wnd[0]/tbar[1]/btn[6]").press '第一个段
  38. .findById("wnd[0]/mbar/menu[2]/menu[1]").Select '转到>>概述段
  39. j = .findById("wnd[1]/usr/txtRCEVM-ENTRIES").Text '段号
  40. .findById("wnd[1]/tbar[0]/btn[12]").press '取消段概览
  41. For x = 1 To j '为了修改的时候不重名
  42. .findById("wnd[0]/usr/txtKGALS-NAME").Text = x
  43. If x <> j Then .findById("wnd[0]/tbar[1]/btn[19]").press '下一段
  44. Next x
  45. For x = 1 To j - 1 '回退到第一个段
  46. .findById("wnd[0]/tbar[1]/btn[18]").press '前一段
  47. Next x
  48. For x = 2 To UBound(arr1)
  49. i = i + 1
  50. .findById("wnd[0]/usr/txtKGALS-NAME").Text = arr1(x, 5) '段名 (不能大于十个字符)
  51. .findById("wnd[0]/usr/txtKGALS-TXT").Text = arr1(x, 6) '段名描述
  52. .findById("wnd[0]/usr/tabsSEG_TABSTRIP/tabpSQHD/ssubSUB1:SAPMKAL1:0307/ctxtKGALS-ABSCH").Text = "Z3" '分配结构
  53. .findById("wnd[0]/usr/tabsSEG_TABSTRIP/tabpSQHD/ssubSUB1:SAPMKAL1:0307/cmbRKAL1-RCDATAFLG").Key = "7" '可变部分类型
  54. .findById("wnd[0]/usr/tabsSEG_TABSTRIP/tabpOBJS").Select '发送方/接收方
  55. .findById("wnd[0]/usr/tabsSEG_TABSTRIP/tabpOBJS/ssubSUB1:SAPMKAL1:0306/sub:SAPMKAL1:0306/ctxtKGALK-SETID[4,67]").Text = "ZZ00" '成本要素组
  56. .findById("wnd[0]/usr/chkKGALS-ACTIVE").Selected = False '锁定标识符
  57. .findById("wnd[0]/usr/tabsSEG_TABSTRIP/tabpOBJS/ssubSUB1:SAPMKAL1:0306/sub:SAPMKAL1:0306/ctxtKGALK-VALMIN[1,17]").Text = arr1(x, 7) '发送者成本中心从
  58. .findById("wnd[0]/usr/tabsSEG_TABSTRIP/tabpOBJS/ssubSUB1:SAPMKAL1:0306/sub:SAPMKAL1:0306/ctxtKGALK-VALMAX[1,42]").Text = arr1(x, 8) '发送者成本中心至
  59. .findById("wnd[0]/usr/tabsSEG_TABSTRIP/tabpOBJS/ssubSUB1:SAPMKAL1:0306/sub:SAPMKAL1:0306/ctxtKGALK-SETID[1,67]").Text = arr1(x, 9) '发送者成本中心组
  60. .findById("wnd[0]/usr/tabsSEG_TABSTRIP/tabpOBJS/ssubSUB1:SAPMKAL1:0306/sub:SAPMKAL1:0306/ctxtKGALK-VALMIN[7,17]").Text = arr1(x, 10) '接收方成本中心从
  61. .findById("wnd[0]/usr/tabsSEG_TABSTRIP/tabpOBJS/ssubSUB1:SAPMKAL1:0306/sub:SAPMKAL1:0306/ctxtKGALK-VALMAX[7,42]").Text = arr1(x, 11) '接收方成本中心至
  62. .findById("wnd[0]/usr/tabsSEG_TABSTRIP/tabpOBJS/ssubSUB1:SAPMKAL1:0306/sub:SAPMKAL1:0306/ctxtKGALK-SETID[7,67]").Text = arr1(x, 12) '接收方成本中心组
  63. .findById("wnd[0]/usr/tabsSEG_TABSTRIP/tabpSEND").Select '发送方值
  64. .findById("wnd[0]/usr/tabsSEG_TABSTRIP/tabpSEND/ssubSUB1:SAPMKAL1:0451/sub:SAPMKAL1:0451/ctxtKGALK-VALMIN[0,17]").Text = "0" '版本
  65. .findById("wnd[0]/usr/tabsSEG_TABSTRIP/tabpRECE").Select '接收方追踪因素
  66. .findById("wnd[0]/usr/tabsSEG_TABSTRIP/tabpRECE/ssubSUB1:SAPMKAL1:0461/sub:SAPMKAL1:0461/ctxtKGALK-VALMIN[0,17]").Text = "ZZ01" '活动类型:从
  67. .findById("wnd[0]/usr/tabsSEG_TABSTRIP/tabpRECE/ssubSUB1:SAPMKAL1:0461/sub:SAPMKAL1:0461/ctxtKGALK-VALMAX[0,42]").Text = "ZZ06" '活动类型:到
  68. .findById("wnd[0]/usr/tabsSEG_TABSTRIP/tabpRWFC").Select '参考加权因素
  69. If x = UBound(arr1) Then
  70. ElseIf i < j Then
  71. .findById("wnd[0]/tbar[1]/btn[19]").press
  72. Else
  73. .findById("wnd[0]/tbar[1]/btn[20]").press '增加段
  74. End If
  75. Next x
  76. Do While i < j '如果没有修改的必要则全部锁定掉
  77. i = i + 1
  78. .findById("wnd[0]/tbar[1]/btn[19]").press '下一个段
  79. .findById("wnd[0]/usr/chkKGALS-ACTIVE").Selected = "TRUE" '锁定标识符
  80. Loop
  81. End With
  82. MsgBox "请自行保存!"
  83. End Sub

KSU3

显示分摊规则

查询循环名查询开始时间
C121012022.06.01
  1. Sub KSU3_标题()
  2. Dim arr() As String
  3. arr = Split("查询循环名;查询开始时间;循环名;循环名描述;开始时间;结束时间;段名;段名描述;锁定标识符;分配结构;可变部分类型;成本要素组;发送者成本中心从;发送者成本中心至;发送者成本中心组;接收方成本中心从;接收方成本中心至;接收方成本中心组;版本;活动类型从;活动类型到", ";")
  4. With ThisWorkbook.Sheets("KSU3")
  5. .AutoFilterMode = False
  6. .Cells(1, 1).Resize(1, UBound(arr) + 1) = arr
  7. End With
  8. End Sub
  9. Sub KSU3_显示实际分摊()
  10. Dim iMg As VbMsgBoxStyle
  11. iMg = MsgBox("是否显示实际分摊?" & Chr(10) & " " & Chr(10), vbYesNo, "KSU3")
  12. If iMg = 7 Then Exit Sub
  13. Dim SapGuiAuto As Object, AppSap As Object, Connection As Object, session As Object
  14. Set SapGuiAuto = GetObject("SAPGUI")
  15. Set AppSap = SapGuiAuto.GetScriptingEngine
  16. Set Connection = AppSap.Children(0)
  17. Set session = Connection.Children(0)
  18. Dim x As Integer, sr As String, rg As Range, arr1(), arr2(), brr(), k As Long, iMax As Integer, i As Integer, j As Integer
  19. ReDim arr2(1 To 100000, 1 To 19)
  20. ReDim brr(1 To 3)
  21. sr = "KSU3"
  22. Set rg = ThisWorkbook.Sheets(sr).UsedRange.Find("*")
  23. If rg Is Nothing Then
  24. MsgBox "错误!表【" & sr & "】中无数据!"
  25. Exit Sub
  26. End If
  27. arr1 = rg.CurrentRegion.Value
  28. With session
  29. For x = 2 To UBound(arr1)
  30. If arr1(x, 1) <> "" Then
  31. .findById("wnd[0]").maximize
  32. .findById("wnd[0]/tbar[0]/okcd").Text = "/NKSU3" '显示实际分摊
  33. .findById("wnd[0]").sendVKey 0 'Enter
  34. .findById("wnd[0]/usr/ctxtRKAL1-KSCYC").Text = arr1(x, 1) '循环名
  35. .findById("wnd[0]/usr/ctxtT811C-SDATE").Text = arr1(x, 2) '开始日期
  36. .findById("wnd[0]").sendVKey 0 'Enter
  37. brr(1) = .findById("wnd[0]/usr/txtRKAL1-CTXT").Text '循环名描述
  38. brr(2) = .findById("wnd[0]/usr/ctxtT811C-SDATE").Text '开始时间
  39. brr(3) = .findById("wnd[0]/usr/ctxtT811C-EDATE").Text '结束时间
  40. .findById("wnd[0]/tbar[1]/btn[6]").press '第一个段
  41. .findById("wnd[0]/mbar/menu[2]/menu[1]").Select '转到>>概述段
  42. j = .findById("wnd[1]/usr/txtRCEVM-ENTRIES").Text '段号
  43. .findById("wnd[1]/tbar[0]/btn[12]").press '取消段概览
  44. i = 0
  45. Do
  46. On Error Resume Next
  47. k = k + 1
  48. i = i + 1
  49. arr2(k, 1) = arr1(x, 1)
  50. arr2(k, 2) = brr(1)
  51. arr2(k, 3) = brr(2)
  52. arr2(k, 4) = brr(3)
  53. arr2(k, 5) = .findById("wnd[0]/usr/txtKGALS-NAME").Text '段名
  54. arr2(k, 6) = .findById("wnd[0]/usr/txtKGALS-TXT").Text '段名描述
  55. arr2(k, 7) = .findById("wnd[0]/usr/chkKGALS-ACTIVE").Selected '锁定标识符
  56. arr2(k, 8) = .findById("wnd[0]/usr/tabsSEG_TABSTRIP/tabpSQHD/ssubSUB1:SAPMKAL1:0307/ctxtKGALS-ABSCH").Text '分配结构
  57. arr2(k, 9) = Trim(.findById("wnd[0]/usr/tabsSEG_TABSTRIP/tabpSQHD/ssubSUB1:SAPMKAL1:0307/cmbRKAL1-RCDATAFLG").Text) '可变部分类型
  58. .findById("wnd[0]/usr/tabsSEG_TABSTRIP/tabpOBJS").Select '发送方/接收方
  59. arr2(k, 10) = .findById("wnd[0]/usr/tabsSEG_TABSTRIP/tabpOBJS/ssubSUB1:SAPMKAL1:0306/sub:SAPMKAL1:0306/ctxtKGALK-SETID[4,67]").Text '成本要素组
  60. arr2(k, 11) = .findById("wnd[0]/usr/tabsSEG_TABSTRIP/tabpOBJS/ssubSUB1:SAPMKAL1:0306/sub:SAPMKAL1:0306/ctxtKGALK-VALMIN[1,17]").Text '发送者成本中心从
  61. arr2(k, 12) = .findById("wnd[0]/usr/tabsSEG_TABSTRIP/tabpOBJS/ssubSUB1:SAPMKAL1:0306/sub:SAPMKAL1:0306/ctxtKGALK-VALMAX[1,42]").Text '发送者成本中心至
  62. arr2(k, 13) = .findById("wnd[0]/usr/tabsSEG_TABSTRIP/tabpOBJS/ssubSUB1:SAPMKAL1:0306/sub:SAPMKAL1:0306/ctxtKGALK-SETID[1,67]").Text '发送者成本中心组
  63. arr2(k, 14) = .findById("wnd[0]/usr/tabsSEG_TABSTRIP/tabpOBJS/ssubSUB1:SAPMKAL1:0306/sub:SAPMKAL1:0306/ctxtKGALK-VALMIN[7,17]").Text '接收方成本中心从
  64. arr2(k, 15) = .findById("wnd[0]/usr/tabsSEG_TABSTRIP/tabpOBJS/ssubSUB1:SAPMKAL1:0306/sub:SAPMKAL1:0306/ctxtKGALK-VALMAX[7,42]").Text '接收方成本中心至
  65. arr2(k, 16) = .findById("wnd[0]/usr/tabsSEG_TABSTRIP/tabpOBJS/ssubSUB1:SAPMKAL1:0306/sub:SAPMKAL1:0306/ctxtKGALK-SETID[7,67]").Text '接收方成本中心组
  66. .findById("wnd[0]/usr/tabsSEG_TABSTRIP/tabpSEND").Select '发送方值
  67. arr2(k, 17) = .findById("wnd[0]/usr/tabsSEG_TABSTRIP/tabpSEND/ssubSUB1:SAPMKAL1:0451/sub:SAPMKAL1:0451/ctxtKGALK-VALMIN[0,17]").Text '版本
  68. .findById("wnd[0]/usr/tabsSEG_TABSTRIP/tabpRECE").Select '接收方追踪因素
  69. arr2(k, 18) = .findById("wnd[0]/usr/tabsSEG_TABSTRIP/tabpRECE/ssubSUB1:SAPMKAL1:0461/sub:SAPMKAL1:0461/ctxtKGALK-VALMIN[0,17]").Text '活动类型:从
  70. arr2(k, 19) = .findById("wnd[0]/usr/tabsSEG_TABSTRIP/tabpRECE/ssubSUB1:SAPMKAL1:0461/sub:SAPMKAL1:0461/ctxtKGALK-VALMAX[0,42]").Text '活动类型:到
  71. .findById("wnd[0]/usr/tabsSEG_TABSTRIP/tabpRWFC").Select '参考加权因素
  72. .findById("wnd[0]/tbar[1]/btn[19]").press
  73. Loop Until i >= j
  74. .findById("wnd[1]/tbar[0]/btn[0]").press
  75. End If
  76. Next x
  77. End With
  78. With ThisWorkbook.Sheets("KSU3")
  79. .AutoFilterMode = False
  80. With .Cells(1, 3)
  81. .Resize(1, UBound(arr2, 2)).EntireColumn.ClearContents
  82. .Resize(1, UBound(arr2, 2)) = Split("循环名;循环名描述;开始时间;结束时间;段名;段名描述;锁定标识符;分配结构;可变部分类型;成本要素组;发送者成本中心从;发送者成本中心至;发送者成本中心组;接收方成本中心从;接收方成本中心至;接收方成本中心组;版本;活动类型从;活动类型到", ";")
  83. If k > 0 Then .Cells(2, 1).Resize(k, UBound(arr2, 2)) = arr2
  84. End With
  85. End With
  86. MsgBox "成功"
  87. End Sub

KSV1、KSV2、KSV3

分配

  1. Sub KSV1_创建实际分配()
  2. Dim iMg As VbMsgBoxStyle
  3. iMg = MsgBox("是否创建实际分配?" & Chr(10) & " " & Chr(10), vbYesNo, "KSV1")
  4. If iMg = 7 Then Exit Sub
  5. Dim SapGuiAuto As Object, AppSap As Object, Connection As Object, session As Object
  6. Set SapGuiAuto = GetObject("SAPGUI")
  7. Set AppSap = SapGuiAuto.GetScriptingEngine
  8. Set Connection = AppSap.Children(0)
  9. Set session = Connection.Children(0)
  10. Dim x As Integer, sr As String, rg As Range, arr1()
  11. sr = "KSV1"
  12. Set rg = ThisWorkbook.Sheets(sr).UsedRange.Find("*")
  13. If rg Is Nothing Then
  14. MsgBox "错误!表【" & sr & "】中无数据!"
  15. Exit Sub
  16. End If
  17. arr1 = rg.CurrentRegion.Value
  18. With session
  19. .findById("wnd[0]").maximize
  20. .findById("wnd[0]/tbar[0]/okcd").Text = "/NKSV1" '创建实际分摊
  21. .findById("wnd[0]").sendVKey 0 'Enter
  22. .findById("wnd[0]/usr/ctxtRKAL1-KSCYC").Text = arr1(2, 1) '循环名
  23. .findById("wnd[0]/usr/ctxtT811C-SDATE").Text = arr1(2, 3) '开始时间
  24. .findById("wnd[0]").sendVKey 0 'Enter
  25. .findById("wnd[0]/usr/ctxtT811C-EDATE").Text = arr1(2, 4) '结束时间
  26. .findById("wnd[0]/usr/txtRKAL1-CTXT").Text = arr1(2, 2) '循环名描述
  27. For x = 2 To UBound(arr1)
  28. .findById("wnd[0]/tbar[1]/btn[20]").press '增加段
  29. .findById("wnd[0]/usr/txtKGALS-NAME").Text = arr1(x, 5) '段名
  30. .findById("wnd[0]/usr/txtKGALS-TXT").Text = arr1(x, 6) '段名描述
  31. .findById("wnd[0]/usr/tabsSEG_TABSTRIP/tabpSQHD/ssubSUB1:SAPMKAL1:0308/cmbRKAL1-RCDATAFLG").Key = "7" '可变部分类型
  32. .findById("wnd[0]/usr/tabsSEG_TABSTRIP/tabpOBJS").Select '发送方/接收方
  33. .findById("wnd[0]/usr/tabsSEG_TABSTRIP/tabpOBJS/ssubSUB1:SAPMKAL1:0306/sub:SAPMKAL1:0306/ctxtKGALK-SETID[3,67]").Text = "ZZ00" '成本要素组
  34. .findById("wnd[0]/usr/tabsSEG_TABSTRIP/tabpOBJS/ssubSUB1:SAPMKAL1:0306/sub:SAPMKAL1:0306/ctxtKGALK-VALMIN[1,17]").Text = arr1(x, 7) '发送者成本中心从
  35. .findById("wnd[0]/usr/tabsSEG_TABSTRIP/tabpOBJS/ssubSUB1:SAPMKAL1:0306/sub:SAPMKAL1:0306/ctxtKGALK-VALMAX[1,42]").Text = arr1(x, 8) '发送者成本中心至
  36. .findById("wnd[0]/usr/tabsSEG_TABSTRIP/tabpOBJS/ssubSUB1:SAPMKAL1:0306/sub:SAPMKAL1:0306/ctxtKGALK-SETID[1,67]").Text = arr1(x, 9) '发送者成本中心组
  37. .findById("wnd[0]/usr/tabsSEG_TABSTRIP/tabpOBJS/ssubSUB1:SAPMKAL1:0306/sub:SAPMKAL1:0306/ctxtKGALK-VALMIN[7,17]").Text = arr1(x, 10) '接收方成本中心从
  38. .findById("wnd[0]/usr/tabsSEG_TABSTRIP/tabpOBJS/ssubSUB1:SAPMKAL1:0306/sub:SAPMKAL1:0306/ctxtKGALK-VALMAX[7,42]").Text = arr1(x, 11) '接收方成本中心至
  39. .findById("wnd[0]/usr/tabsSEG_TABSTRIP/tabpOBJS/ssubSUB1:SAPMKAL1:0306/sub:SAPMKAL1:0306/ctxtKGALK-SETID[7,67]").Text = arr1(x, 12) '接收方成本中心组
  40. .findById("wnd[0]/usr/tabsSEG_TABSTRIP/tabpSEND").Select '发送方值
  41. .findById("wnd[0]/usr/tabsSEG_TABSTRIP/tabpSEND/ssubSUB1:SAPMKAL1:0451/sub:SAPMKAL1:0451/ctxtKGALK-VALMIN[0,17]").Text = "0" '版本
  42. .findById("wnd[0]/usr/tabsSEG_TABSTRIP/tabpRECE").Select '接收方追踪因素
  43. .findById("wnd[0]/usr/tabsSEG_TABSTRIP/tabpRECE/ssubSUB1:SAPMKAL1:0461/sub:SAPMKAL1:0461/ctxtKGALK-VALMIN[0,17]").Text = "ZZ01" '活动类型:从
  44. .findById("wnd[0]/usr/tabsSEG_TABSTRIP/tabpRECE/ssubSUB1:SAPMKAL1:0461/sub:SAPMKAL1:0461/ctxtKGALK-VALMAX[0,42]").Text = "ZZ06" '活动类型:到
  45. .findById("wnd[0]/usr/tabsSEG_TABSTRIP/tabpRWFC").Select '参考加权因素
  46. Next x
  47. End With
  48. MsgBox "运行成功!"
  49. End Sub
  50. Sub KSV3_显示实际分配()
  51. Dim iMg As VbMsgBoxStyle
  52. iMg = MsgBox("是否显示实际分配?" & Chr(10) & " " & Chr(10), vbYesNo, "KSV3")
  53. If iMg = 7 Then Exit Sub
  54. Dim SapGuiAuto As Object, AppSap As Object, Connection As Object, session As Object
  55. Set SapGuiAuto = GetObject("SAPGUI")
  56. Set AppSap = SapGuiAuto.GetScriptingEngine
  57. Set Connection = AppSap.Children(0)
  58. Set session = Connection.Children(0)
  59. Dim x As Integer, sr As String, rg As Range, arr1(), arr2(), brr(), k As Long, iMax As Integer, i As Integer, j As Integer
  60. ReDim arr2(1 To 100000, 1 To 18)
  61. ReDim brr(1 To 3)
  62. sr = "KSV3"
  63. Set rg = ThisWorkbook.Sheets(sr).UsedRange.Find("*")
  64. If rg Is Nothing Then
  65. MsgBox "错误!表【" & sr & "】中无数据!"
  66. Exit Sub
  67. End If
  68. arr1 = rg.CurrentRegion.Value
  69. With session
  70. For x = 2 To UBound(arr1)
  71. If arr1(x, 1) <> "" Then
  72. .findById("wnd[0]").maximize
  73. .findById("wnd[0]/tbar[0]/okcd").Text = "/NKSV3" '显示实际分配
  74. .findById("wnd[0]").sendVKey 0 'Enter
  75. .findById("wnd[0]/usr/ctxtRKAL1-KSCYC").Text = arr1(x, 1) '循环名
  76. .findById("wnd[0]/usr/ctxtT811C-SDATE").Text = arr1(x, 2) '开始日期
  77. .findById("wnd[0]").sendVKey 0 'Enter
  78. brr(1) = .findById("wnd[0]/usr/txtRKAL1-CTXT").Text '循环名描述
  79. brr(2) = .findById("wnd[0]/usr/ctxtT811C-SDATE").Text '开始时间
  80. brr(3) = .findById("wnd[0]/usr/ctxtT811C-EDATE").Text '结束时间
  81. .findById("wnd[0]/tbar[1]/btn[6]").press '第一个段
  82. .findById("wnd[0]/mbar/menu[2]/menu[1]").Select '转到>>概述段
  83. j = .findById("wnd[1]/usr/txtRCEVM-ENTRIES").Text '段号
  84. .findById("wnd[1]/tbar[0]/btn[12]").press '取消段概览
  85. i = 0
  86. Do
  87. On Error Resume Next
  88. k = k + 1
  89. i = i + 1
  90. arr2(k, 1) = arr1(x, 1)
  91. arr2(k, 2) = brr(1)
  92. arr2(k, 3) = brr(2)
  93. arr2(k, 4) = brr(3)
  94. arr2(k, 5) = .findById("wnd[0]/usr/txtKGALS-NAME").Text '段名
  95. arr2(k, 6) = .findById("wnd[0]/usr/txtKGALS-TXT").Text '段名描述
  96. arr2(k, 7) = .findById("wnd[0]/usr/chkKGALS-ACTIVE").Selected '锁定标识符
  97. arr2(k, 8) = Trim(.findById("wnd[0]/usr/tabsSEG_TABSTRIP/tabpSQHD/ssubSUB1:SAPMKAL1:0308/cmbRKAL1-RCDATAFLG").Text) '可变部分类型
  98. .findById("wnd[0]/usr/tabsSEG_TABSTRIP/tabpOBJS").Select '发送方/接收方
  99. arr2(k, 9) = .findById("wnd[0]/usr/tabsSEG_TABSTRIP/tabpOBJS/ssubSUB1:SAPMKAL1:0306/sub:SAPMKAL1:0306/ctxtKGALK-SETID[3,67]").Text '成本要素组
  100. arr2(k, 10) = .findById("wnd[0]/usr/tabsSEG_TABSTRIP/tabpOBJS/ssubSUB1:SAPMKAL1:0306/sub:SAPMKAL1:0306/ctxtKGALK-VALMIN[1,17]").Text '发送者成本中心从
  101. arr2(k, 11) = .findById("wnd[0]/usr/tabsSEG_TABSTRIP/tabpOBJS/ssubSUB1:SAPMKAL1:0306/sub:SAPMKAL1:0306/ctxtKGALK-VALMAX[1,42]").Text '发送者成本中心至
  102. arr2(k, 12) = .findById("wnd[0]/usr/tabsSEG_TABSTRIP/tabpOBJS/ssubSUB1:SAPMKAL1:0306/sub:SAPMKAL1:0306/ctxtKGALK-SETID[1,67]").Text '发送者成本中心组
  103. arr2(k, 13) = .findById("wnd[0]/usr/tabsSEG_TABSTRIP/tabpOBJS/ssubSUB1:SAPMKAL1:0306/sub:SAPMKAL1:0306/ctxtKGALK-VALMIN[7,17]").Text '接收方成本中心从
  104. arr2(k, 14) = .findById("wnd[0]/usr/tabsSEG_TABSTRIP/tabpOBJS/ssubSUB1:SAPMKAL1:0306/sub:SAPMKAL1:0306/ctxtKGALK-VALMAX[7,42]").Text '接收方成本中心至
  105. arr2(k, 15) = .findById("wnd[0]/usr/tabsSEG_TABSTRIP/tabpOBJS/ssubSUB1:SAPMKAL1:0306/sub:SAPMKAL1:0306/ctxtKGALK-SETID[7,67]").Text '接收方成本中心组
  106. .findById("wnd[0]/usr/tabsSEG_TABSTRIP/tabpSEND").Select '发送方值
  107. arr2(k, 16) = .findById("wnd[0]/usr/tabsSEG_TABSTRIP/tabpSEND/ssubSUB1:SAPMKAL1:0451/sub:SAPMKAL1:0451/ctxtKGALK-VALMIN[0,17]").Text '版本
  108. .findById("wnd[0]/usr/tabsSEG_TABSTRIP/tabpRECE").Select '接收方追踪因素
  109. arr2(k, 17) = .findById("wnd[0]/usr/tabsSEG_TABSTRIP/tabpRECE/ssubSUB1:SAPMKAL1:0461/sub:SAPMKAL1:0461/ctxtKGALK-VALMIN[0,17]").Text '活动类型:从
  110. arr2(k, 18) = .findById("wnd[0]/usr/tabsSEG_TABSTRIP/tabpRECE/ssubSUB1:SAPMKAL1:0461/sub:SAPMKAL1:0461/ctxtKGALK-VALMAX[0,42]").Text '活动类型:到
  111. .findById("wnd[0]/usr/tabsSEG_TABSTRIP/tabpRWFC").Select '参考加权因素
  112. .findById("wnd[0]/tbar[1]/btn[19]").press
  113. Loop Until i >= j
  114. .findById("wnd[1]/tbar[0]/btn[0]").press
  115. End If
  116. Next x
  117. End With
  118. With ThisWorkbook.Sheets("KSV3")
  119. .AutoFilterMode = False
  120. With .Cells(1, 3)
  121. .Resize(1, UBound(arr2, 2)).EntireColumn.ClearContents
  122. .Resize(1, UBound(arr2, 2)) = Split("循环名;循环名描述;开始时间;结束时间;段名;段名描述;锁定标识符;可变部分类型;成本要素组;发送者成本中心从;发送者成本中心至;发送者成本中心组;接收方成本中心从;接收方成本中心至;接收方成本中心组;版本;活动类型从;活动类型到", ";")
  123. If k > 0 Then .Cells(2, 1).Resize(k, UBound(arr2, 2)) = arr2
  124. End With
  125. End With
  126. MsgBox "成功"
  127. End Sub
  128. Sub KSV2_修改实际分配()
  129. Dim iMg As VbMsgBoxStyle
  130. iMg = MsgBox("是否修改实际分配?" & Chr(10) & " " & Chr(10), vbYesNo, "KSV2")
  131. If iMg = 7 Then Exit Sub
  132. Dim SapGuiAuto As Object, AppSap As Object, Connection As Object, session As Object
  133. Set SapGuiAuto = GetObject("SAPGUI")
  134. Set AppSap = SapGuiAuto.GetScriptingEngine
  135. Set Connection = AppSap.Children(0)
  136. Set session = Connection.Children(0)
  137. Dim x As Integer, sr As String, rg As Range, arr1(), arr2(), brr(), k As Long, iMax As Integer, i As Integer, j As Integer
  138. ReDim arr2(1 To 100000, 1 To 18)
  139. ReDim brr(1 To 3)
  140. sr = "KSV2"
  141. Set rg = ThisWorkbook.Sheets(sr).UsedRange.Find("*")
  142. If rg Is Nothing Then
  143. MsgBox "错误!表【" & sr & "】中无数据!"
  144. Exit Sub
  145. End If
  146. arr1 = rg.CurrentRegion.Value
  147. With session
  148. .findById("wnd[0]").maximize
  149. .findById("wnd[0]/tbar[0]/okcd").Text = "/NKSV2" '修改实际分配
  150. .findById("wnd[0]").sendVKey 0 'Enter
  151. .findById("wnd[0]/usr/ctxtRKAL1-KSCYC").Text = arr1(2, 1) '循环名
  152. .findById("wnd[0]/usr/ctxtT811C-SDATE").Text = arr1(2, 2) '开始日期
  153. .findById("wnd[0]").sendVKey 0 'Enter
  154. .findById("wnd[0]/usr/ctxtT811C-EDATE").Text = arr1(2, 3) '结束时间
  155. .findById("wnd[0]/usr/txtRKAL1-CTXT").Text = arr1(2, 4) '循环名描述
  156. .findById("wnd[0]/tbar[1]/btn[6]").press '第一个段
  157. .findById("wnd[0]/mbar/menu[2]/menu[1]").Select '转到>>概述段
  158. j = .findById("wnd[1]/usr/txtRCEVM-ENTRIES").Text '段号
  159. .findById("wnd[1]/tbar[0]/btn[12]").press '取消段概览
  160. For x = 1 To j '为了修改的时候不重名
  161. .findById("wnd[0]/usr/txtKGALS-NAME").Text = x
  162. If x <> j Then .findById("wnd[0]/tbar[1]/btn[19]").press '下一段
  163. Next x
  164. For x = 1 To j - 1 '回退到第一个段
  165. .findById("wnd[0]/tbar[1]/btn[18]").press '前一段
  166. Next x
  167. For x = 2 To UBound(arr1)
  168. i = i + 1
  169. .findById("wnd[0]/usr/txtKGALS-NAME").Text = arr1(x, 5) '段名 (不能大于十个字符)
  170. .findById("wnd[0]/usr/txtKGALS-TXT").Text = arr1(x, 6) '段名描述
  171. .findById("wnd[0]/usr/tabsSEG_TABSTRIP/tabpSQHD/ssubSUB1:SAPMKAL1:0308/cmbRKAL1-RCDATAFLG").Key = "7" '可变部分类型
  172. .findById("wnd[0]/usr/tabsSEG_TABSTRIP/tabpOBJS").Select '发送方/接收方
  173. .findById("wnd[0]/usr/tabsSEG_TABSTRIP/tabpOBJS/ssubSUB1:SAPMKAL1:0306/sub:SAPMKAL1:0306/ctxtKGALK-SETID[3,67]").Text = "ZZ00" '成本要素组
  174. .findById("wnd[0]/usr/chkKGALS-ACTIVE").Selected = arr1(x, 7) '锁定标识符
  175. .findById("wnd[0]/usr/tabsSEG_TABSTRIP/tabpOBJS/ssubSUB1:SAPMKAL1:0306/sub:SAPMKAL1:0306/ctxtKGALK-VALMIN[1,17]").Text = arr1(x, 8) '发送者成本中心从
  176. .findById("wnd[0]/usr/tabsSEG_TABSTRIP/tabpOBJS/ssubSUB1:SAPMKAL1:0306/sub:SAPMKAL1:0306/ctxtKGALK-VALMAX[1,42]").Text = arr1(x, 9) '发送者成本中心至
  177. .findById("wnd[0]/usr/tabsSEG_TABSTRIP/tabpOBJS/ssubSUB1:SAPMKAL1:0306/sub:SAPMKAL1:0306/ctxtKGALK-SETID[1,67]").Text = arr1(x, 10) '发送者成本中心组
  178. .findById("wnd[0]/usr/tabsSEG_TABSTRIP/tabpOBJS/ssubSUB1:SAPMKAL1:0306/sub:SAPMKAL1:0306/ctxtKGALK-VALMIN[7,17]").Text = arr1(x, 11) '接收方成本中心从
  179. .findById("wnd[0]/usr/tabsSEG_TABSTRIP/tabpOBJS/ssubSUB1:SAPMKAL1:0306/sub:SAPMKAL1:0306/ctxtKGALK-VALMAX[7,42]").Text = arr1(x, 12) '接收方成本中心至
  180. .findById("wnd[0]/usr/tabsSEG_TABSTRIP/tabpOBJS/ssubSUB1:SAPMKAL1:0306/sub:SAPMKAL1:0306/ctxtKGALK-SETID[7,67]").Text = arr1(x, 13) '接收方成本中心组
  181. .findById("wnd[0]/usr/tabsSEG_TABSTRIP/tabpSEND").Select '发送方值
  182. .findById("wnd[0]/usr/tabsSEG_TABSTRIP/tabpSEND/ssubSUB1:SAPMKAL1:0451/sub:SAPMKAL1:0451/ctxtKGALK-VALMIN[0,17]").Text = "0" '版本
  183. .findById("wnd[0]/usr/tabsSEG_TABSTRIP/tabpRECE").Select '接收方追踪因素
  184. .findById("wnd[0]/usr/tabsSEG_TABSTRIP/tabpRECE/ssubSUB1:SAPMKAL1:0461/sub:SAPMKAL1:0461/ctxtKGALK-VALMIN[0,17]").Text = "ZZ01" '活动类型:从
  185. .findById("wnd[0]/usr/tabsSEG_TABSTRIP/tabpRECE/ssubSUB1:SAPMKAL1:0461/sub:SAPMKAL1:0461/ctxtKGALK-VALMAX[0,42]").Text = "ZZ06" '活动类型:到
  186. .findById("wnd[0]/usr/tabsSEG_TABSTRIP/tabpRWFC").Select '参考加权因素
  187. If x = UBound(arr1) Then
  188. ElseIf i < j Then
  189. .findById("wnd[0]/tbar[1]/btn[19]").press
  190. Else
  191. .findById("wnd[0]/tbar[1]/btn[20]").press '增加段
  192. End If
  193. Next x
  194. Do While i < j '如果没有修改的必要则全部锁定掉
  195. i = i + 1
  196. .findById("wnd[0]/tbar[1]/btn[19]").press '下一个段
  197. .findById("wnd[0]/usr/chkKGALS-ACTIVE").Selected = "TRUE" '锁定标识符
  198. Loop
  199. End With
  200. MsgBox "成功"
  201. End Sub

与其他方式对比

  1. RPA脚本运行时不能操作键盘鼠标,VBA运行时可以操作SAP的其他界面,操作键盘鼠标也没影响。
  2. RPA 比如勾选复选框后需要等待程式运行,VBA不用
  3. VBA是在简体版本的Excel运行,与繁体版的Excel不通用,中文会有乱码。
  4. RPA运用更广泛,可以在其他应用运行。
  5. 与Tricentis对比

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/花生_TL007/article/detail/651248
推荐阅读
相关标签
  

闽ICP备14008679号