[英]Compare 2 cells in excel using VBA for SAP GUI scripting
目前我有一個腳本可以從 Excel 工作表中復制數據並將其粘貼到 SAP 中的事務代碼 scal 中。
我的代碼所做的是按每個 ID(例如 AA/AB)過濾日歷選擇,然后將開始日期和結束日期的數據復制到標度 ID 中。 保存更改,然后移至下一個 ID。
假設我有一個 Excel 工作表,其中包含多個相同日歷 ID 的條目。 我該怎么做才能讓 excel 檢查下一行是否與當前行具有相同的日歷 ID,然后,而不是保存,復制並粘貼相同 ID 的開始/結束日期,然后才移動到下一行。
tldr:比較當前列和下面的列; 如果兩列相同,則繼續更改; 否則保存並返回。
代碼如下:
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
不知道我是否理解正確,實際上應該比較什么。 但一種可能的解決方案可能如下所示:
. . .
'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
. . .
問候, ScriptMan
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.