VBA code to identify one open workbook only working when certain workbook is open in a specific order?

Indominus

Board Regular
Joined
Jul 11, 2020
Messages
160
Office Version
  1. 2016
Platform
  1. Windows
Hello. So I have this VBA code to identify two open workbooks and match data across them. One is Routing. One is Pickorder. This one is a unique name daily but always has PickOrder in it along with the current date. When this code is run a third workbook is open (not part of code). However when this is open AFTER the Pickorder one the code does not work and gives me errror code 9, "Subscript out of range." It works without that other workbook open or if the Pickorder is open after that other workbook. Also that one has a similar name to the Pickorder one but does not contain "Pickorder." Super weird issue and I am struggling to identify why. Any help would be appreciated. Thank you to anyone willing to help. Here is the line that is highlighted for the error and the rest of the code.

VBA Code:
Set PO = dwp.Sheets(Worksheets(i).Name]



Sub MatchCorrectTimes()
    'CLICK HERE AND PRESS F5 TO START SCRIPT
  
  
    For Each w In Workbooks
    If UCase(w.Name) Like UCase("*Pick*order*") Then
    Windows(w.Name).Activate
    Exit For
    End If
    Next w

    Dim dwp As Workbook         'DISPATCH
    Dim crtx As Workbook        'CORTEX
    Dim rngCTX As Range         'CORTEX RANGE
    Dim sq As Range             'SPARE RANGE
    Dim PO As Worksheet         'PICKORDER SHEET
    Dim i As Long               'ITERATION

    'FIND PICKORDER WORKBOOK & SHEET
    For i = 1 To Workbooks.Count
        If InStr(1, "PickOrder abc", "Pickorder", vbTextCompare) Then
            Set dwp = Workbooks(i)
        End If
    Next i

    For i = 1 To dwp.Worksheets.Count
        If InStr(1, "PickOrder abc", "Pickorder", vbTextCompare) Then
            Set PO = dwp.Sheets(Worksheets(i).Name)
        End If
    Next i
 
Last edited:

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
What is the name of the workbook that's causing the problem?
 
Upvote 0
What is the name of the workbook that's causing the problem?

Looks like 2020-09-12-DU2-CYCLE_2-DSP-DayOfOpsPlan.xlsx. The date is always the current date. Rest stays the same. The Pickorder one is the same but instead of “DayOfOpsPlan” it is “PickOrder.”
 
Last edited:
Upvote 0
Looks like 2020-09-12-DU2-CYCLE_2-DSP-DayOfOpsPlan.xlsx. The date is always the current date. Rest stays the same. The Pickorder one is the same but instead of “DayOfOpsPlan” it is “PickOrder.”
 
Upvote 0
What about the 3rd workbook?
The third one that is not part of the code is 2020-09-12-DU2-CYCLE_2-DSP-DayOfOpsPlan.xlsx . The two other open ones that are part of the code is Routing.xlsm and 2020-09-12-DU2-CYCLE_2-DSP-PickOrder.xlsx
 
Upvote 0

Forum statistics

Threads
1,223,227
Messages
6,170,849
Members
452,361
Latest member
d3ad3y3

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