This is my first time using VBA, so I am not familiar with how it really works. I need a code that continously imports the most recent measurement scan from a certain folder and updates the cells in a certain location with the contents of the txt-file. So far I am using the code below in order to automatically import the file "Scan00.txt" every 10 seconds. Unfortunately the software storing the scans uses padding digits and so the next scan is called "Scan01.txt". I want to modify my code to instead of importing the contents of Scan00, it imports the most recent txt-file in the folder instead.
In the Microsoft Excel Objects folder I have this code:
I then have this module:
I have found a lot of similar examples on these forums, but they don't use this convenient .Add(Connection:= part, and I don't understand how I can incorporate this in the various codes used to determine the most recent file.
In the Microsoft Excel Objects folder I have this code:
Code:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Application.OnTime dTime, "Scanimport", , False
End Sub
Private Sub Workbook_Open()
Application.OnTime Now + TimeValue("00:00:10"), "Scanimport"
End Sub
I then have this module:
Code:
Sub Scanimport()
Set shFirstQtr = Workbooks(1).Worksheets(1)
Set qtQtrResults = shFirstQtr.QueryTables _
.Add(Connection:="TEXT;M:\pc\Desktop\Innovasjonsprosjekt\Test\Scan00", _
Destination:=Range( _
"K2"))
With qtQtrResults
Columns("K:L").Select
Selection.ClearContents
.Name = "test"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.TextFilePromptOnRefresh = False
.TextFilePlatform = 737
.TextFileStartRow = 18
.TextFileParseType = xlDelimited
.TextFileTextQualifier = xlTextQualifierDoubleQuote
.TextFileConsecutiveDelimiter = False
.TextFileTabDelimiter = True
.TextFileSemicolonDelimiter = False
.TextFileCommaDelimiter = False
.TextFileSpaceDelimiter = False
.TextFileColumnDataTypes = Array(1)
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery = False
Columns("K:L").Select
Selection.Replace What:=",", Replacement:=".", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
End With
dTime = Now + TimeValue("00:00:10")
Application.OnTime dTime, "Scanimport"
End Sub
I have found a lot of similar examples on these forums, but they don't use this convenient .Add(Connection:= part, and I don't understand how I can incorporate this in the various codes used to determine the most recent file.