Я могу читать и записывать файл текущего дня в текстовый файл для удобства ввода на лист. Но отчеты прошедшего дня с тем же именем отличаются тем, что в них указана дата, т. Е. C:\Users\name\reports\report.html
против C:\Users\name\reports\archive\date\report.html
Я не могу понять, как сказать, чтобы она проходила через выбранные даты. Кроме того, дата отчета отображается как 06292019, а не 06/29/2019.
Я не очень старался, так как не знаю, с чего начать. Большинство вопросов, которые я просмотрел, не относятся достаточно конкретно.
Sub FindoldFile()
Dim fName As String
Dim fPath As String
Dim Rpt As String
Dim Source As String
Dim filePath As String
Dim Textfile As Integer
Dim Ifile As String
Dim CurRow As Long
fPath = "C:\Users\name\reports\"
Rpt = Dir(fPath & "reportname" & "date" & "_*")
'Would like to have "date" looped through date range input on sheet in cells
If Rpt = "" Then
MsgBox "No report found"
Exit Sub
End If
Source = fPath And Rpt
filePath = "C:\Data\report.txt"
Textfile = FreeFile
Open filePath For Output As Textfile 'First one as for output, second as for append?
Close Textfile
Kill "C:\Data\report.txt"
Ifile = "C:\Data\report.txt"
FileCopy Source, Ifile
Source = Ifile
'Here I would want to input multiple files from the date range selected
CurRow = 2
Open Source For Input As #1
Do While (Not EOF())
'do stuff
CurRow = CurRow + 1
Loop
Close #1
End Sub
Я хочу иметь возможность поместить значения начальной и конечной даты в две ячейки, а затем записать их все в .txt
. После этого введите их в лист Excel.
Правка, посмотрев на тему, предложенную Сиддхартом, я придумал эту адаптацию. Из которых я опробую, когда вернусь на работу завтра и обновлю после.
Sub FindOld()
Dim ws As Worksheet
Dim st As Range
Dim en As Range
Dim x As Integer
Dim stDate As Date
Dim enDate As Date
Dim d As Date
Dim LR As Long
Dim CurRow As Long
Dim wb As Workbook
Dim fPath As String
Dim fName As String
Set ws = ThisWorkbook.Sheets("Sheet1")
ws.Activate
fPath = "C:\Users\"
Dim LastRow As Long
With ActiveSheet
If Application.WorksheetFunction.CountA(.Cells) <> 0 Then
LastRow = .Cells.Find(What:="*", _
After:=.Range("A1"), _
Lookat:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious, _
MatchCase:=False).Row
Else
LastRow = 1
End If
LR = LastRow
End With
For x = 0 To LR - 2
Set st = Range("B2").Offset(x, 0)
Set en = Range("C2").Offset(x, 0)
stDate = DateSerial(Year(st), Month(st), Day(st))
enDate = DateSerial(Year(en), Month(en), Day(en))
Dim subPath As String
Dim Report As String
LR = LR + 1
For d = stDate To enDate
Debug.Print d
fName = Format(d, "mmddyyyy")
subPath = fPath & fName
Report = Dir(subPath & fName & "_*" & ".html")
Source = subPath And Rpt
filePath = "C:\Data\report.txt"
Textfile = FreeFile
If d = stDate Then
Open filePath For Output As Textfile
Close Textfile
Kill "C:\Data\report.txt"
Ifile = "C:\Data\report.txt"
FileCopy Source, Ifile
Source = Ifile
Else
Open filePath For Append As Textfile
Close Textfile
Ifile = "C:\Data\report.txt"
FileCopy Source, Ifile
Source = Ifile
End If
Next d
Next
CurRow = 2
Open Source For Input As #1
Do While (Not EOF())
'do stuff
CurRow = CurRow + 1
Loop
Close #1
Exit Sub
End Sub