0

我有一个自动打开的excel文件,然后它运行一个宏。此宏从 txt 文件导入数据。每 1 小时,数据进入 txt 文件的新行(时间戳和值)。我的宏在下午 5 点打开,导入数据,绘制图表,将其保存到动态日期,然后关闭。

这个宏每天下午 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
4

2 回答 2

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
于 2013-10-28T23:10:23.563 回答
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
于 2013-10-28T23:38:00.790 回答