Macro to extract text from .txt file into Excel

striker359

Board Regular
Joined
Jun 19, 2014
Messages
64
Hi,

I'm completely new to macro building but here goes:

I would like to extract info from an external txt file into Excel. For example:

txt file:

/* ----------------- Dummy ------------------- */
JobName: Dummy
send_notifications: y
max_runs: 5


/* ----------------- TAF-O-FF401D ------------------- */
JobName: TAF-O-FF401D
send_notifications: y
max_runs: 5

I would require a way to read the item category (JobName, send_notifcations, max_runs), for lack of a better term, and then to populate the corresponding info into specific columns in Excel.

For example:

JobName (Dummy) in Column A
send_notifications (y) in Column B
max_runs (5) in Column C

With the /* ----------------- JobName ------------------- */ indicating a new job entry, I would require the next job entry to be on the next row and so on till there are no more job entries left.

Thanks in advance!
 
Last edited:
Hi Tyger,

Not sure if you are able to solve this, seeing that the delimiter used is a space but unfortunately for the notification_msg tag, the items DO have spaces in them, thus the macro only extracts the first word in the tag.

I have an idea but not too sure how to execute it in VBA.

1. In the macro, enclose the item in " " so that it is treated as one string
2. Remove the " " during the print to cell



P.S I'm not sure how to find what tag the macro is looking at anymore, since there's no mention of what the search criteria.
How does that even work?

Does it mean that if one job has its tags in a different order as the rest, the data input for that job would be screwed up?
eg. Normal Sequence is: insert_job, description, condition


Jumbled Sequence is: condition, insert_job, description
 
Upvote 0

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
Ok just tried jumbling the order and it still copies fine... Now i'm even more curious how the macro works coz I have totally no idea how to debug this baby right now.
(●´ω`●)ゞ

Hi Tyger,

Not sure if you are able to solve this, seeing that the delimiter used is a space but unfortunately for the notification_msg tag, the items DO have spaces in them, thus the macro only extracts the first word in the tag.

I have an idea but not too sure how to execute it in VBA.

1. In the macro, enclose the item in " " so that it is treated as one string
2. Remove the " " during the print to cell



P.S I'm not sure how to find what tag the macro is looking at anymore, since there's no mention of what the search criteria.
How does that even work?

Does it mean that if one job has its tags in a different order as the rest, the data input for that job would be screwed up?
eg. Normal Sequence is: insert_job, description, condition


Jumbled Sequence is: condition, insert_job, description
 
Upvote 0
The sheet is using the List of Options sheet to find the criteria .

Code:
I = I + 1  ' Makes I equal 3 since it was first set to equal 2 
LastRowColc = wsResults.Range("C65536").End(xlUp).Row
                   If Cell.Value = wsOptions.Range("A" & I).Value Then  ' Checks to see if cell equals what is in cell A3 of the options sheet 
              Note = Cell.Offset(0, 1).Value 
                   wsResults.Range("C" & LastRowColc + 1).Value = Note   
                End If
 
Upvote 0
I'm not quite sure what the data looked like for notification_msg. Can you give examples



Ok just tried jumbling the order and it still copies fine... Now i'm even more curious how the macro works coz I have totally no idea how to debug this baby right now.
(●´ω`●)ゞ
 
Upvote 0
Another problem is that for some tags, eg. start_times,the data is extracted to the row below the supposed job's row:

For eg:

If the job at row 30 has a start_time, the start_time from that job is printed on row 31 instead.
 
Last edited:
Upvote 0
More than likely the please contact dummy is being separated into different cells instead of one. This is cause of how the query was set up.

Change
Code:
TextFileSpaceDelimiter = True


To

Code:
TextFileSpaceDelimiter = False




notification_msg: Please contact Dummy


What ends up getting extracted is just Please
 
Upvote 0
More than likely the please contact dummy is being separated into different cells instead of one. This is cause of how the query was set up.

Change
Code:
TextFileSpaceDelimiter = True


To

Code:
TextFileSpaceDelimiter = False

That's what I tried but in the end nothing was printed when the space delimiter was switched off
 
Upvote 0
For this you'll have to change the code to have an offset(-1,0) so it goes up one row


Another problem is that for some tags, eg. start_times,the data is extracted to the row below the supposed job's row:

For eg:

If the job at row 30 has a start_time, the start_time from that job is printed on row 31 instead.
 
Upvote 0
Right now there are 2 main problems I've encountered:

1. When there's a space in the data, only the first word is extracted. (I understand that it's caused by the space delimiter, but when the space delimiter is turned off, nothing gets extracted.)

2. The data printed occasionally end up in the wrong row (aka data from one job is printed onto the row of another job instead).


PS. Is this supposed to be created for every column?

Code:
For Each Cell In Range("B1:B6000")
If Cell.Value = "" Then
Cell.Value = "na"
End If
Next

If so, I've made it to become like that and as expected, it took a way longer time to finish executing because of the greater range involved.

Code:
For Each Cell In Range("A1:AA6000")
If Cell.Value = "" Then
Cell.Value = "na"
End If
Next


Even so, I failed to notice any difference between the 2 codes, so I was wondering what this code actually did, considering the fact that I saw no "na" even when i removed this code:

Code:
For Each Cell In wsResults.Range("A1:Z6000")
 If Cell.Value = "na" Then Cell.Clear
 Next
 
Upvote 0

Forum statistics

Threads
1,215,140
Messages
6,123,269
Members
449,093
Latest member
Vincent Khandagale

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top