Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2
Default Open and Append based on input string

I am just starting to get into VBA (a couple days and lots of
searching the net). I have an application that each day has report
saved based on the date.
ex. 01310701-1.dat, 01310702-1.dat

I would like to be able to have a input box asking for the date, open
any file containing that date and append them into 1 worksheet. I
have come up with the following code, from cutting and
pasting....etc......Now I have an input box, show a msg box (just so I
know it was the proper date) and its supposed to open each file in its
own worksheet (which isn't really what I want, but its a start).
However it always says no files in that directory. I believe the line
in question is FNames = Dir("varInput*.dat") where varInput is a
String that is entered as
ex. 013107

I would like to open any dat files that begin with 013107 and append
them into 1 worksheet.

Sub TestFile3()
Dim varInput As String
Dim basebook As Workbook
Dim mybook As Workbook
Dim sourceRange As Range
Dim destrange As Range
Dim FNames As String
Dim MyPath As String
Dim SaveDriveDir As String
varInput = InputBox("Please Enter the Day (mmddyy)?")
SaveDriveDir = CurDir
MyPath = "c:\test"
ChDrive MyPath
ChDir MyPath
MsgBox varInput
FNames = Dir("varInput*.dat")
If Len(FNames) = 0 Then
MsgBox "No files in the Directory"
ChDrive SaveDriveDir
ChDir SaveDriveDir
Exit Sub
End If
Application.ScreenUpdating = False
Set basebook = ThisWorkbook
Do While FNames < ""
Set mybook = Workbooks.Open(FNames)
mybook.Worksheets(1).Copy after:= _
basebook.Sheets(basebook.Sheets.Count)
On Error Resume Next
ActiveSheet.Name = mybook.Name
On Error GoTo 0
' You can use this if you want to copy only the values
' With ActiveSheet.UsedRange
' .Value = .Value
' End With
mybook.Close False
FNames = Dir()
Loop
ChDrive SaveDriveDir
ChDir SaveDriveDir
Application.ScreenUpdating = True
End Sub

Please bear with me as I am just learning this, and any help would be
greatly appreciated. Also any info on some websites that would help
in learning would be great as well.

Thanks
Rob

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 694
Default Open and Append based on input string

Hi:
You were nearly there, you have the variable as a string.

Change:

FNames = Dir("varInput*.dat")
to
FNames = Dir(varInput & "*.dat")

--
Hope this helps
Martin Fishlock, Bangkok, Thailand
Please do not forget to rate this reply.


"wooljr" wrote:

I am just starting to get into VBA (a couple days and lots of
searching the net). I have an application that each day has report
saved based on the date.
ex. 01310701-1.dat, 01310702-1.dat

I would like to be able to have a input box asking for the date, open
any file containing that date and append them into 1 worksheet. I
have come up with the following code, from cutting and
pasting....etc......Now I have an input box, show a msg box (just so I
know it was the proper date) and its supposed to open each file in its
own worksheet (which isn't really what I want, but its a start).
However it always says no files in that directory. I believe the line
in question is FNames = Dir("varInput*.dat") where varInput is a
String that is entered as
ex. 013107

I would like to open any dat files that begin with 013107 and append
them into 1 worksheet.

Sub TestFile3()
Dim varInput As String
Dim basebook As Workbook
Dim mybook As Workbook
Dim sourceRange As Range
Dim destrange As Range
Dim FNames As String
Dim MyPath As String
Dim SaveDriveDir As String
varInput = InputBox("Please Enter the Day (mmddyy)?")
SaveDriveDir = CurDir
MyPath = "c:\test"
ChDrive MyPath
ChDir MyPath
MsgBox varInput
FNames = Dir("varInput*.dat")
If Len(FNames) = 0 Then
MsgBox "No files in the Directory"
ChDrive SaveDriveDir
ChDir SaveDriveDir
Exit Sub
End If
Application.ScreenUpdating = False
Set basebook = ThisWorkbook
Do While FNames < ""
Set mybook = Workbooks.Open(FNames)
mybook.Worksheets(1).Copy after:= _
basebook.Sheets(basebook.Sheets.Count)
On Error Resume Next
ActiveSheet.Name = mybook.Name
On Error GoTo 0
' You can use this if you want to copy only the values
' With ActiveSheet.UsedRange
' .Value = .Value
' End With
mybook.Close False
FNames = Dir()
Loop
ChDrive SaveDriveDir
ChDir SaveDriveDir
Application.ScreenUpdating = True
End Sub

Please bear with me as I am just learning this, and any help would be
greatly appreciated. Also any info on some websites that would help
in learning would be great as well.

Thanks
Rob


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
Open Books with VBA based on certain string [email protected] Excel Discussion (Misc queries) 3 September 6th 06 11:35 PM
How to use Open as Append for log file mike Excel Programming 2 September 4th 06 05:02 AM
open for append Mark[_17_] Excel Programming 2 May 18th 04 07:46 AM
Append to open CSV CY459 Excel Programming 1 October 17th 03 10:45 PM
open for append Marcel Jahn Excel Programming 1 July 22nd 03 10:15 PM


All times are GMT +1. The time now is 06:54 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"