Workbook.Open mystery

nigelandrewfoster

Well-known Member
Joined
May 27, 2009
Messages
747
Hello there - strange one this. If I run the code below using a button on a userform it executes perfectly. When I attempt to fire it using a keyboard shortcut it stops executing after the Workbook.Open line (highlighted). Any idea why? And how I can solve this?

Many thanks

VBA Code:
Sub Edit_Orders()
  
    Dim wbOrders As Workbook
    Dim rngLastRow As Range
    Dim avarOrders() As Variant
    Dim strNotes As String
    Dim lngGatherRow As Long
    Dim lngDataRows As Long
    Dim i As Long
    Dim j As Long
    
    [B]Set wbOrders = OpenWorkbook(MyDocumentsPath & "+MAIN WORKBOOKS+\", "+Orders+.xlsx")[/B]
    
    If Not wbOrders Is Nothing Then
        
        Application.ScreenUpdating = False
  
        With ThisWorkbook.Worksheets("Clipboard")
            avarOrders = .Range(.[b2], .[b1048576].End(xlUp)).Offset(, -1).Resize(, 25).Value
        End With
        
        i = 1
        lngDataRows = UBound(avarOrders)
        
        Do While i <= lngDataRows
            If IsEmpty(avarOrders(i, 13)) Then
                j = 1
                lngGatherRow = i - 1
                strNotes = avarOrders(lngGatherRow, 16)
                Do While IsEmpty(avarOrders(i, 13))
                    For j = 1 To 24
                        If Not IsEmpty(avarOrders(i, j)) Then
                            If Not IsError(avarOrders(i, j)) Then strNotes = strNotes & " " & avarOrders(i, j) ' Gather split notes together
                            avarOrders(i, j) = Empty
                        End If
                    Next
                    i = i + 1
                Loop
                avarOrders(lngGatherRow, 16) = strNotes
            End If
            If strNotes <> "" Then
                strNotes = ""
            Else
                i = i + 1
            End If
        Loop
        
        With wbOrders.Worksheets("Orders")
            .Cells.ClearContents
            .[a2].Resize((lngDataRows), 25).Value = avarOrders
            ThisWorkbook.Worksheets("Clipboard").[a1:y1].Copy .[a1]
            .[a1].EntireColumn.Delete Shift:=xlLeft
            .[a1:x1].Resize(lngDataRows + 1).Sort Key1:=.[b1], Order1:=xlAscending, Header:=xlYes
            Set rngLastRow = .[a1048576].End(xlUp).EntireRow
            rngLastRow.Resize(1048576 - rngLastRow.Row).Offset(1).Delete
        End With
        
        Application.ScreenUpdating = True

    End If
 
End Sub
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
Well, there is in fact no workbooks.open line in your code. Presuming your file is named "+MAIN WORKBOOKS+\+Orders+.xlsx":

Replace
VBA Code:
Set wbOrders = OpenWorkbook(MyDocumentsPath & "+MAIN WORKBOOKS+\", "+Orders+.xlsx")
with
VBA Code:
set wbOrders = workbooks.open(MyDocumentsPath & "+MAIN WORKBOOKS+\+Orders+.xlsx")
 
Upvote 0
I wonder how that code could have run from a userform since the used "openworkbook" is no legit vba command and should have thrown an error.

Wait... is "OpenWorkbook" another sub? If so, what is the code and where is it stored?
 
Upvote 0
Another question, what is MyDocumentsPath, there is nothing in the code that assigns a value to it.
 
Upvote 0
Well spotted. Sorry - yes I replaced Workbooks.Open with a function to check if already open and if not to open the workbook using Workbooks.Open
 
Upvote 0
Question remains: where is the code stored? A module? Please show the code of the OpenWorkbook function.

And, of course, answer Fluffs question. Where is MyDocumentsPath assigned its value(string)?
 
Upvote 0
I'm very sorry for my awful post - they're usually of a higher quality than this - and thank you for bearing with me and your interest in my post.

MyDocuments:

VBA Code:
Public Function MyDocumentsPath() As String

    MyDocumentsPath = "H:\Documents\My Documents\"
   
End Function

OpenWorkbook:

VBA Code:
Public Function OpenWorkbook(strWbPath As String, strWbName As String) As Workbook

    If WorkbookIsOpen(strWbName) Then
        Set OpenWorkbook = Workbooks(strWbName)
    Else
        On Error Resume Next
        Workbooks.Open (strWbPath & strWbName)
        If Err.Number <> 0 Then
            MsgBox strWbName & " not found"
        Else
            Set OpenWorkbook = Workbooks(strWbName)
        End If
    End If
       
End Function

The path/filename must be fine for, as I said, it runs perfectly well when I run it from a userform or VBA editor, just not when I run it using a keyboard shortcut for it stops executing after the line

VBA Code:
Set wbOrders = OpenWorkbook(MyDocumentsPath & "+MAIN WORKBOOKS+\", "+Orders+.xlsx")

Thanks again
 
Upvote 0
Can you also post the code for the WorkbookIsOpenfunction
 
Upvote 0
Can you also post the code for the WorkbookIsOpenfunction
Sure, here you go...

VBA Code:
Public Function WorkbookIsOpen(strWbName As String) As Boolean
    
    Dim wb As Workbook
    
    For Each wb In Workbooks
        If wb.Name = strWbName Then
            WorkbookIsOpen = True
            Exit For
        End If
    Next
    
End Function
 
Upvote 0
Thanks for that, it all works for me. Do you get any error messages?
 
Upvote 0

Forum statistics

Threads
1,215,098
Messages
6,123,082
Members
449,094
Latest member
mystic19

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