1

I am almost done with this small part of my program. I am trying to assign a path file to a variable so I can use that variable in the second part, I have tried the following. I appreciate any help. Thank you very much in advanced

FIRST PART

 Public strPathAndFile As String  

 Sub CommandButton2_Click()

 Dim fd As FileDialog
 Dim strShortName As String
 Dim strInitialDir As String
 Dim wb As Workbook

 strInitialDir = CurDir   'Save current directory if required

 Set fd = Application.FileDialog(msoFileDialogFilePicker)

 With fd
     .InitialFileName = CurDir & "\"     'Startup folder (Optional line of code)
     .AllowMultiSelect = False
     .Filters.Clear
     .Filters.Add "All Excel Files", "*.xls;*.xlsm;*.xlsx"     'Display excel files only
     If .Show = False Then
         MsgBox "User cancelled without selecting. Processing terminated."
         ChDir (strInitialDir)  'Change back to Initial directory if user cancels
         Exit Sub
     End If
     'Next line strPathAndFile contains path and filename of selected file ' < ======= FROM HERE
     strPathAndFile = .SelectedItems(1)
 End With

 'The following populates a cell with just the filename (without the path)
 strShortName = Right(strPathAndFile, Len(strPathAndFile) - _
         InStrRev(strPathAndFile, "\"))

 ' Store the path file in this cell
 ThisWorkbook.Worksheets("Sheet1").Range("F12") = strShortName

End Sub

SECOND PART - Here I want to use the strPathAndFile variable.

 Sub CommandButton3_Click()
' Read a file and find the title name
' Variables
Dim WhatToFind As Variant
Dim wbFind As Workbook
Dim foundRange As Range

' Assign file path to a variable
Set wbFind = Workbooks.Open(strPathAndFile) ' <========  TO HERE

' Do not show actions on screen
Application.ScreenUpdating = False

' Array of valuse to be found
WhatToFind = Array("Dog", "House", "Table")

With wbFind.Sheets("general_report")
    Set foundRange = .Cells.Find(What:="status", After:=.Cells(1, 1), _
    LookIn:=xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, _
    SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False)
End With

If Not foundRange Is Nothing Then foundRange.Activate

' close the source workbook without saving any changes
wbFind.Close False

' free memory
Set wbFind = Nothing

' Show actions on screen
Application.ScreenUpdating = False
End Sub
2
  • msdn.microsoft.com/en-us/library/zb8fth98.aspx yields Public Const strPathAndFile As String = "C:\yourpath" or is the problem that you want to dynamically assign the path based on current workbook path? Commented Jun 16, 2014 at 17:29
  • Yes Sr. I am trying to select a file and then use that path file and save it into a variable to be used by another part of the program. But I do not want to hard code the path, I need to do it dynamically. Thank you very much, I appreciate your kind help. Commented Jun 16, 2014 at 17:30

2 Answers 2

2

You are already saving the path in the cell,Worksheets("Sheet1").Range("F12"), Cant you just do

 ' Store the path file in this cell
 ThisWorkbook.Worksheets("Sheet1").Range("F12") = strPathAndFile

then use

 Dim fpath As String
 fpath = Worksheets("Sheet1").Range("F12").Value
 Set oWB = Application.Workbooks.Open(fpath)
Sign up to request clarification or add additional context in comments.

Comments

1
Public strFileAndPath As String

Sub getpath()
    Dim retvalue As String
    retvalue = ThisWorkbook.FullName
    strFileAndPath = retvalue
End Sub

you just need to set the strfileandpath global variable which can be done via strFileAndPath = thisworkbook.fullname

The other stuff above was me testing it.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.