+ Reply to Thread
Results 1 to 24 of 24

VBA code to import and format log file

  1. #1
    Forum Contributor
    Join Date
    11-14-2014
    Location
    uk
    MS-Off Ver
    Excel 2010
    Posts
    100

    VBA code to import and format log file

    Hi
    Is it possible to code excel so that it will import the last X number of lines from the log file i have uploaded ?

    I have renamed the log file to .txt and have also uploaded a screenshot of the original showing the encoding method.

    The log file changes size frequently.

    I dont really care what the output looks like providing just the words and numbers (no strange symbols) are within individual cells each time the code runs.

    I have tried to achieve what i want by just importing the data from the original log file and also from the file changed to .txt using the various options available but I just can't get any order to it

    thanks for looking
    Attached Images Attached Images
    Attached Files Attached Files
    Last edited by semajjames; 02-13-2020 at 10:35 AM.

  2. #2
    Valued Forum Contributor
    Join Date
    01-14-2013
    Location
    Austria
    MS-Off Ver
    2016 / 2019
    Posts
    339

    Re: VBA code to import and format log file

    Please Login or Register  to view this content.
    adopt the file path and the cell to copy the result

    cheers

  3. #3
    Forum Contributor
    Join Date
    11-14-2014
    Location
    uk
    MS-Off Ver
    Excel 2010
    Posts
    100

    Re: VBA code to import and format log file

    Thanks for taking the time with this,

    That has sorted out which rows i want to look at, now is it possible for your code to get the layout more like this new upload so things maintain column structure and are contained within cells, without the unwanted symbols ?

    so a cell containing ”‚ EMA spread would become just EMA spread and a cell containing ”‚ 0.09575415 would become just 0.09575415

    That new upload btw is my attempt using import file function but as you see i have unwanted symbols inside data cells and no way to run the import constantly on a timed loop


    I would like to add that when i run the code i would like it to continue working on a file extension of .log purely because i have many files that need to be loaded regularly and i dont want to be renaming all the time

    thanks
    Attached Files Attached Files
    Last edited by semajjames; 02-14-2020 at 06:08 AM.

  4. #4
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 Version 2404 Win 11 Home 64 Bit
    Posts
    23,855

    Re: VBA code to import and format log file

    If you have excel version 2010 or later (Your profile shows 2007), then you might try importing the file to Power Query and manipulating the data as necessary to meet your requirements. If you are running a later version of Excel than 2007, please update your profile accordingly.
    Alan עַם יִשְׂרָאֵל חַי


    Change an Ugly Report with Power Query
    Database Normalization
    Complete Guide to Power Query
    Man's Mind Stretched to New Dimensions Never Returns to Its Original Form

  5. #5
    Valued Forum Contributor
    Join Date
    01-14-2013
    Location
    Austria
    MS-Off Ver
    2016 / 2019
    Posts
    339

    Re: VBA code to import and format log file

    I analyzed your original log

    you have to reformat line per line and exchange all "strange" characters.

    I could not put the function sFormatLine(sLine As String) here because it did not work.
    Most probably all the strange characters prevent normal functionality (could not submit).

    the rest is almost like the first time

    cheers,
    guenter
    Attached Files Attached Files

  6. #6
    Forum Expert
    Join Date
    10-06-2017
    Location
    drevni ruchadlo
    MS-Off Ver
    old
    Posts
    2,150

    Re: VBA code to import and format log file

    Quote Originally Posted by semajjames View Post
    ... I have renamed the log file to .txt and have also uploaded a screenshot of the original showing the encoding method ...
    What is the format of this original log before changing to txt - is this some 'dbf', 'csv', 'prn', etc. ?
    And why are you using '1252' encoding instead of 'utf-8' below ?

  7. #7
    Forum Contributor
    Join Date
    11-14-2014
    Location
    uk
    MS-Off Ver
    Excel 2010
    Posts
    100

    Re: VBA code to import and format log file

    Quote Originally Posted by gue2013 View Post
    I analyzed your original log

    cheers,
    guenter
    Thanks this is Great.

    I now have access to all the data I originally imagined might be a possibility.

    The power of some lines of code always amazes me.

    If anything more, only if very easy could the below section move to the right

    ɢƱҊɮøͳ RT Edition Version Round Exchange Pair Strategy Timestamp

    then also giving me access to this below section in columns/cells

    2020/02/06 16:00:11: Moving TrailingStop limit to 120.02262393912

  8. #8
    Valued Forum Contributor
    Join Date
    01-14-2013
    Location
    Austria
    MS-Off Ver
    2016 / 2019
    Posts
    339

    Re: VBA code to import and format log file

    if you want to move it to the right you have only to add another replace function and add a TAB before the line

  9. #9
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: VBA code to import and format log file

    Hello semajjames,

    The attached workbook should be very close to what you want. I can tweak it as needed. I ran this on the example log file you posted.

    Module1 Macro Code
    Please Login or Register  to view this content.
    Attached Files Attached Files
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  10. #10
    Forum Contributor
    Join Date
    11-14-2014
    Location
    uk
    MS-Off Ver
    Excel 2010
    Posts
    100

    Re: VBA code to import and format log file

    So as not to appear ignorant to any you guys I am going to talk with you both as it would seem that either might solve my wishes completely

  11. #11
    Forum Contributor
    Join Date
    11-14-2014
    Location
    uk
    MS-Off Ver
    Excel 2010
    Posts
    100

    Re: VBA code to import and format log file

    Quote Originally Posted by gue2013 View Post
    if you want to move it to the right you have only to add another replace function and add a TAB before the line
    I've studied your code to the best of my knowledge but I have no idea how to do what you are saying or how to make these changes

  12. #12
    Forum Contributor
    Join Date
    11-14-2014
    Location
    uk
    MS-Off Ver
    Excel 2010
    Posts
    100

    Re: VBA code to import and format log file

    Quote Originally Posted by Leith Ross View Post
    Hello semajjames,

    The attached workbook should be very close to what you want. I can tweak it as needed. I ran this on the example log file you posted.
    there a couple things with this code:-

    1/this stuff is missing

    GBRT_16.5.2_RC USDT-DASH@poloniex Round #2652 2020/02/06 15:59:04
    ************************************************************************************************
    BEGIN TRAILME! LOGS for TrailMe! BUY
    ************************************************************************************************
    ------------------------------------------------------------------------------------------------
    2020/02/06 15:59:04: TrailingBuy limit 119.7956027366756
    ------------------------------------------------------------------------------------------------
    2020/02/06 15:59:04: Last price 120.10829203 | Target to buy: 119.89561431188811
    ------------------------------------------------------------------------------------------------
    2020/02/06 15:59:04: Stop limit 120.03543
    ------------------------------------------------------------------------------------------------
    Price to buy is not in TSSL range
    ************************************************************************************************
    BEGIN TRAILME! LOGS for TrailMe! SELL
    ************************************************************************************************
    ------------------------------------------------------------------------------------------------
    2020/02/06 15:59:05: TrailingStop limit 120.30039869088
    ------------------------------------------------------------------------------------------------
    2020/02/06 15:59:05: Last price 119.56208652 | Target to sell: 119.60130829869416
    ------------------------------------------------------------------------------------------------
    2020/02/06 15:59:05: StopLoss limit 119.73820305435
    ------------------------------------------------------------------------------------------------

    2/ I am not sure that your code is reading the last x number of rows ( I cant find a place in the code for me to insert the number of rows required) basically the log file gets very much longer but i am only interested in the last cycle at the end at any time (the cycle at the bottom )

    3/ I will be inserting the code into another code already running in a loop so need your code to be able to just read my log file from a defined file path rather than having to browse for the file

    thanks

    3/

  13. #13
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Arrow

    Hi,

    according to your initial text file just attach at least the exact filled expected result workbook …

  14. #14
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: VBA code to import and format log file

    Hello semajjames,


    Do you need all the punctuation lines i.e. lines like *************, +_____________+, etc intact?

    Are you saying you require only the last entry of the log file to be imported to the worksheet? If this is the case there is no need for you to input the number lines. The macro can isolate that section for you.

    I can include a hard coded path for the log file. You can either tell me what the path is or edit the code yourself.

  15. #15
    Forum Expert
    Join Date
    10-06-2017
    Location
    drevni ruchadlo
    MS-Off Ver
    old
    Posts
    2,150

    Re: VBA code to import and format log file

    Quote Originally Posted by semajjames View Post
    ... i am only interested in the last cycle at the end at any time (the cycle at the bottom )...
    1. If you do not have many log files to process, then you only need to manually import data from the "import from text" option with 65001 Unicode (utf-8) encoding. In the import window you can specify from which rows the data should be imported.

    2. However, if you need all "text" from file (including the tables from the log file), then you need to examine unicode characters from 9472 to 9551 - the table frames in the log are built of them:

    Table frame => ChrW range from 9474/9484 to 9547 (the full range: from 9472 to 9551):
    "-¦¦"..."¦T¦"..."¦¦¬" => 9484 9472 9472 ... 9472 9516 9472 ... 9472 9472 9488
    "+¦¦"..."¦+¦"..."¦¦+" => 9500 9472 9472 ... 9472 9532 9472 ... 9472 9472 9508
    "L¦¦"..."¦+¦"..."¦¦-" => 9492 9472 9472 ... 9472 9524 9472 ... 9472 9472 9496

    The modified Leith Ross code will work very well for that.
    Last edited by mjr veverka; 02-16-2020 at 03:54 PM.

  16. #16
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: VBA code to import and format log file

    Hello semajjames,

    This version uses a fixed file path. The macro code has been changed to provide a "clean" output by dividing the text at natural breaks. It starts at cell "A1" on the "Sheet1". This can be changed to start on the next empty row of the worksheet if you like. Let me know and I will will amend the code.

    Module1 - Updated Macro Code
    Please Login or Register  to view this content.
    Attached Files Attached Files

  17. #17
    Forum Contributor
    Join Date
    11-14-2014
    Location
    uk
    MS-Off Ver
    Excel 2010
    Posts
    100

    Re: VBA code to import and format log file

    Quote Originally Posted by Leith Ross View Post
    Hello semajjames,


    Do you need all the punctuation lines i.e. lines like *************, +_____________+, etc intact?

    Are you saying you require only the last entry of the log file to be imported to the worksheet? If this is the case there is no need for you to input the number lines. The macro can isolate that section for you.

    I can include a hard coded path for the log file. You can either tell me what the path is or edit the code yourself.
    I dont need the punctuation ************ or ____________ just the words and numbers in cells

    I need the last complete cycle of data at any time imported to work sheet ( aprox 40 lines),,, but as this was hard to define at this point due to stuff being removed ect I thought it easier to have the number of line variable available to me, and also gives me flexibility should I want more than just one cycle i can edit code to provide this (the log file gets very long)

    please make it so I can define/edit the file path because I have many files to work with all at the same time and would duplicate the code

  18. #18
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: VBA code to import and format log file

    Hello samejjames,

    Is the complete cycle from "?????? RT Record" to the line below "Profit/Loss" ?

    Will this record be copied to the next empty row on the worksheet?

    Will the files all be located in the same folder? Is so, what is the folder path and what do the file names look like?

  19. #19
    Forum Contributor
    Join Date
    11-14-2014
    Location
    uk
    MS-Off Ver
    Excel 2010
    Posts
    100

    Re: VBA code to import and format log file

    Quote Originally Posted by Leith Ross View Post
    Hello samejjames,

    Is the complete cycle from "?????? RT Record" to the line below "Profit/Loss" ?
    in this case the complete cycle is from

    GBRT_16.5.2_RC USDT-DASH@poloniex Round #2652 2020/02/06 15:59:04
    to
    All good...keep calm and Gunbot.



    but this can change with each different .log file tho the basic format is the same just some lines could be omitted from the file which is why i kind of need to be able to define the amount of lines

    the files will all be in the same folder and the names will look like this:- poloniex.USDT-DASH.log

    ok so the worksheet ,,, the code can first "clear the worksheet" then copy to the worksheet.
    I guess to save having a separate worksheet for each log file the first instance of the code could start at A:1 then additional instances could use the next available column's along defined by user ?
    Last edited by semajjames; 02-16-2020 at 09:19 PM.

  20. #20
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: VBA code to import and format log file

    Hello semajjames,

    I have made some changes to the import macro and the workbook. The output now appears on "Sheet2" while the button to run the macro is on "Sheet1".

    Each time the macro runs "Sheet2" is cleared. Each file will be separated from the next file by an empty column.

    The folder path is fixed. You will need to edit the macro to point to the folder you will be using. The macro will examine the files in the folder and only import those that match the pattern "*.DASH.log". Case is ignored.

    As for your request about the specifying lines, I have not approached this problem. Since each line is cleaned before it is output, the number of lines in the original log file will not match up with the cleaned lines output. Taking a guess at how many you need to display seems like more work. Perhaps if I were involved in day trading cryptos, I would better understand what you doing.

    Anyway, look it over and let me know what you think.

    Please Login or Register  to view this content.
    Attached Files Attached Files

  21. #21
    Forum Contributor
    Join Date
    11-14-2014
    Location
    uk
    MS-Off Ver
    Excel 2010
    Posts
    100

    Re: VBA code to import and format log file

    Hi Leith Ross

    I have added the folder path in one place as follows

    Please Login or Register  to view this content.
    the folder contains the files

    poloniex.USDT-BCHSV.log
    poloniex.USDT-DASH.log
    poloniex.USDT-ETC.log
    poloniex.USDT-LTC.log
    poloniex.USDT-TRX.log

    unfortunately tho when i run the macro i get the error shown in attached screen shots

    I have also tried with just this one file in the folder

    DASH.log

    I have tried changing code below by removing the "*DASH" and just leaving ".log" When I do that the code then runs but does not copy to sheet.

    Please Login or Register  to view this content.
    I now get your point about "the number of lines" where by having to import before clean ,,,

    The log files come a running piece of software with no user GUI, I am or rather you are kindly trying to make a simple GUI for me,
    At present time the files contain 200000 lines each and update. I am only interested in the most recent cycle at any one time but the number of lines the cycles consist of changes ! So could we just say read and clean the LAST 300 lines ? this will cover all possible cycle sizes.

    I am trying to save excel importing then cleaning 200000+ lines x 8 when they are not needed.

    thanks again for your patience
    Attached Images Attached Images
    Last edited by semajjames; 02-18-2020 at 09:33 AM.

  22. #22
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: VBA code to import and format log file

    Hello semajjames,

    Okay, that was my mistake. Delete the macro ImportLogFile and replace it with this amended macro code. The first problem was with the file name. I was looking for DASH in the name instead of poloniex. The second problem was the folder path was not included with the file name. I don't use Dir that much and often forget it only returns the file name and not the fully qualified path.

    I will need to make some more changes to extract the last 300 lines. Before I do, I want to know this code is working correctly.

    Please Login or Register  to view this content.

  23. #23
    Forum Contributor
    Join Date
    11-14-2014
    Location
    uk
    MS-Off Ver
    Excel 2010
    Posts
    100

    Re: VBA code to import and format log file

    Hi Leith Ross

    So all I should be adding is the line below, just the one position ?

    Path = "C:\gb_logs"

    if so then it still not quite getting there

    see attached
    Attached Images Attached Images

  24. #24
    Forum Contributor
    Join Date
    11-14-2014
    Location
    uk
    MS-Off Ver
    Excel 2010
    Posts
    100

    Re: VBA code to import and format log file

    for some reason my last two attached files have become invisible and I dont know why this could be ??

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Import text file to different column format
    By spriggan in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 03-25-2016, 12:37 AM
  2. modify .csv file to get into format for import to another program.
    By wingfield65 in forum Excel Programming / VBA / Macros
    Replies: 12
    Last Post: 01-02-2015, 07:44 PM
  3. Import data from text file and format
    By JamesL555 in forum Excel General
    Replies: 15
    Last Post: 04-17-2014, 09:36 AM
  4. VBA code to import text file to the fixed excel file
    By wangdian in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 08-30-2010, 10:13 AM
  5. Is there way to Import PDB file (.pdb format) to excel
    By sam76210 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 03-24-2006, 05:23 PM
  6. Replies: 1
    Last Post: 01-09-2006, 06:20 PM
  7. [SOLVED] Import VBA Code in Excel-File ? (Export VBA Code to file)
    By Matthias Pospiech in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 03-22-2005, 01:06 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1