Import a text file via macro, then select and import certain data.
Hi all,
Macro noob here, would love some help.
I have a simple Excel table where I would like to import a .txt file via the button on the spreadsheet and auto populate the cells 'calibration flashed'.
So UNIT 1 would populate cells C8 and C9 with the info contained in the UNIT 1 section of the .txt file and so on.
The reason this is difficult is there is a set .txt format, so some bits of info will need to be ignored in the txt file.
Documents are attached as is a screen shot of the sheet and the text file.
First, according to forum rules thanks to not create any duplicate thread anymore …
According to your last attachment as it is (no mod is required and no needs to 'Clear Cells') a VBA basics demonstration for starters :
PHP Code:
Sub Demo1()
Const D = ": "
Dim V, R&, W, X(1, 0)
V = Application.GetOpenFilename("Text files,*.txt"): If V = False Then Exit Sub
R = FreeFile
Open V For Input As #R
V = Split(Input(LOF(R), #R), vbCrLf)
Close #R
R = 6
While Not IsEmpty(Cells(R, 2))
W = Application.Match("Reading Part Numbers and IDs from: " & Cells(R, 2).Text, V, 0)
If IsNumeric(W) Then X(0, 0) = Split(V(W + 1), D)(1): X(1, 0) = RTrim(Split(V(W + 3), D)(1))
Cells(R + 2, 4).Resize(2) = X
Erase X
R = R + 5
Wend
End Sub
► Do you like it ? ► ► So thanks to click on bottom left star icon « ★ Add Reputation » ! ◄ ◄
Re: Import a text file via macro, then select and import certain data.
Apologies for the dual post.
Thanks for the code, very helpful.
I am trying to make it work in a different way, the text file I have supplied is much simpler than what I will actually use (I have simplified it / removed private info).
For the Macro to work it needs to:
1. Search for a specific line ''Reading Part Numbers and IDs from: PDM''
2. then it needs to pick the Software version and Calibration version from that section
3. then it will place it in the correct cells (which it already does)
this then needs to be repeated for each of the 4 sections (PDM, CBM, BBH, BBH2). This will then mean when the text file is much larger it will search for a specific line, then pick the Software version and Calibration version from that section.
Re: Import a text file via macro, then select and import certain data.
According to your post #6 attachment it is exactly what my VBA demonstration does …
So you must well elaborate your need with details in order there is nothing to guess as guessing can't be coding !
As proceeding like you did means you are enough confident with your Excel / VBA skills to fit any helper code
but if it's not the case you just wasted helpers time …
You can attach a workbook with a before state worksheet and an expected result worksheet
according to a 'sanitized' exact layout source text file …
Your initial explanation is so far from your real need, to really avoid if you are not able to fit any helper code …
Accuracy is the only path to an efficient solution.
According to the last attachment another Excel / VBA basics demonstration where 'Clear Cells' button is useless :
PHP Code:
Sub Demo2()
Dim V, R&, W, S, X, Y, L&
V = Application.GetOpenFilename("Text files,*.txt"): If V = False Then Exit Sub
R = FreeFile
Open V For Input As #R
V = Split(Input(LOF(R), #R), "Filter set OK for ECU: ")
Close #R
W = [{"--Software Version (JLR) * : *","--Calibration Version (JLR) * : *","--Veh Mfr Software Number * : *"}]
For R = 6 To Cells(Rows.Count, 2).End(xlUp).Row
With Cells(R, 2).CurrentRegion.Rows
.Item("3:" & .Count).Columns(3).ClearContents
S = .Cells(1) & vbCrLf & "*"
For Each X In V
If X Like S Then
Y = Split(X, vbCrLf)
For L = 3 To .Count
X = Application.Match(.Cells(L, 1), [{"SW Version ","Calibration Version ","Mfr SW Number"}], 0)
If IsNumeric(X) Then
X = Application.Match(W(X), Y, 0)
If IsNumeric(X) Then .Cells(L, 3) = Split(Y(X - 1), ": ")(1)
End If
Next
Exit For
End If
Next
R = R + .Count
End With
Next
End Sub
► Do you like it ? ► ► So thanks to click on bottom left star icon « ★ Add Reputation » ! ◄ ◄
Last edited by Marc L; 06-01-2022 at 06:19 AM.
Reason: little tweak …
Bookmarks