Input value for specific workbook variable

Eric Penfold

Active Member
Joined
Nov 19, 2021
Messages
424
Office Version
  1. 365
Platform
  1. Windows
  2. Mobile
Need help to use Input value to reference open workbook? When i try it shows nothing in the Case command tried If Statement but it`s the same result??

VBA Code:
Public Sub Depot_Name()

    Dim wb     As Workbook
    Dim ws     As Worksheet
    Dim Result As Variant

    With Application
        .ScreenUpdating = False
        .EnableEvents = False
        .DisplayAlerts = False
        .Calculation = xlCalculationManual
    End With
  
    Result = Application.InputBox("Type Depot Name Alton\Cov\Basildon", "What is your Current Open Workbook", "Type Depot Name Here")
  
    Select Case Result

        Case Is = "Alton"
        Set wb = Workbooks("S:\PURCHASING\Stock Control\Alton\2023\2023 Alton Back OrderT.xlsm")
          
        Case Is = "Cov"
            Set wb = Workbooks("S:\PURCHASING\Stock Control\Coventry\2023\2023 Coventry BackOrder.xlsm")
          
        Case Is = "Basildon"
            Set wb = Workbooks("S:\PURCHASING\Stock Control\Basildon\2023\2023 Basildon BackOrder.xlsm")
          
    End Select
  
    With Application
        .ScreenUpdating = True
        .EnableEvents = True
        .DisplayAlerts = True
        .Calculation = xlCalculationAutomatic
    End With

End Sub
 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
Remove or comment your
VBA Code:
    With Application
        .ScreenUpdating = False
        .EnableEvents = False
        .DisplayAlerts = False
        .Calculation = xlCalculationManual
    End With
block (In this code specifically, I do not see the reasonability of using it) or move it after the line
VBA Code:
End Select
plus amend your data type of Result to string:
VBA Code:
Result = Application.InputBox("Type Depot Name Alton\Cov\Basildon", "What is your Current Open Workbook", "Type Depot Name Here",,,,,2)
 
Upvote 0
I`ve taken the application blocks away but the wb still say`s out of context?
 
Upvote 0
Hi,
see if this update to your code does what you want

VBA Code:
Public Sub Depot_Name()
    
    Dim wb           As Workbook
    Dim Result      As Variant
    Dim Depot()     As String, strFileName As String
    
    Const strFolderName As String = "S:\PURCHASING\Stock Control\"
    
    Depot = Split("Alton,Coventry,Basildon", ",")
    
    Do
        Result = InputBox("Enter Depot Number" & Chr(10) & Chr(10) & _
                 "1 - " & Depot(0) & Chr(10) & _
                 "2 - " & Depot(1) & Chr(10) & _
                 "3 - " & Depot(2) & Chr(10) & Chr(10) & _
                 "To Select your Current Open Workbook", "Select Depot")
        'cancel pressed
        If StrPtr(Result) = 0 Then Exit Sub
    Loop Until Val(Result) > 0 And Val(Result) < 4
    
    strFileName = "2023 " & Depot(Result - 1) & " BackOrder.xlsm"
    
    For Each wb In Workbooks
        If wb.Name = strFileName Then wb.Activate: Exit Sub
    Next
    
    
    If MsgBox(strFileName & Chr(10) & "Workbook Not Found Do You Want To Open It?", 36, "Open Workbook") = vbNo Then Exit Sub
    Set wb = Workbooks.Open(strFolderName & Depot(Result - 1) & "\2023\" & strFileName, 0, False)
    
End Sub

Dave
 
Upvote 0
Remove or comment your
VBA Code:
    With Application
        .ScreenUpdating = False
        .EnableEvents = False
        .DisplayAlerts = False
        .Calculation = xlCalculationManual
    End With
block (In this code specifically, I do not see the reasonability of using it) or move it after the line
VBA Code:
End Select
plus amend your data type of Result to string:
VBA Code:
Result = Application.InputBox("Type Depot Name Alton\Cov\Basildon", "What is your Current Open Workbook", "Type Depot Name Here",,,,,2)
I`ve done the above but it still say`s wb as Out of context
 
Upvote 0
Hi,
see if this update to your code does what you want

VBA Code:
Public Sub Depot_Name()
   
    Dim wb           As Workbook
    Dim Result      As Variant
    Dim Depot()     As String, strFileName As String
   
    Const strFolderName As String = "S:\PURCHASING\Stock Control\"
   
    Depot = Split("Alton,Coventry,Basildon", ",")
   
    Do
        Result = InputBox("Enter Depot Number" & Chr(10) & Chr(10) & _
                 "1 - " & Depot(0) & Chr(10) & _
                 "2 - " & Depot(1) & Chr(10) & _
                 "3 - " & Depot(2) & Chr(10) & Chr(10) & _
                 "To Select your Current Open Workbook", "Select Depot")
        'cancel pressed
        If StrPtr(Result) = 0 Then Exit Sub
    Loop Until Val(Result) > 0 And Val(Result) < 4
   
    strFileName = "2023 " & Depot(Result - 1) & " BackOrder.xlsm"
   
    For Each wb In Workbooks
        If wb.Name = strFileName Then wb.Activate: Exit Sub
    Next
   
   
    If MsgBox(strFileName & Chr(10) & "Workbook Not Found Do You Want To Open It?", 36, "Open Workbook") = vbNo Then Exit Sub
    Set wb = Workbooks.Open(strFolderName & Depot(Result - 1) & "\2023\" & strFileName, 0, False)
   
End Sub

Dave
Thanks this is great but I need to capture the file that is already open name? Moment it says wb out of context?
 
Upvote 0
This part of the code

VBA Code:
For Each wb In Workbooks
        If wb.Name = strFileName Then wb.Activate: Exit Sub
    Next

checks eack open workbook & if name matches the strFileName variable activates it otherwise you see msgbox asking if want to open then file

did you see the Msgbox prompt?

Dave
 
Upvote 0
If the workbook is open then you need to drop the file path:
so instead of:
VBA Code:
Set wb = Workbooks("S:\PURCHASING\Stock Control\Coventry\2023\2023 Coventry BackOrder.xlsm")
You need:
VBA Code:
Set wb = Workbooks("2023 Coventry BackOrder.xlsm")
 
Upvote 0
I`ve used your code.
But i can`t seem to get the wb to return the already open workbook name?
Yes i used your prompt but it didn`t return the wb name?
This part of the code

VBA Code:
For Each wb In Workbooks
        If wb.Name = strFileName Then wb.Activate: Exit Sub
    Next

checks eack open workbook & if name matches the strFileName variable activates it otherwise you see msgbox asking if want to open then file

did you see the Msgbox prompt?

Dave
I
 
Upvote 0
sorry not been fully following what you are trying to do but try this modification to part of of the code

Rich (BB code):
For Each wb In Workbooks
        If wb.Name = strFileName Then Set wb = Workbooks(strFileName): Exit Sub
    Next

and see if this helps

Dave
 
Upvote 0

Forum statistics

Threads
1,215,086
Messages
6,123,043
Members
449,092
Latest member
ikke

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