Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 6
Default Import Text File w/ VBA

I am trying to set up a macro to import a specific text file based on the
information the user puts into specific cells. They imput the company,
division and date and this generates a path & filename in a cell in the
spreadsheet. How can I get that path & filename into the macro? I've tried
quite a few options but with no success.

Thanks,
--
Suz
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 180
Default Import Text File w/ VBA

I'd do this in two steps. First, record the opening of a specific file.
Then edit the resulting VBA code, replacing the Filename:= parameter to
incorporate the variables provided by the user. You'll probably use the &
operator to combine some fixed components with some variables along with the
format operator to turn the input date into the appropriate file name,
something like
Filename := "S:\" & CompanyName & "\" & DivisionName & "\" &
text(Date,"mmddyy") & ".txt"
Along the way you can also use the immediate window in the VB Editor; type
the work print followed by the right-hand side of the equation above to see
that you get the expected path and filename.
--Bruce

"Suzseb" wrote:

I am trying to set up a macro to import a specific text file based on the
information the user puts into specific cells. They imput the company,
division and date and this generates a path & filename in a cell in the
spreadsheet. How can I get that path & filename into the macro? I've tried
quite a few options but with no success.

Thanks,
--
Suz

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2
Default Import Text File w/ VBA

If you just want to open the text file you should use the following command
in your VBA:

Dim sCompany As String
Dim sDivision As String
Dim sDate As String
Dim sFilename As String

sCompany = Range("A1")
sDivision = Range("A2")
sDate = Replace(Range("A3"), "/", "")
sFilename = "C:\" & sCompany & "\" & sDivision & "\" & sDate & ".txt"


Workbooks.OpenText Filename:=sFilename

In the above example you should replace C:\ with the starting point of your
folder structure. You'll notice Replace(Range("A3"), "/", "") this ensures
that if the date is entered dd/mm/yyyy or mm/dd/yyyy ie. with slashes, which
are invalid for filenames, then they are removed.

I hope this helps.

--
Kurt Farrar
..NET Developer & Computer Enthusiast



"Suzseb" wrote:

I am trying to set up a macro to import a specific text file based on the
information the user puts into specific cells. They imput the company,
division and date and this generates a path & filename in a cell in the
spreadsheet. How can I get that path & filename into the macro? I've tried
quite a few options but with no success.

Thanks,
--
Suz

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 6
Default Import Text File w/ VBA

Thank you. I'll try that.
--
Suz


"Kurt Farrar" wrote:

If you just want to open the text file you should use the following command
in your VBA:

Dim sCompany As String
Dim sDivision As String
Dim sDate As String
Dim sFilename As String

sCompany = Range("A1")
sDivision = Range("A2")
sDate = Replace(Range("A3"), "/", "")
sFilename = "C:\" & sCompany & "\" & sDivision & "\" & sDate & ".txt"


Workbooks.OpenText Filename:=sFilename

In the above example you should replace C:\ with the starting point of your
folder structure. You'll notice Replace(Range("A3"), "/", "") this ensures
that if the date is entered dd/mm/yyyy or mm/dd/yyyy ie. with slashes, which
are invalid for filenames, then they are removed.

I hope this helps.

--
Kurt Farrar
.NET Developer & Computer Enthusiast



"Suzseb" wrote:

I am trying to set up a macro to import a specific text file based on the
information the user puts into specific cells. They imput the company,
division and date and this generates a path & filename in a cell in the
spreadsheet. How can I get that path & filename into the macro? I've tried
quite a few options but with no success.

Thanks,
--
Suz

  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 6
Default Import Text File w/ VBA

Thank you. I'll let you know how it goes.
--
Suz


"bpeltzer" wrote:

I'd do this in two steps. First, record the opening of a specific file.
Then edit the resulting VBA code, replacing the Filename:= parameter to
incorporate the variables provided by the user. You'll probably use the &
operator to combine some fixed components with some variables along with the
format operator to turn the input date into the appropriate file name,
something like
Filename := "S:\" & CompanyName & "\" & DivisionName & "\" &
text(Date,"mmddyy") & ".txt"
Along the way you can also use the immediate window in the VB Editor; type
the work print followed by the right-hand side of the equation above to see
that you get the expected path and filename.
--Bruce

"Suzseb" wrote:

I am trying to set up a macro to import a specific text file based on the
information the user puts into specific cells. They imput the company,
division and date and this generates a path & filename in a cell in the
spreadsheet. How can I get that path & filename into the macro? I've tried
quite a few options but with no success.

Thanks,
--
Suz

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Can I import text file of cash flow to excel file then use formula Bumpa Excel Discussion (Misc queries) 2 May 28th 10 04:22 PM
Would Like to Automate Batch File Creation and Text FIle Import socrtwo Excel Discussion (Misc queries) 2 August 18th 06 03:54 PM
How do I import text file, analyze data, export results, open next file Geoffro Excel Programming 2 March 6th 05 08:02 PM
Import text file into excel with preset file layout, delimeters VBA meldrape Excel Programming 7 June 15th 04 08:31 PM
Get External Data, Import Text File, File name problem Scott Riddle Excel Programming 1 July 11th 03 05:40 PM


All times are GMT +1. The time now is 01:18 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"