Szukowny
New Member
- Joined
- Feb 16, 2023
- Messages
- 5
- Office Version
- 2013
- 2011
- 2010
- 2007
- Platform
- Windows
Hi Everyone , I'm new to macros etc , I have did the code which is serving the purpose , but it is slow , is there any chance anyone could help me optimize the code below? Thanks :
Sub NavigateToApplicationWindow()
Application.Calculation = xlCalculationManual
Application.EnableEvents = False
Dim i As Integer
Dim e As Long
Dim rng As Range
Dim cell As Range
Dim duplicateFound As Boolean
Dim ws As Worksheet
Set ws = ActiveSheet
Dim lastRow As Long
Set rng = Range("A2:A100")
' Wait for the window to become active
Application.Wait (Now + TimeValue("0:00:02"))
' Activate the SAPlogon.exe application window with the title "SAP logon"
AppActivate "List of Outbound Deliveries"
' Wait for the window to become active
Application.Wait (Now + TimeValue("0:00:02"))
' Send the key arrow up
SendKeys "^{RIGHT 2}"
' Send the key arrow right
SendKeys "{F2}"
Application.Wait (Now + TimeValue("0:00:02"))
' Send the key arrow down
SendKeys "+{RIGHT 11}"
Application.Wait (Now + TimeValue("0:00:01"))
SendKeys "^c"
AppActivate "Microsoft Excel"
ThisWorkbook.Sheets("Left To Pack").Activate
lastRow = ThisWorkbook.Sheets("Left To Pack").Cells(Rows.Count, "A").End(xlUp).Row + 1
Application.Wait (Now + TimeValue("0:00:01"))
ThisWorkbook.Sheets("Left To Pack").Cells(lastRow, "A").PasteSpecial
Application.Wait (Now + TimeValue("0:00:01"))
SendKeys "%{TAB}"
Application.Wait (Now + TimeValue("0:00:01"))
SendKeys "{TAB 3}"
SendKeys "^{UP}"
SendKeys "+{Left 2}"
Application.Wait (Now + TimeValue("0:00:01"))
SendKeys "^c"
AppActivate "Microsoft Excel"
ThisWorkbook.Sheets("Left To Pack").Activate
lastRow = ThisWorkbook.Sheets("Left To Pack").Cells(Rows.Count, "B").End(xlUp).Row + 1
Application.Wait (Now + TimeValue("0:00:01"))
ThisWorkbook.Sheets("Left To Pack").Cells(lastRow, "B").PasteSpecial
Application.Wait (Now + TimeValue("0:00:01"))
SendKeys "%{TAB}"
Application.Wait (Now + TimeValue("0:00:01"))
SendKeys "^{UP}"
SendKeys "+{RIGHT 3}"
Application.Wait (Now + TimeValue("0:00:01"))
SendKeys "^c"
AppActivate "Microsoft Excel"
ThisWorkbook.Sheets("Left To Pack").Activate
lastRow = ThisWorkbook.Sheets("Left To Pack").Cells(Rows.Count, "C").End(xlUp).Row + 1
Application.Wait (Now + TimeValue("0:00:01"))
ThisWorkbook.Sheets("Left To Pack").Cells(lastRow, "C").PasteSpecial
Application.Wait (Now + TimeValue("0:00:01"))
SendKeys "%{TAB}"
Application.Wait (Now + TimeValue("0:00:01"))
SendKeys "{ESC}"
Application.Wait (Now + TimeValue("0:00:01"))
SendKeys "{DOWN}"
Application.Wait (Now + TimeValue("0:00:01"))
Do While i <= 100
SendKeys "{F2}"
Application.Wait (Now + TimeValue("0:00:02"))
' Send the key arrow down
SendKeys "+{RIGHT 11}"
Application.Wait (Now + TimeValue("0:00:01"))
SendKeys "^c"
AppActivate "Microsoft Excel"
ThisWorkbook.Sheets("Left To Pack").Activate
lastRow = ThisWorkbook.Sheets("Left To Pack").Cells(Rows.Count, "A").End(xlUp).Row + 1
Application.Wait (Now + TimeValue("0:00:01"))
ThisWorkbook.Sheets("Left To Pack").Cells(lastRow, "A").PasteSpecial
Application.Wait (Now + TimeValue("0:00:01"))
SendKeys "%{TAB}"
Application.Wait (Now + TimeValue("0:00:01"))
SendKeys "{TAB 3}"
SendKeys "^{UP}"
SendKeys "+{Left 2}"
Application.Wait (Now + TimeValue("0:00:01"))
SendKeys "^c"
AppActivate "Microsoft Excel"
ThisWorkbook.Sheets("Left To Pack").Activate
lastRow = ThisWorkbook.Sheets("Left To Pack").Cells(Rows.Count, "B").End(xlUp).Row + 1
Application.Wait (Now + TimeValue("0:00:01"))
ThisWorkbook.Sheets("Left To Pack").Cells(lastRow, "B").PasteSpecial
Application.Wait (Now + TimeValue("0:00:01"))
SendKeys "%{TAB}"
Application.Wait (Now + TimeValue("0:00:01"))
SendKeys "^{UP}"
SendKeys "+{RIGHT 7}"
Application.Wait (Now + TimeValue("0:00:01"))
SendKeys "^c"
AppActivate "Microsoft Excel"
ThisWorkbook.Sheets("Left To Pack").Activate
lastRow = ThisWorkbook.Sheets("Left To Pack").Cells(Rows.Count, "C").End(xlUp).Row + 1
Application.Wait (Now + TimeValue("0:00:01"))
ThisWorkbook.Sheets("Left To Pack").Cells(lastRow, "C").PasteSpecial
Application.Wait (Now + TimeValue("0:00:01"))
For Each cell In rng
If Application.CountIf(rng, cell.Value) > 1 Then
duplicateFound = True
Exit Do
End If
Next cell
SendKeys "%{TAB}"
Application.Wait (Now + TimeValue("0:00:01"))
SendKeys "{ESC}"
Application.Wait (Now + TimeValue("0:00:02"))
SendKeys "{DOWN}"
Application.Wait (Now + TimeValue("0:00:01"))
Loop
lastRow = ws.Cells(ws.Rows.Count, "B").End(xlUp).Row
For e = lastRow To 2 Step -1 ' Loop through each row from bottom to top
If ws.Range("B" & e).Value <> 0 Or ws.Range("C" & e).Value <> 0 Then ' Check if values in columns B and C are not 0
ws.Rows(e).Delete ' Delete the row if values in columns B and C are not 0
End If
Next e
Application.Calculation = xlCalculationAutomatic
Application.EnableEvents = True
MsgBox "Finish"
End Sub
Sub NavigateToApplicationWindow()
Application.Calculation = xlCalculationManual
Application.EnableEvents = False
Dim i As Integer
Dim e As Long
Dim rng As Range
Dim cell As Range
Dim duplicateFound As Boolean
Dim ws As Worksheet
Set ws = ActiveSheet
Dim lastRow As Long
Set rng = Range("A2:A100")
' Wait for the window to become active
Application.Wait (Now + TimeValue("0:00:02"))
' Activate the SAPlogon.exe application window with the title "SAP logon"
AppActivate "List of Outbound Deliveries"
' Wait for the window to become active
Application.Wait (Now + TimeValue("0:00:02"))
' Send the key arrow up
SendKeys "^{RIGHT 2}"
' Send the key arrow right
SendKeys "{F2}"
Application.Wait (Now + TimeValue("0:00:02"))
' Send the key arrow down
SendKeys "+{RIGHT 11}"
Application.Wait (Now + TimeValue("0:00:01"))
SendKeys "^c"
AppActivate "Microsoft Excel"
ThisWorkbook.Sheets("Left To Pack").Activate
lastRow = ThisWorkbook.Sheets("Left To Pack").Cells(Rows.Count, "A").End(xlUp).Row + 1
Application.Wait (Now + TimeValue("0:00:01"))
ThisWorkbook.Sheets("Left To Pack").Cells(lastRow, "A").PasteSpecial
Application.Wait (Now + TimeValue("0:00:01"))
SendKeys "%{TAB}"
Application.Wait (Now + TimeValue("0:00:01"))
SendKeys "{TAB 3}"
SendKeys "^{UP}"
SendKeys "+{Left 2}"
Application.Wait (Now + TimeValue("0:00:01"))
SendKeys "^c"
AppActivate "Microsoft Excel"
ThisWorkbook.Sheets("Left To Pack").Activate
lastRow = ThisWorkbook.Sheets("Left To Pack").Cells(Rows.Count, "B").End(xlUp).Row + 1
Application.Wait (Now + TimeValue("0:00:01"))
ThisWorkbook.Sheets("Left To Pack").Cells(lastRow, "B").PasteSpecial
Application.Wait (Now + TimeValue("0:00:01"))
SendKeys "%{TAB}"
Application.Wait (Now + TimeValue("0:00:01"))
SendKeys "^{UP}"
SendKeys "+{RIGHT 3}"
Application.Wait (Now + TimeValue("0:00:01"))
SendKeys "^c"
AppActivate "Microsoft Excel"
ThisWorkbook.Sheets("Left To Pack").Activate
lastRow = ThisWorkbook.Sheets("Left To Pack").Cells(Rows.Count, "C").End(xlUp).Row + 1
Application.Wait (Now + TimeValue("0:00:01"))
ThisWorkbook.Sheets("Left To Pack").Cells(lastRow, "C").PasteSpecial
Application.Wait (Now + TimeValue("0:00:01"))
SendKeys "%{TAB}"
Application.Wait (Now + TimeValue("0:00:01"))
SendKeys "{ESC}"
Application.Wait (Now + TimeValue("0:00:01"))
SendKeys "{DOWN}"
Application.Wait (Now + TimeValue("0:00:01"))
Do While i <= 100
SendKeys "{F2}"
Application.Wait (Now + TimeValue("0:00:02"))
' Send the key arrow down
SendKeys "+{RIGHT 11}"
Application.Wait (Now + TimeValue("0:00:01"))
SendKeys "^c"
AppActivate "Microsoft Excel"
ThisWorkbook.Sheets("Left To Pack").Activate
lastRow = ThisWorkbook.Sheets("Left To Pack").Cells(Rows.Count, "A").End(xlUp).Row + 1
Application.Wait (Now + TimeValue("0:00:01"))
ThisWorkbook.Sheets("Left To Pack").Cells(lastRow, "A").PasteSpecial
Application.Wait (Now + TimeValue("0:00:01"))
SendKeys "%{TAB}"
Application.Wait (Now + TimeValue("0:00:01"))
SendKeys "{TAB 3}"
SendKeys "^{UP}"
SendKeys "+{Left 2}"
Application.Wait (Now + TimeValue("0:00:01"))
SendKeys "^c"
AppActivate "Microsoft Excel"
ThisWorkbook.Sheets("Left To Pack").Activate
lastRow = ThisWorkbook.Sheets("Left To Pack").Cells(Rows.Count, "B").End(xlUp).Row + 1
Application.Wait (Now + TimeValue("0:00:01"))
ThisWorkbook.Sheets("Left To Pack").Cells(lastRow, "B").PasteSpecial
Application.Wait (Now + TimeValue("0:00:01"))
SendKeys "%{TAB}"
Application.Wait (Now + TimeValue("0:00:01"))
SendKeys "^{UP}"
SendKeys "+{RIGHT 7}"
Application.Wait (Now + TimeValue("0:00:01"))
SendKeys "^c"
AppActivate "Microsoft Excel"
ThisWorkbook.Sheets("Left To Pack").Activate
lastRow = ThisWorkbook.Sheets("Left To Pack").Cells(Rows.Count, "C").End(xlUp).Row + 1
Application.Wait (Now + TimeValue("0:00:01"))
ThisWorkbook.Sheets("Left To Pack").Cells(lastRow, "C").PasteSpecial
Application.Wait (Now + TimeValue("0:00:01"))
For Each cell In rng
If Application.CountIf(rng, cell.Value) > 1 Then
duplicateFound = True
Exit Do
End If
Next cell
SendKeys "%{TAB}"
Application.Wait (Now + TimeValue("0:00:01"))
SendKeys "{ESC}"
Application.Wait (Now + TimeValue("0:00:02"))
SendKeys "{DOWN}"
Application.Wait (Now + TimeValue("0:00:01"))
Loop
lastRow = ws.Cells(ws.Rows.Count, "B").End(xlUp).Row
For e = lastRow To 2 Step -1 ' Loop through each row from bottom to top
If ws.Range("B" & e).Value <> 0 Or ws.Range("C" & e).Value <> 0 Then ' Check if values in columns B and C are not 0
ws.Rows(e).Delete ' Delete the row if values in columns B and C are not 0
End If
Next e
Application.Calculation = xlCalculationAutomatic
Application.EnableEvents = True
MsgBox "Finish"
End Sub