Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
d chaps
 
Posts: n/a
Default open file (as variable) from macro

User inputs a date etc., I add path in front of date and some other
manipulation, and come up w/ the filename that I'm trying to open with a
macro. Then I work on that file, print it, etc. and want to close it. Problem
is, when I record the "open filename", it puts that one filename in my macro
as literal, rather than the cell address. I need that to be variable. As is,
even when a different date is entered by user, it still keeps opening the
same file cuz it's recorded that way in the macro.
  #2   Report Post  
Dave Peterson
 
Posts: n/a
Default

Maybe something like:
Option Explicit
Sub testme()

Dim wkbk As Workbook
Dim mystr As String
Dim userDate As Date
Dim teststr As String

userDate = DateSerial(2005, 12, 31)

mystr = "C:\my documents\excel\test\filePrefix" _
& Format(userDate, "yyyy_mm_dd") & ".xls"

teststr = ""
On Error Resume Next
teststr = Dir(mystr)
On Error GoTo 0

If teststr = "" Then
MsgBox "File not available"
Exit Sub
End If

Set wkbk = Workbooks.Open(Filename:=mystr)
wkbk.Worksheets(1).PrintOut

wkbk.Close savechanges:=False

End Sub







d chaps wrote:

User inputs a date etc., I add path in front of date and some other
manipulation, and come up w/ the filename that I'm trying to open with a
macro. Then I work on that file, print it, etc. and want to close it. Problem
is, when I record the "open filename", it puts that one filename in my macro
as literal, rather than the cell address. I need that to be variable. As is,
even when a different date is entered by user, it still keeps opening the
same file cuz it's recorded that way in the macro.


--

Dave Peterson
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
I want to make a Macro that would so I can open a CSV file and it. Bob L Excel Discussion (Misc queries) 2 March 8th 05 01:04 AM
Opening a file with a Macro Adam1 Chicago Excel Discussion (Misc queries) 2 February 28th 05 10:13 PM
Cannot open a file that Excel says is open Plodhia Excel Discussion (Misc queries) 2 December 7th 04 01:43 AM
format question when open csv file Jeff Excel Discussion (Misc queries) 1 December 1st 04 06:53 PM
Macro did not run after download file from net ariffin Excel Worksheet Functions 2 November 6th 04 01:20 AM


All times are GMT +1. The time now is 06:53 AM.

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"