+ Reply to Thread
Results 1 to 8 of 8

Import a hex file into Excel

  1. #1
    Registered User
    Join Date
    10-08-2013
    Location
    Zambia
    MS-Off Ver
    MS365 64-bit
    Posts
    18

    Import a hex file into Excel

    I am looking for VBA to export a binary file (in hex) into Excel.

    I currently use a hex editor to open the binary file, and set it to show 100 bytes in each row. The hex output looks something as follows:

    Please Login or Register  to view this content.
    etc.

    I then select all of this, copy and paste into Excel, and use text to columns to fill a range with all of the hex values (100 columns wide, by ~2,000 rows long).

    Instead, I want to automatically extract the hex into Excel without having to first open the file in a hex editor, copy and paste, etc.

    I found the following code that is able to pull out one byte at a time, but there are two issues:it imports in decimal (instead of hex), and it places one value in a row, then the next value in the next row, etc.
    Instead, I need the first value in A1, the second value in B1, the third value in C1, etc. Until it has filled up to A100, then start in the next row. Byte 101 in B1, the next in B2, etc.
    I hope that makes sense!

    Here is the code I used:
    Please Login or Register  to view this content.
    I am almost there, but the main issues are:
    - Need to paste directly in hex, (not decimal first and then using DEC2HEX to convert to hex)
    - Fill 100 cells in row 1, before moving to row 2, fill 100 cells, move to row 3, etc.

    Thanks!
    Attached Files Attached Files
    Last edited by 6StringJazzer; 02-15-2024 at 10:40 AM. Reason: not export

  2. #2
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS365 Family 64-bit
    Posts
    24,721

    Re: Import a hex file into Excel

    Format the entire worksheet as Text then use this code. Sample file attached. Fully tested with your data.
    Please Login or Register  to view this content.
    Attached Files Attached Files
    Jeff
    | | |會 |會 |會 |會 | |:| | |會 |會
    Read the rules
    Use code tags to [code]enclose your code![/code]

  3. #3
    Registered User
    Join Date
    10-08-2013
    Location
    Zambia
    MS-Off Ver
    MS365 64-bit
    Posts
    18

    Re: Import a hex file into Excel

    Thanks for the super fast response. I ran it and it looks great. There's just one issue that has me stumped.

    I first ran the VBA code, then extracted the hex manually, i.e. copying and pasting from the hex editor, and compared the two datasets in separate worksheets in this workbook. It is sprinkled with differences all throughout. Take a look at the attached.

    I used conditional formatting to highlight differences from the manual one to the VBA one. The VBA worksheet has the conditional formatting.

    For some of them, it's a leading zero that gets dropped. E.g. "0E" becomes "E" (see cell L9), but then for some others it just changes to "00" (see D4 and H5). Thoughts?

    I've included in the ZIP file the working Excel file (xlsb format), as well as a snippet from the import file, containing only the first 15 rows of the full file (it took too long to extract the entire thing every time).

    P.S. Is there a way to force Excel to look for the import file in the current directory?? I kept failing (not finding anything), until I moved the file to c:\ and changing the VBA to point to it directly, including path (i.e. C:\import.ros)
    Attached Files Attached Files

  4. #4
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS365 Family 64-bit
    Posts
    24,721

    Re: Import a hex file into Excel

    When VBA converts a value to Hex, if the value is F or less it doesn't need to add a leading zero, so it doesn't. Because the result is a text string, you can't force the leading zero with the Format function. So I had to add it explicitly.

    I was stumped why 122 should be x7A but shows up as 00. So I just wrote around it. This code seems to solve the problems.

    Please Login or Register  to view this content.

  5. #5
    Registered User
    Join Date
    10-08-2013
    Location
    Zambia
    MS-Off Ver
    MS365 64-bit
    Posts
    18

    Thumbs up Re: Import a hex file into Excel

    Phenomenal... Thank you so much!

  6. #6
    Registered User
    Join Date
    10-08-2013
    Location
    Zambia
    MS-Off Ver
    MS365 64-bit
    Posts
    18

    Re: Import a hex file into Excel

    I almost forgot... Is there any way to force Excel to look in the workbook's current folder for the import file, so that there is no need for the entire path to be supplied in the VBA code?

    If I just use "import.ros" then the output is a single 00 in cell A1, so it looks like Excel can't find the file.

  7. #7
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS365 Family 64-bit
    Posts
    24,721

    Re: Import a hex file into Excel

    That's exactly what I did and it worked for me.


    Please Login or Register  to view this content.

  8. #8
    Registered User
    Join Date
    10-08-2013
    Location
    Zambia
    MS-Off Ver
    MS365 64-bit
    Posts
    18

    Re: Import a hex file into Excel

    It's weird, for me it works the first time, then when I try again it just imports 00.

    After a lot of Googling and experimenting I found a way to make it work:
    Please Login or Register  to view this content.
    But I have another problem that I'm struggling to work out...

    How do I then export it back out to a new.ros file? I've made some changes, then I need to export the same info (from Hex!A1:CV2192 as well as the last 20 bytes in row 2193) into a new ros file, say:
    Please Login or Register  to view this content.
    Thanks again for all the help, I'm learning heaps!

+ 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. Replies: 1
    Last Post: 11-08-2022, 05:26 AM
  2. How to export pdf from excel file
    By icytuvi in forum Excel General
    Replies: 4
    Last Post: 10-16-2021, 03:00 PM
  3. Export Excel records to seperate text file & compress it to protecte ZIP file
    By firedragon in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 03-03-2012, 10:47 AM
  4. Export file from Excel
    By okelly in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 05-12-2008, 08:07 AM
  5. Replies: 1
    Last Post: 12-02-2007, 05:52 AM
  6. Export excel file to semicolon delimited text file
    By capitan in forum Excel General
    Replies: 5
    Last Post: 04-06-2005, 11:07 PM
  7. Export excel file to semicolon delimited text file
    By capitan in forum Excel General
    Replies: 5
    Last Post: 04-06-2005, 11:07 PM

Tags for this Thread

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