VBA Loop Help - A Fresh one!

sanantonio

Board Regular
Joined
Oct 26, 2021
Messages
124
Office Version
  1. 365
Platform
  1. Windows
Hi All,

I'm trying to recycle a loop that @NateSC helped me with previously. This might sound premature but I only have limited testing time (4 hours on Wednesday afternoon) where I'll have access to the systems I need.

What it should do is loop through the data in the table below and "sendkey" it to a module within our client-specific software.

1684160433448.png


This list could be as many as 250 lines of entry or 1 or 2 lines of entry depending on the week and decisions made by other business divisions.

1684160531264.png


This is the code that I've adapted that I think will work (Like I say I can't test this until Wednesday). Can anyone see anything that'll go wrong?

VBA Code:
Sub SFO_Entry()

Dim myAlo As Range
Dim myRow, myCount As Long
Dim myWindow As String
Dim myItem, myQuantity As Range
Dim mySlot As Variant
Dim hWnd As Long
Dim Row1 As Long, Row2 As Long, Num1 As Long, Counter1 As Long
Dim Item As Range, Items As Range
Dim ItemCode As String
Dim Window1 As String, Window2 As String, Window3 As String


If Cell Is Nothing Then

    'Select MDE Module
    Window1 = "[SFO000] - Store Forms Module - DB: USWH00" & Sheets("Cover").Range("J13").Value & "L (USWH00" & Sheets("Cover").Range("J13").Value & "L)  Schema: WAWIADM Role: R_WAWI"
    Window2 = "[SFO002] Store Claim Delivery"
    hWnd = FindWindow(vbNullString, Window1)
    SetForegroundWindow hWnd
    If hWnd > 0 Then
        Else
            MsgBox ("SFO Module cannot be found.")
            myCancel = "Cancel"
    Exit Sub
    End If


' Updated code below this point !!!
Dim numStores As Integer
Dim i As Integer
Dim j As Integer
Dim temp(10) As Variant
Dim ws As Worksheet
Dim myLastCol As Integer
Dim myFirstCol As Integer
Dim myLastRow As Integer
Dim myFirstRow As Integer
Dim myCurrentRow As Integer

    Set ws = ActiveSheet
    
    ' Finds first and last rows and columns with data
    myLastRow = ws.Cells(Rows.Count, 4).End(xlUp).Row
    myFirstRow = ws.Cells(myLastRow, 4).End(xlUp).Row
    myLastCol = ws.Cells(myFirstRow, Columns.Count).End(xlToLeft).Column
    myFirstCol = ws.Cells(myFirstRow, myLastCol).End(xlToLeft).Column
    
  
    'Loop 1, enters store, document number, document date
    For i = 1 To numStores
        
        
        'Store
        temp(1) = ws.Cells(myFirstRow, myFirstCol - 1 + i * 2).Value
        SendKeys temp(1)
        SendKeys ("{TAB}")
        Application.Wait (Now + TimeValue("00:00:01"))
        'Document Nr
        temp(2) = ws.Cells(myFirstRow + 3, myFirstCol - 1 + i * 2).Value
        SendKeys temp(2)
        SendKeys ("{TAB}")
        Application.Wait (Now + TimeValue("00:00:01"))
        'Picklist Nr
        temp(3) = ws.Cells(myFirstRow + 2, myFirstCol - 1 + i * 2).Value
        SendKeys temp(3)
        SendKeys ("{TAB}")
        Application.Wait (Now + TimeValue("00:00:01"))
        'Item
        temp(4) = ws.Cells(myFirstRow + 1, myFirstCol - 1 + i * 2).Value
        SendKeys temp(4)
        SendKeys ("{TAB}")
        Application.Wait (Now + TimeValue("00:00:01"))
        'Case Size
        temp(4) = ws.Cells(myFirstRow + 1, myFirstCol - 1 + i * 2).Value
        SendKeys temp(5)
        SendKeys ("{TAB}")
        Application.Wait (Now + TimeValue("00:00:01"))
        'Case Qty
        temp(4) = ws.Cells(myFirstRow + 1, myFirstCol - 1 + i * 2).Value
        SendKeys temp(6)
        SendKeys ("{TAB}")
        Application.Wait (Now + TimeValue("00:00:01"))
        'Units Qty
        temp(4) = ws.Cells(myFirstRow + 1, myFirstCol - 1 + i * 2).Value
        SendKeys temp(7)
        SendKeys ("{TAB}")
        Application.Wait (Now + TimeValue("00:00:01"))
        
        'Then inserts
        SendKeys ("{TAB}")
        SendKeys ("{TAB}")
        SendKeys (" ")
        Application.Wait (Now + TimeValue("00:00:01"))
        SendKeys ("{F3}")
          
    Next i

SendKeys ("{ESC}")
MsgBox "Action Complete"

End If
End Sub

Thanks in advance!
 
The second check can go pretty much anywhere inside the i loop. If you put it near the beginning of the loop, the program will end before entering data for that line. If you put it at the end of the loop, it will enter the data and then stop. So it depends on your design for the program.

