简体   繁体   English

使用 VBA 比较 excel 中的 2 个单元格进行 SAP GUI 脚本编写

[英]Compare 2 cells in excel using VBA for SAP GUI scripting

Currently I have a script to copy data from an excel sheet and paste it in SAP in the transaction code scal.目前我有一个脚本可以从 Excel 工作表中复制数据并将其粘贴到 SAP 中的事务代码 scal 中。

excel表格示例

What my code does, is filter the calendar selection by each ID (eg AA/AB) and then copy the data for Start Date and End Date into the scal ID.我的代码所做的是按每个 ID(例如 AA/AB)过滤日历选择,然后将开始日期和结束日期的数据复制到标度 ID 中。 Save the changes and then move onto the next ID.保存更改,然后移至下一个 ID。

Let's say I have an excel sheet with more than one entry for the same calendar ID.假设我有一个 Excel 工作表,其中包含多个相同日历 ID 的条目。 What can I do to make excel check if the next row has the same calendar ID as the current one, and then, instead of saving, copy and paste the start/end date for the same ID and only then moving onto the next one.我该怎么做才能让 excel 检查下一行是否与当前行具有相同的日历 ID,然后,而不是保存,复制并粘贴相同 ID 的开始/结束日期,然后才移动到下一行。

tldr:compare current column and the column below; tldr:比较当前列和下面的列; if both columns are same, continue with the changes;如果两列相同,则继续更改; otherwise save and go back.否则保存并返回。

Code is given below:代码如下:

Sub Main()
    Dim row As Integer
    Dim Session

'1. System name entry
    sSystemName = sSystemName_EntrySTD()
'2. Connect to open SAP session
    If bSessionConfirmSTD(Session, sSystemName) = False Then
        MsgBox sSystemName & " session is not opened", vbCritical
        End
    End If

'3. Display status in EXCEL
    application.StatusBar = "Attached to active session"

    On Error Resume Next

'4. for all rows in active sheet (Tabelle1)
    row = 2
    While Tabelle1.Cells(row, 1).Value <> ""

        Dim s1, s2, s3, s4, s5, s6, s7, s8, s9, s10, s11, s12, s13, s14, s15, s16, s17, s18, s19 As String
        Dim i As Integer

'5. Get value from first column of current row
        s1 = Tabelle1.Cells(row, 1).Value
        s2 = Tabelle1.Cells(row, 2).Value
        s3 = Tabelle1.Cells(row, 3).Value
        s4 = Tabelle1.Cells(row, 4).Value
        s5 = Tabelle1.Cells(row, 5).Value
        s6 = Tabelle1.Cells(row, 6).Value
        s7 = Tabelle1.Cells(row, 7).Value
        s8 = Tabelle1.Cells(row, 8).Value
        s9 = Tabelle1.Cells(row, 9).Value
        s10 = Tabelle1.Cells(row, 10).Value
        s11 = Tabelle1.Cells(row, 11).Value
        s12 = Tabelle1.Cells(row, 12).Value
        s13 = Tabelle1.Cells(row, 13).Value
        s14 = Tabelle1.Cells(row, 14).Value
        s15 = Tabelle1.Cells(row, 15).Value
        s16 = Tabelle1.Cells(row, 16).Value
        s17 = Tabelle1.Cells(row, 17).Value
        s18 = Tabelle1.Cells(row, 18).Value
        s19 = Tabelle1.Cells(row, 19).Value

'6. Update status in EXCEL
        application.StatusBar = "Processing row " & RTrim(LTrim(Str(row))) & ": " & s1

'BEGIN Paste your script here
'===============================================================================================================

Session.findById("wnd[0]").maximize
Session.findById("wnd[0]/tbar[0]/okcd").Text = "scal"
Session.findById("wnd[0]").sendVKey 0
Session.findById("wnd[0]/usr/radFMEN-FABKAL").Select
Session.findById("wnd[0]/usr/radFMEN-FABKAL").SetFocus
Session.findById("wnd[0]/usr/btnUPDATE").press
Session.findById("wnd[0]/usr/cntlGRID1/shellcont/shell").currentCellRow = -1
Session.findById("wnd[0]/usr/cntlGRID1/shellcont/shell").selectColumn "IDENT"
Session.findById("wnd[0]/tbar[1]/btn[38]").press
Session.findById("wnd[1]/usr/ssub%_SUBSCREEN_FREESEL:SAPLSSEL:1105/ctxt%%DYN001-LOW").Text = s1
Session.findById("wnd[1]/usr/ssub%_SUBSCREEN_FREESEL:SAPLSSEL:1105/ctxt%%DYN001-LOW").caretPosition = 2
Session.findById("wnd[1]").sendVKey 0
Session.findById("wnd[0]/usr/cntlGRID1/shellcont/shell").currentCellColumn = ""
Session.findById("wnd[0]/usr/cntlGRID1/shellcont/shell").selectedRows = "0"
Session.findById("wnd[0]/tbar[1]/btn[7]").press
Session.findById("wnd[0]/tbar[1]/btn[17]").press
Session.findById("wnd[0]/tbar[1]/btn[13]").press
Session.findById("wnd[1]/usr/chkTIFAB-ARBTAG").Selected = s4
Session.findById("wnd[1]/usr/ctxtTIFAB-DATUMVON").Text = s2
Session.findById("wnd[1]/usr/ctxtTIFAB-DATUMBIS").Text = s3
Session.findById("wnd[1]/usr/txtTFAIT-LTEXT").Text = s5
Session.findById("wnd[1]/usr/txtTFAIT-LTEXT").SetFocus
Session.findById("wnd[1]/usr/txtTFAIT-LTEXT").caretPosition = 11
Session.findById("wnd[1]").sendVKey 0



'===============================================================================================================
'END Paste your script here
Tabelle1.Cells(row, 15).Value = Session.findById("wnd[0]/sbar").Text

'7. Log processed entries in Tabelle1
        Tabelle1.Cells(row, 16).Value = "Done"
'8. Continue with next row
        row = row + 1

    Wend

    application.StatusBar = "Processing finished"

    On Error GoTo 0
    Exit Sub

info:
    MsgBox "error on line " & row

End Sub

No idea if I understood it correctly, what should be compared actually.不知道我是否理解正确,实际上应该比较什么。 But one possible solution might look like this:但一种可能的解决方案可能如下所示:

. . .
'4. for all rows in active sheet (Tabelle1)
row = 2
While Tabelle1.Cells(row, 1).Value <> ""

    Dim s1, s2, s3, s4, s5, s6, s7, s8, s9, s10, s11, s12, s13, s14, s15, s16, s17, s18, s19 As String
    Dim i As Integer

'5. Get value from first column of current row
    s1 = Tabelle1.Cells(row, 1).Value
    if Tabelle1.Cells(row + 1, 1).Value <> "" then
       if s1 = Tabelle1.Cells(row + 1, 1).Value then row = row + 1
    end if
    s2 = Tabelle1.Cells(row, 2).Value
    . . .

Regards, ScriptMan问候, ScriptMan

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM