Using macro to open file in different path Problem


New Member
Oct 28, 2008
I've looked through several posts and tried this several different ways, but I still can't get it to work. It keeps telling me that the file (Y5533.PRN) could not be found. Any help would be greatly appreciated. This is the situation:

I have a "Main" tab and a "File Locations" tab. The user types in a file number and a code on the "Main" tab. Then the code and the file number are put together in the "File Locations" tab to create the file name. So if on the "Main" tab, the user enters file number 5533 in cell N4 and code Y in cell N5, then on the "File Locations" tab in cell I1, they are concatenated along with the file extension to create file name Y5533.PRN. I have the path typed in cell E1 on the "File Locations" tab, the user does not change this. I want to open the text file Y5533.PRN or whatever the filename in I1 happens to be. My workbook is on my desktop, but the files that I am opening are in C:\tire\workfil\dat (which is stored in cell E1). This is the first part of my code:

Sub Open_Files()
' Open_Files Macro
' Macro recorded 10/24/2008 by Terri Markle
Sheets("File Locations").Select
FilePath = Range("E1")
PRNfile = Range("I1")
Workbooks.OpenText Filename:=FilePath & "\" & PRNfile _
, Origin:=437, StartRow:=1, DataType:=xlDelimited, TextQualifier:= _
xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=False, Semicolon:=False _
, Comma:=True, Space:=False, Other:=False, FieldInfo:=Array(Array(1, 1), _
Array(2, 1), Array(3, 1), Array(4, 1), Array(5, 1), Array(6, 1), Array(7, 1), Array(8, 1), _
Array(9, 1), Array(10, 1), Array(11, 1), Array(12, 1), Array(13, 1), Array(14, 1), Array(15 _
, 1), Array(16, 1), Array(17, 1), Array(18, 1), Array(19, 1), Array(20, 1), Array(21, 1), _
Array(22, 1), Array(23, 1), Array(24, 1), Array(25, 1), Array(26, 1), Array(27, 1), Array( _
28, 1), Array(29, 1), Array(30, 1), Array(31, 1), Array(32, 1), Array(33, 1), Array(34, 1), _
Array(35, 1), Array(36, 1), Array(37, 1), Array(38, 1), Array(39, 1), Array(40, 1), Array( _
41, 1), Array(42, 1), Array(43, 1), Array(44, 1), Array(45, 1), Array(46, 1), Array(47, 1), _
Array(48, 1), Array(49, 1), Array(50, 1), Array(51, 1), Array(52, 1), Array(53, 1), Array( _
54, 1), Array(55, 1), Array(56, 1), Array(57, 1), Array(58, 1), Array(59, 1)), _


MrExcel MVP, Moderator
Mar 2, 2007
Office Version
Are you sure you have remembered the "." between the file name and the file extension?

I believe all the Array() arguments are just telling Excel that all the fields should be "General" formatting so you can probably safely ignore them. I've thrown in a file exists check on your file name:

Sub Open_Files()
Dim FS As Object
Dim strConcatenatedFileName As String
Dim FilePath As String
Dim PRNfile As String

Sheets("File Locations").Select
FilePath = Range("E1").Value
PRNfile = Range("I1").Value
strConcatenatedFileName = FilePath & "\" & PRNfile
Set FS = CreateObject("Scripting.FileSystemObject")

If Not FS.FileExists(strConcatenatedFileName) Then
    MsgBox "File " & """" & strConcatenatedFileName & """" & " does not exist!"
    Workbooks.OpenText Filename:=strConcatenatedFileName _
    , Origin:=437, StartRow:=1, DataType:=xlDelimited, TextQualifier:= _
    xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=False, Semicolon:=False _
    , Comma:=True, Space:=False, Other:=False, TrailingMinusNumbers:=True
End If

End Sub

Forum statistics

Latest member

Some videos you may like

This Week's Hot Topics

  • VBA (Userform)
    Hi All, I just would like to know why my code isn't working. Here is my VBA code: [CODE=vba]Private Sub OKButton_Click() Dim i As Integer...
  • List box that changes fill color
    Hello, I have gone through so many pages trying to figure this out. I have a 2020 calendar that depending on the day needs to have a certain...
  • Remove duplicates and retain one. Cross-linked cases
    Hi all I ran out of google keywords to use and still couldn't find a reference how to achieve the results of a single count. It would be great if...
  • VBA Copy and Paste With Duplicates
    Hello All, I'm in need of some input. My VBA skills are sub-par at best. I've assembled this code from basic research and it works but is...
  • Macro
    is it possible for a macro to run if the active cell value is different to the value above it
  • IF DATE and TIME
    I currently use this to check if date has passed but i also need to set a time on it too. Is it possible? [CODE=vba]=IF(B:B>TODAY(),"Not...