2013-10-28 162 views
0

我有一個自動打開的excel文件,然後運行一個宏。該宏從txt文件導入數據。每隔1小時,數據就會進入txt文件到一個新行(時間戳和一個值)。我的宏在下午5點打開,導入數據,繪製數據,將其保存爲動態日期,然後關閉。Excel VBA宏動態繪圖

這個宏每天在下午5點打開一次。但是,我不希望宏在第一天繼續選擇前24行,第二次24行。當它在第二天運行時。第二天,我想只選擇行(25-49)。第三天,我想選擇行(50-74)。

有沒有人有使用相同的宏動態選擇行的方法?

Sub Auto_Open() 
' 
' Auto_Open Macro 
' 

' 
    Workbooks.OpenText Filename:="C:\Users\Pablo\Desktop\LOGTEST.txt", Origin _ 
     :=437, StartRow:=1, DataType:=xlDelimited, TextQualifier:=xlDoubleQuote _ 
     , ConsecutiveDelimiter:=False, Tab:=False, Semicolon:=True, Comma:= _ 
     False, Space:=False, Other:=False, FieldInfo:=Array(Array(1, 4), Array(2, 1) _ 
     ), TrailingMinusNumbers:=True 
    Columns("A:A").EntireColumn.AutoFit 
    Columns("A:B").Select 
    ActiveSheet.Shapes.AddChart.Select 
    ActiveChart.SetSourceData Source:=Range("'LOGTEST'!$A:$B") 
    ActiveChart.ChartType = xlXYScatterSmoothNoMarkers 
    myFileName = "myFile_" & Month(Now) & "_" & Day(Now) & "_" & Hour(Now) & "_" & Minute(Now) 
    ActiveWorkbook.SaveAs Filename:="C:\Users\Pablo\Desktop\" & myFileName, _ 
     FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False 

    ActiveWorkbook.Close savechanges = True 
    Application.Quit 
End Sub 
+0

在文本文件中的時間標記是否包括日期?如果有的話,我可能會有答案。 –

+0

它包含日期和時間。感謝您的努力 – ENGR024

回答

0
Sub Auto_Open() 
Const F_PATH As String = "C:\_stuff\" 
Dim wb As Workbook, sht As Worksheet, rng As Range 
Dim cht As Chart, myFileName As String 

    Workbooks.OpenText Filename:=F_PATH & "test.txt", Origin:=437, _ 
     StartRow:=1, DataType:=xlDelimited, TextQualifier:=xlDoubleQuote, _ 
     ConsecutiveDelimiter:=False, Tab:=True, Semicolon:=False, _ 
     Comma:= False, Space:=False, Other:=False, _ 
     FieldInfo:=Array(Array(1, 4), Array(2, 1)), TrailingMinusNumbers:=True 

    Set wb = ActiveWorkbook 
    Set sht = wb.Sheets(1) 
    Set rng = sht.Cells(Rows.Count, 1).End(xlUp) 

    'just use the last 24 rows... 
    If rng.Row >= 24 Then Set rng = rng.Offset(-23, 0) 
    Set rng = rng.Resize(24, 2) 

    sht.Columns(1).EntireColumn.AutoFit 

    Set cht = ActiveSheet.Shapes.AddChart(Top:=100, Left:=100).Chart 
    cht.SetSourceData Source:=rng 
    cht.ChartType = xlXYScatterSmoothNoMarkers 

    myFileName = "myFile_" & Month(Now) & "_" & Day(Now) & _ 
        "_" & Hour(Now) & "_" & Minute(Now) 
    wb.SaveAs Filename:=F_PATH & myFileName, _ 
     FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False 

    wb.Close savechanges:=True 
    Application.Quit 
End Sub 
+0

這工作非常好。謝謝 – ENGR024

0

讀取內存中的文件,而不是把它變成一個電子表格:

dFirstDate = #10/1/2013# 
dDate = Now 
nDays = DateDiff("d", dDate, dFirstDate) 
nRowStart = nDays * 24 
sFile = "C:\Users\Pablo\Desktop\LOGTEST.txt" 
Open sFile For Input As iFileNum 
Line Input #iFileNum, sBuf 
While Not sBuf = "" And Len(sBuf) > 1 
    nFileRow = nFileRow + 1 
    If nFileRow >= nRowStart And nFileRow <= nRowStart + 24 Then 
     ActiveSheet.Range("A1").Offset(nSheetRow, 0).Value = sBuf 
     nSheetRow = sSheetRow + 1 
    End If 
    Line Input #iFileNum, sBuf 
Wend 
Close #iFileNum