Opening workbook from user form

Darren Smith

Well-known Member
Joined
Nov 23, 2020
Messages
631
Office Version
  1. 2019
Platform
  1. Windows
I have previously done this code to open workbooks in a specified folder using the workbook cell value.
Now I want to use a value from the user form see my attempt below

VBA Code:
Private Sub Open_Old_Jobcard_Click()

Dim JobCardNo As New DataObject
JobCardNo.SetText Me.Open_Old_Jobcard.Text


End Sub

Private Sub Worksheet_SelectionChange(ByVal Target As Range)

Dim PID As Double
Dim strRootPath As String

Dim objFile As Scripting.File
Dim wb As Workbook
Dim myfilename As String
Dim Test As String

Dim objFSO As Scripting.FileSystemObject
Dim objFolder As Scripting.Folder

If Target.Cells.Row = 1 And Target.Cells.Column = 2 Then

Const strExpExe = "explorer.exe"
Const strArg = " " '" /e,/root, "

strRootPath = "\\TGS-SRV01\Share\ShopFloor\PRODUCTION\JOB CARDS\1 - ARCHIVED JOB CARDS\" & Int((ActiveSheet.Range("C1") - 1) / 50) * 50 + 1 & "-" & Int((ActiveSheet.Range("C1") - 1) / 50 + 1) * 50 & "\" & ActiveSheet.Range("C1") & "\P" & ActiveSheet.Range("C1")

PID = Shell(strExpExe & strArg & strRootPath, 3)

End If

If Target.Cells.Row = 1 And Target.Cells.Column = 1 Then

Set objFSO = CreateObject("Scripting.FileSystemObject")

Set objFolder = objFSO.GetFolder("\\TGS-SRV01\Share\ShopFloor\PRODUCTION\JOB CARDS\1 - ARCHIVED JOB CARDS\" & Int((ActiveSheet.Range("C1") - 1) / 50) * 50 + 1 & "-" & Int((ActiveSheet.Range("C1") - 1) / 50 + 1) * 50 & "\" & ActiveSheet.Range("C1"))

For Each objFile In objFolder.Files
If Left(objFile.Name, 5) = CStr(ActiveSheet.Range("C1")) And Right(objFile.Name, 4) = "xlsm" Then

myfilename = objFile.Path

End If

Next objFile

Set wb = Workbooks.Open(myfilename)

End If

End Sub


Private Sub JobCardOpen(ByVal Target As Range)

Dim objFile As Scripting.File
Dim wb As Workbook
Dim myfilename As String

Dim objFSO As Scripting.FileSystemObject
Dim objFolder As Scripting.Folder

If Target.Cells.Row = 1 And Target.Cells.Column = 1 Then

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder("\\TGS-SRV01\Share\ShopFloor\PRODUCTION\JOB CARDS\1 - ARCHIVED JOB CARDS\" & Int((ActiveSheet.Range("C1") - 1) / 50) * 50 + 1 & "-" & Int((ActiveSheet.Range("C1") - 1) / 50 + 1) * 50 & "\" & ActiveSheet.Range("C1"))
For Each objFile In objFolder.Files
If Left(objFile.Name, 5) = ActiveSheet.Range("C1") Then

myfilename = objFile.Path & objFile.Name

End If

Next objFile

Set wb = Workbooks.Open(myfilename)
End Sub
 

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
I`ve also, asked
 
Upvote 0

Forum statistics

Threads
1,214,641
Messages
6,120,693
Members
448,979
Latest member
DET4492

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