If you want to set any of the IF statements to allow the code to end, make them look like the long form IF (hit enter after the "Then" and then add an "End If") and put an END statement after your message box:
VBA Code:
        If testValue = 6 Or testValue = 11 Or testValue = 12 Then
            MsgBox "The value of " & testValue & " was found."
            End
        End If

Please note this will STOP all program execution at this point. If you just want to skip that particular line, but process all the other lines after, this is completely possible. You just need to make the if at the top of the loop, add an ELSE, and put all of your data transfer steps inside the ELSE:

VBA Code:
For i = 1 To numStores
    If testValue = 6 Or testValue = 11 Or testValue = 12 Then
         MsgBox "The value of " & testValue & " was found."
   Else
        'Store
        temp(1) = ws.Cells(myFirstRow + i, myFirstCol).Value
        SendKeys temp(1)
         ....... ' INSERT ALL THE CODE TO TRANSFER THE DATA HERE
        Then inserts
        SendKeys ("{TAB}")
        SendKeys ("{TAB}")
        SendKeys (" ")
        Application.Wait (Now + TimeValue("00:00:01"))
        'SendKeys ("{F3}")

   End if
next i
Think through exactly how you want your code to respond and this will determine how you need to setup the IF statements.
 
Upvote 0

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
With the "End"s and "End If"s in those locations I get debug errors back.

If I run it without them the error check works, but will still proceed regardless if it's found the error.

Any ideas?
 
Upvote 0
If you are trying to use the single line versions, you do not need a End If.
VBA Code:
If testValue = 6 Or testValue = 11 Or testValue = 12 Then MsgBox "The value of " & testValue & " was found."

If you want to do more than one thing, you need the End If.
VBA Code:
If testValue = 6 Or testValue = 11 Or testValue = 12 Then
                MsgBox "The value of " & testValue & " was found."
                End
End If

Which version are you using?
 
Upvote 0
Also I noticed the code I posted said:
VBA Code:
         ....... ' INSERT ALL THE CODE TO TRANSFER THE DATA HERE
        Then inserts
        SendKeys ("{TAB}")

The "Then inserts" is a bad line. You can completely delete that line. I think I started to write a comment there and then accidentally left that. This line will mess things up.
 
Upvote 0
VBA Code:
    'Loop 1, enters store, document number, Picklist number, item, case size, qty case, qty units, enters
    For i = 1 To numStores
        
        testValue = ws.Cells(myFirstRow + i, myFirstCol + 8)
If testValue = 6 Or testValue = 11 Or testValue = 12 Then MsgBox "Items on status " & testValue & " were found."

        
hWnd2 = FindWindow(vbNullString, Window2)
        'Store
        temp(1) = ws.Cells(myFirstRow + i, myFirstCol).Value
        SendKeys temp(1)
        SendKeys ("{TAB}")
                'Document Nr
        temp(2) = ws.Cells(myFirstRow + i, myFirstCol + 1).Value
        If Application.WorksheetFunction.CountIf(ws.Range("E" & myFirstRow & ":E" & myLastRow), temp(2)) > 1 Then MsgBox temp(2) & " is repeated"
        SendKeys temp(2)
        SendKeys ("{TAB}")
                'Picklist Nr
        temp(3) = ws.Cells(myFirstRow + i, myFirstCol + 2).Value
        SendKeys temp(3)
        SendKeys ("{TAB}")
                'Item
        temp(4) = ws.Cells(myFirstRow + i, myFirstCol + 3).Value
        SendKeys temp(4)
        SendKeys ("{TAB}")
        Application.Wait (Now + TimeValue("00:00:01"))
                'Case Size
        temp(5) = ws.Cells(myFirstRow + i, myFirstCol + 4).Value
        SendKeys temp(5)
        SendKeys ("{TAB}")
                'Case Qty
        temp(6) = ws.Cells(myFirstRow + i, myFirstCol + 5).Value
        SendKeys temp(6)
        SendKeys ("{TAB}")
                'Units Qty
        temp(7) = ws.Cells(myFirstRow + i, myFirstCol + 6).Value
        SendKeys temp(7)
        SendKeys ("{TAB}")
        Application.Wait (Now + TimeValue("00:00:01"))

        'Then select "Under"
        SendKeys ("{TAB}")
        SendKeys ("{TAB}")
        SendKeys (" ")
        Application.Wait (Now + TimeValue("00:00:01"))
        SendKeys ("{ENTER}")
        Application.Wait (Now + TimeValue("00:00:01"))
        SendKeys ("{ENTER}")
        SendKeys ("{F3}")
        Application.Wait (Now + TimeValue("00:00:01"))
          
                  
    Next i

SendKeys ("{ESC}")

This is my code inside the loop. It runs fine and performs the checks, but it doesn't stop running once it's run the checks it just gives you a message box You click ok) and it finishes out the macro.
 
Upvote 0
Replace:
VBA Code:
If testValue = 6 Or testValue = 11 Or testValue = 12 Then MsgBox "Items on status " & testValue & " were found."

with:
VBA Code:
If testValue = 6 Or testValue = 11 Or testValue = 12 Then
                MsgBox "The value of " & testValue & " was found."
                End
End If
 
Upvote 0

Forum statistics

Threads
1,214,584
Messages
6,120,384
Members
448,956
Latest member
JPav

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top