View Single Post
  #8   Report Post  
Posted to microsoft.public.excel.misc
Tom Tom is offline
external usenet poster
 
Posts: 19
Default How to increment the date

Thanks again, Dave. It is testing ok with regard to the last two problems.
The new problem it throws up is when the file of a particular day is missing
e.g. when it is a public holiday, the debug screen pops up. How can I ask it
to skip and keep skipping (like when it is the Christmas break) until the
next available file is found?

"Dave Peterson" wrote in message
...
First, this is untested--but it did compile.

Second, when you do a find in excel, you can set an option to match entire
cell
contents. In code, you'd use xlWhole instead of xlPart.

And as soon as you open a text file, it becomes the activeworkbook. So
you can
use that to assign a variable that represents that newly opened text file.

If you open a .xls (or .csv), you can set a variable when you open that
workbook.

Option Explicit
Sub Build_Data()

Dim myStr As String
Dim dCtr As Long
Dim myDate As Date
Dim NewDataWkbk As Workbook
Dim TxtWkbk As Workbook
Dim FoundCell As Range
Dim DestCell As Range

Set NewDataWkbk = Workbooks.Open _
(Filename:="D:\Echart\Download\newdata.csv")

'Paste into the next available row in the .CSV file?
With NewDataWkbk.Worksheets(1)
Set DestCell = .Cells(.Rows.Count, "A").End(xlUp).Offset(1, 0)
End With

For dCtr = 1 To 10 'some big number
myDate = dCtr - 1 + DateSerial(2006, 12, 11)
If Weekday(myDate) = vbSaturday _
Or Weekday(myDate) = vbSunday Then
'do nothing
Else
myStr = "D:\Echart\Download\" & Format(myDate, "yyyymmdd") &
".txt"
Workbooks.OpenText Filename:=myStr, Origin:=437, _
StartRow:=1, DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, _
Tab:=True, Semicolon:=False, Comma:=True, _
FieldInfo:=Array(Array(1, 1), Array(2, 1), Array(3, 1), _
Array(4, 1), Array(5, 1), Array(6, 1), _
Array(7, 1)), _
TrailingMinusNumbers:=True
Set TxtWkbk = ActiveWorkbook 'the text file just opened

With TxtWkbk.Worksheets(1)
Set FoundCell = .Cells.Find(What:="APA", _
After:=.cells(.cells.count), LookIn:=xlFormulas, _
LookAt:=xlWhole, SearchOrder:=xlByColumns, _
SearchDirection:=xlNext, MatchCase:=False, _
SearchFormat:=False)
End With

If FoundCell Is Nothing Then
'not found, skip this file??
Else
FoundCell.EntireRow.Resize(1, 7).Copy _
Destination:=DestCell
'get ready for next file
Set DestCell = DestCell.Offset(1, 0)
End If
TxtWkbk.Close savechanges:=False
End If
Next dCtr

End Sub



Tom wrote:

"Dave Peterson" wrote in message
...
I'm not quite sure what you're doing, but if you're doing lots of dates
and want
to skip all the weekends, you could just check inside the loop and not
do
the
work if it's a weekend day.

<snip

What I'm trying to do is, extracting the day's trading stock prices for a
particular stock (say APA), and assembling the data in the file
newdata.csv,
over a number of years. I've included the codes below. With your new
codes
I'm now able to automate the process. It fetches and opens the files
correctly. However, with sight unseen, I am not able to close the file as
I
am not able to tell which was the last file it had open. Secondly, when
doing it manually, if it had found the wrong code, like "APAO" instead of
"APA", I would simply ask it to find it again until I could see that it
was
the right one before proceeding. Is there a way to test this? Thanks for
any
suggestions.

Sub Build_Data()
'
' Build_Data Macro
' Macro recorded 29/12/2006 by Tom
'

'
Dim myStr As String
Dim dCtr As Long
Dim myDate As Date

' Workbooks.Open Filename:="D:\Echart\Download\newdata.csv", Origin:=
_
xlWindows
Workbooks.Open Filename:="D:\Echart\Download\newdata.csv"
Range("A1").Select
For dCtr = 1 To 10 'some big number
myDate = dCtr - 1 + DateSerial(2006, 12, 11)
If Weekday(myDate) = vbSaturday _
Or Weekday(myDate) = vbSunday Then
'do nothing
Else
myStr = "D:\Echart\Download\" & Format(myDate, "yyyymmdd") &
".txt"
Workbooks.OpenText Filename:=myStr, Origin:=437 _
, StartRow:=1, DataType:=xlDelimited,
TextQualifier:=xlDoubleQuote,
_
Tab:=True, Semicolon:=False, Comma:=True,
FieldInfo:=Array(Array(1,
1), _
Array(2, 1), Array(3, 1), Array(4, 1), Array(5, 1), Array(6, 1),
_
Array(7, 1)), TrailingMinusNumbers:=True
Cells.Find(What:="APA", After:=ActiveCell, LookIn:=xlFormulas,
LookAt:=
_
xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlNext,
MatchCase:= _
False, SearchFormat:=False).Activate ' how to match?
ActiveCell.Range("A1:G1").Select
Selection.Copy
Windows("newdata.csv").Activate
ActiveSheet.Paste
ActiveCell.Offset(1, 0).Range("A1").Select
Windows("20061211.txt").Activate ' how to close after looping?
Application.CutCopyMode = False
ActiveWorkbook.Close
End If
Next dCtr

End Sub


--

Dave Peterson