Using macro to open file in different path Problem

tmarkle

New Member
Joined
Oct 28, 2008
Messages
2
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)), _
TrailingMinusNumbers:=True
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
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:

Code:
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!"
Else
    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
 
Upvote 0

Forum statistics

Threads
1,214,653
Messages
6,120,755
Members
448,989
Latest member
mariah3

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