+ Reply to Thread
Results 1 to 29 of 29

macro import file hex in excel

Hybrid View

  1. #1
    Registered User
    Join Date
    01-19-2012
    Location
    italy
    MS-Off Ver
    Excel 2003
    Posts
    16

    macro import file hex in excel

    cerco una macro che mi permetta di importare un file hex in excel.
    tanks

    I want a macro that allows me to import a hex file into Excel.
    tanks
    Last edited by tatop; 01-19-2012 at 07:09 PM. Reason: error only italian

  2. #2
    Forum Expert martindwilson's Avatar
    Join Date
    06-23-2007
    Location
    London,England
    MS-Off Ver
    office 97 ,2007
    Posts
    19,320

    Re: macro import file hex in excel

    a hex file? what is that. is it a text file with hex numbers?
    "Unless otherwise stated all my comments are directed at OP"

    Mojito connoisseur and now happily retired
    where does code go ?
    look here
    how to insert code

    how to enter array formula

    why use -- in sumproduct
    recommended reading
    wiki Mojito

    how to say no convincingly

    most important thing you need
    Martin Wilson: SPV
    and RSMBC

  3. #3
    Registered User
    Join Date
    01-19-2012
    Location
    italy
    MS-Off Ver
    Excel 2003
    Posts
    16

    Re: macro import file hex in excel

    Quote Originally Posted by martindwilson View Post
    a hex file? what is that. is it a text file with hex numbers?
    I have to import the files with a programmer to read eeprom ... having an extension. bin and contain the hex.
    tanks

  4. #4
    Forum Contributor
    Join Date
    12-28-2011
    Location
    England
    MS-Off Ver
    Excel 2007
    Posts
    280

    Re: macro import file hex in excel

    If you have a bin(ary) file, then don't you want to open it in a hex editor to review/make changes? Apologies, i just can't see why you might open it in Excel...

  5. #5
    Registered User
    Join Date
    01-19-2012
    Location
    italy
    MS-Off Ver
    Excel 2003
    Posts
    16

    Re: macro import file hex in excel

    Quote Originally Posted by Firefly2012 View Post
    If you have a bin(ary) file, then don't you want to open it in a hex editor to review/make changes? Apologies, i just can't see why you might open it in Excel...
    I would like to open it in Excel because I have to perform calculations using the algorithm on the file and then save it in. bin, to rewrite the EEPROM

  6. #6
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: macro import file hex in excel

    Not too complicated; reading as HEX file, converting to ascii

    sub snb()
      Open "E:\file.bin" For Binary As #1
       c00 = Input(LOF(1), #1)
      Close #1
    
      redim sn(len(c00)+1)
    
      For j = 1 To len(c00) 
        sn(j) = Asc(Mid(c00, j, 1))
      Next
    
      cells(1)=join(sn,"")
    End sub



  7. #7
    Registered User
    Join Date
    01-19-2012
    Location
    italy
    MS-Off Ver
    Excel 2003
    Posts
    16

    Re: macro import file hex in excel

    Quote Originally Posted by snb View Post
    Not too complicated; reading as HEX file, converting to ascii

    sub snb()
      Open "E:\file.bin" For Binary As #1
       c00 = Input(LOF(1), #1)
      Close #1
    
      redim sn(len(c00)+1)
    
      For j = 1 To len(c00) 
        sn(j) = Asc(Mid(c00, j, 1))
      Next
    
      cells(1)=join(sn,"")
    End sub
    TANKS for macro but I try a macro that displays the values ​​of excel a BIN file in hex, sorted as if it were an editor and divided by single byte.
    Attached Images Attached Images

  8. #8
    Forum Expert martindwilson's Avatar
    Join Date
    06-23-2007
    Location
    London,England
    MS-Off Ver
    office 97 ,2007
    Posts
    19,320

    Re: macro import file hex in excel

    i still don't understand, what would an example excel file look like?

  9. #9
    Registered User
    Join Date
    01-19-2012
    Location
    italy
    MS-Off Ver
    Excel 2003
    Posts
    16

    Re: macro import file hex in excel

    Quote Originally Posted by martindwilson View Post
    i still don't understand, what would an example excel file look like?
    0 1 2 3 4 5 6 7 8 9 a b c d e f
    0 af 12 bf ff 44 5f 6f ff ff 12 f1 34 55 89 ff ac
    10 5f 6f ff ff 12 ff 44 5f 6f ff 5f 6f ff ff 12 12
    20 f1 34 f1 34 55 89 ff ac ff 44 5f 6f ff ff 12 12
    30 6f f1 34 55 89 f1 34 55 89 ff ac ff 44 5f 6f ff
    40 5f 6f ff ff 12 5f 6f ff ff 12 ff 44 5f 6f ff ff
    50 bf ff 44 5f 6f ff ff 12 f1 34 55 89 ff ac ff ff
    60 f1 34 55 89 5f 6f ff ff 12 12 f1 34 55 89 ff ac
    70 f1 34 55 89 5f 6f ff ff 12 12 f1 34 55 89 ff ac
    80 f1 34 55 89 ff ac ff 44 5f 6f ff ff 12 ff 44 5f
    90 ff 44 5f 6f ff bf ff 44 5f 55 89 ff ac ff 44 5f
    100 f1 34 bf ff 44 5f 6f ff ff 12 f1 34 55 89 ff ac
    Attached Files Attached Files

  10. #10
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: macro import file hex in excel

    Yes I see, but are you aware that a cell in Excel can only show a limited amount of characters ?
    So where do you want the hex characters to be shown in Excel ??

  11. #11
    Registered User
    Join Date
    01-19-2012
    Location
    italy
    MS-Off Ver
    Excel 2003
    Posts
    16

    Re: macro import file hex in excel

    Quote Originally Posted by snb View Post
    Yes I see, but are you aware that a cell in Excel can only show a limited amount of characters ?
    So where do you want the hex characters to be shown in Excel ??
    ok, every single cell in excel it must contain two characters (a single bite (AB) ok ?????
    see attached sample file first

  12. #12
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: macro import file hex in excel

    You only have to change the file's name

    Sub snb()
      Open "G:\midbmut_snb.dbf" For Binary As #1
        c00 = Input(LOF(1), #1)
      Close #1
      
      For j = 1 To Len(c00)
        Cells(j \ 16 + 1, (j - 1) Mod 16 + 1) = "'" & Right("00" & Hex(Asc(Mid(c00, j, 1))), 2)
      Next
    End Sub

  13. #13
    Registered User
    Join Date
    01-19-2012
    Location
    italy
    MS-Off Ver
    Excel 2003
    Posts
    16

    Re: macro import file hex in excel

    thanks for the macro, but I have two problems:
    1) the last cell of the first line is empty and the value of that cell is inserted into the cell below, so moving 'the last column at the bottom of a cell;
    2) values ​​in individual cells are displayed as text but I would display them as hexadecimal values ​​as I have to make calculations.
    You can edit the macro please
    thanks a lot
    Attached Files Attached Files

  14. #14
    Forum Guru
    Join Date
    03-02-2006
    Location
    Los Angeles, Ca
    MS-Off Ver
    WinXP/MSO2007;Win10/MSO2016
    Posts
    12,943

    Re: macro import file hex in excel

    Try this one:
    Option Explicit
    Sub BinToHex()
        Dim ColumnNumber    As Long, _
            RowNumber       As Long, _
            Ndx             As Long, _
            BEN             As Worksheet, _
            InBucket        As Variant
            
        Set BEN = Sheets("BEN")
        
        Open "c:\downloads\encoding.bin" For Binary As #1
        InBucket = Input(LOF(1), #1)
        Close #1
        RowNumber = 1
        ColumnNumber = 0
    
        For Ndx = 1 To Len(InBucket)
            ColumnNumber = ColumnNumber + 1
            BEN.Cells(RowNumber, ColumnNumber) = Hex(Asc(Mid(InBucket, Ndx, 1)))
            If (Ndx Mod 16) = 0 Then
                RowNumber = RowNumber + 1
                ColumnNumber = 0
            End If
        Next Ndx
      End Sub
    Ben Van Johnson

  15. #15
    Registered User
    Join Date
    01-19-2012
    Location
    italy
    MS-Off Ver
    Excel 2003
    Posts
    16

    Re: macro import file hex in excel

    tanks but when play macro :

    index out of range

  16. #16
    Forum Guru
    Join Date
    03-02-2006
    Location
    Los Angeles, Ca
    MS-Off Ver
    WinXP/MSO2007;Win10/MSO2016
    Posts
    12,943

    Re: macro import file hex in excel

    When I was writing/testing the macro, I used a file from my own computer and entered the output to a worksheet I named BEN. The mod below will write the output to what ever the active sheet is. But you must still tell the macro where the binary file is located >> change the BLUE portion of the code below.
    Option Explicit
    Sub BinToHex()
        Dim ColumnNumber    As Long, _
            RowNumber       As Long, _
            Ndx             As Long, _
            InBucket        As Variant
         
        Open "c:\downloads\encoding.bin" For Binary As #1
        InBucket = Input(LOF(1), #1)
        Close #1
        RowNumber = 1
        ColumnNumber = 0
    
        For Ndx = 1 To Len(InBucket)
            ColumnNumber = ColumnNumber + 1
            Cells(RowNumber, ColumnNumber) = Hex(Asc(Mid(InBucket, Ndx, 1)))
            If (Ndx Mod 16) = 0 Then
                RowNumber = RowNumber + 1
                ColumnNumber = 0
            End If
        Next Ndx
      End Sub
    1. What is the path and name of your file ?
    2. Where did you copy the code (did you put it in a worksheet module)?

  17. #17
    Registered User
    Join Date
    01-19-2012
    Location
    italy
    MS-Off Ver
    Excel 2003
    Posts
    16

    Re: macro import file hex in excel

    Quote Originally Posted by protonLeah View Post
    When I was writing/testing the macro, I used a file from my own computer and entered the output to a worksheet I named BEN. The mod below will write the output to what ever the active sheet is. But you must still tell the macro where the binary file is located >> change the BLUE portion of the code below.
    Option Explicit
    Sub BinToHex()
        Dim ColumnNumber    As Long, _
            RowNumber       As Long, _
            Ndx             As Long, _
            InBucket        As Variant
         
        Open "c:\downloads\encoding.bin" For Binary As #1
        InBucket = Input(LOF(1), #1)
        Close #1
        RowNumber = 1
        ColumnNumber = 0
    
        For Ndx = 1 To Len(InBucket)
            ColumnNumber = ColumnNumber + 1
            Cells(RowNumber, ColumnNumber) = Hex(Asc(Mid(InBucket, Ndx, 1)))
            If (Ndx Mod 16) = 0 Then
                RowNumber = RowNumber + 1
                ColumnNumber = 0
            End If
        Next Ndx
      End Sub
    1. What is the path and name of your file ?
    2. Where did you copy the code (did you put it in a worksheet module)?
    TANKS, works perfectly ....... but I still need you !!!!!!
    I'm sorry .... Once I changed the file in excel how do I save it in. BIN?? I need to calculate a few bytes to join 4-cell, and I do it with the following function: "= A1&A2&A3&A4 "..... but then how do I rewrite the result obtained on 4 separate cells?hopefully last question: can you give me create a macro that I want to ask me to upload files without having to change each time the macro??
    tanks
    tanks

  18. #18
    Registered User
    Join Date
    01-19-2012
    Location
    italy
    MS-Off Ver
    Excel 2003
    Posts
    16

    Re: macro import file hex in excel

    Quote Originally Posted by tatop View Post
    TANKS, works perfectly ....... but I still need you !!!!!!
    I'm sorry .... Once I changed the file in excel how do I save it in. BIN?? I need to calculate a few bytes to join 4-cell, and I do it with the following function: "= A1&A2&A3&A4 "..... but then how do I rewrite the result obtained on 4 separate cells?hopefully last question: can you give me create a macro that I want to ask me to upload files without having to change each time the macro??
    tanks
    tanks
    UP !!!!!! tankssss

  19. #19
    Forum Contributor
    Join Date
    12-28-2011
    Location
    England
    MS-Off Ver
    Excel 2007
    Posts
    280

    Re: macro import file hex in excel

    Does the EEPROM use Big- or Little-endian data? That may well determine how you want the data extracted and written to the file.

  20. #20
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: macro import file hex in excel

    Please take a few minutes to read the forum rules, and then edit your post to add CODE tags.

    Thanks.
    Entia non sunt multiplicanda sine necessitate

  21. #21
    Forum Guru
    Join Date
    03-02-2006
    Location
    Los Angeles, Ca
    MS-Off Ver
    WinXP/MSO2007;Win10/MSO2016
    Posts
    12,943

    Re: macro import file hex in excel

    tatop seems to be done but I thought I would post the update anyway:
    Option Explicit
    Option Base 1
    Sub BinToHex()
        Dim ColumnNumber    As Long, _
            RowNumber       As Long, _
            Ndx             As Long, _
            handleNumber    As Long, _
            InBucket        As Variant, _
            FileToOpen      As String
        
        'get the name of the binary file to open
        FileToOpen = Application.GetOpenFilename("Binary files (*.bin),*.bin")
        
        ' get the next handle number from windows
        handleNumber = FreeFile
        
        'open the file, read into a string
        Open FileToOpen For Binary As handleNumber
            InBucket = Input(LOF(handleNumber), handleNumber)
        Close handleNumber
        
        RowNumber = 1
        ColumnNumber = 0
    
        ' for each byte of the string, convert to decimal integer value 0 - 256,
        ' convert the integer to hex
        
        For Ndx = 1 To Len(InBucket)
            ColumnNumber = ColumnNumber + 1
            Cells(RowNumber, ColumnNumber) = Hex(Asc(Mid(InBucket, Ndx, 1)))
            If (Ndx Mod 16) = 0 Then
                RowNumber = RowNumber + 1
                ColumnNumber = 0
            End If
        Next Ndx
      End Sub
    Sub SaveAsBinary()
        Dim LastRow     As Long, _
            RowNdx      As Long, _
            ColNdx      As Long, _
            FileNum     As Long, _
            t0          As Double, _
            FName
            
        FName = Application.GetSaveAsFilename
        t0 = Timer
    
        LastRow = Cells(Rows.Count, "R").End(xlUp).Row
        ReDim outarray(1 To LastRow, 1 To 16)
        
        For RowNdx = 1 To LastRow
            For ColNdx = 1 To 16
                ' convert the cell hex back to decimal and replace with the character string
                outarray(RowNdx, ColNdx) = Chr(WorksheetFunction.Hex2Dec(Cells(RowNdx, ColNdx).Value))
            Next ColNdx
        Next RowNdx
        
        'open the file and save the array
        FileNum = FreeFile
        Open FName For Binary As FileNum
        Put FileNum, , outarray
        Close FileNum
        Debug.Print "elapsed time ", Timer - t0
    End Sub

  22. #22
    Registered User
    Join Date
    01-19-2012
    Location
    italy
    MS-Off Ver
    Excel 2003
    Posts
    16

    Re: macro import file hex in excel

    Quote Originally Posted by protonLeah View Post
    tatop seems to be done but I thought I would post the update anyway:
    Option Explicit
    Option Base 1
    Sub BinToHex()
        Dim ColumnNumber    As Long, _
            RowNumber       As Long, _
            Ndx             As Long, _
            handleNumber    As Long, _
            InBucket        As Variant, _
            FileToOpen      As String
        
        'get the name of the binary file to open
        FileToOpen = Application.GetOpenFilename("Binary files (*.bin),*.bin")
        
        ' get the next handle number from windows
        handleNumber = FreeFile
        
        'open the file, read into a string
        Open FileToOpen For Binary As handleNumber
            InBucket = Input(LOF(handleNumber), handleNumber)
        Close handleNumber
        
        RowNumber = 1
        ColumnNumber = 0
    
        ' for each byte of the string, convert to decimal integer value 0 - 256,
        ' convert the integer to hex
        
        For Ndx = 1 To Len(InBucket)
            ColumnNumber = ColumnNumber + 1
            Cells(RowNumber, ColumnNumber) = Hex(Asc(Mid(InBucket, Ndx, 1)))
            If (Ndx Mod 16) = 0 Then
                RowNumber = RowNumber + 1
                ColumnNumber = 0
            End If
        Next Ndx
      End Sub
    Sub SaveAsBinary()
        Dim LastRow     As Long, _
            RowNdx      As Long, _
            ColNdx      As Long, _
            FileNum     As Long, _
            t0          As Double, _
            FName
            
        FName = Application.GetSaveAsFilename
        t0 = Timer
    
        LastRow = Cells(Rows.Count, "R").End(xlUp).Row
        ReDim outarray(1 To LastRow, 1 To 16)
        
        For RowNdx = 1 To LastRow
            For ColNdx = 1 To 16
                ' convert the cell hex back to decimal and replace with the character string
                outarray(RowNdx, ColNdx) = Chr(WorksheetFunction.Hex2Dec(Cells(RowNdx, ColNdx).Value))
            Next ColNdx
        Next RowNdx
        
        'open the file and save the array
        FileNum = FreeFile
        Open FName For Binary As FileNum
        Put FileNum, , outarray
        Close FileNum
        Debug.Print "elapsed time ", Timer - t0
    End Sub
    tanks , the first macro is OK...loaf file OK
    but the macro SaveAsBinary dont go!!!
    show this error:
    error of runtime "438"
    Property or method not supported

    can u correct , please

    thank again !!!!!!

  23. #23
    Registered User
    Join Date
    01-19-2012
    Location
    italy
    MS-Off Ver
    Excel 2003
    Posts
    16

    Re: macro import file hex in excel

    up ! ! ! ! !

  24. #24
    Forum Guru
    Join Date
    03-02-2006
    Location
    Los Angeles, Ca
    MS-Off Ver
    WinXP/MSO2007;Win10/MSO2016
    Posts
    12,943

    Re: macro import file hex in excel

    Sorry, the save macro runs on my machine. I'm not familiar with runtime error 438. Can you step through the "save" macro to see which line is highlighted when it stops?

  25. #25
    Registered User
    Join Date
    01-19-2012
    Location
    italy
    MS-Off Ver
    Excel 2003
    Posts
    16

    Re: macro import file hex in excel

    error 438.jpg

    error of runtime "438"
    Property or method not supported

  26. #26
    Registered User
    Join Date
    01-19-2012
    Location
    italy
    MS-Off Ver
    Excel 2003
    Posts
    16

    Re: macro import file hex in excel

    Attachment 137838

    error of runtime "438"
    Property or method not supported

  27. #27
    Registered User
    Join Date
    01-19-2012
    Location
    italy
    MS-Off Ver
    Excel 2003
    Posts
    16

    Re: macro import file hex in excel

    Quote Originally Posted by protonLeah View Post
    Sorry, the save macro runs on my machine. I'm not familiar with runtime error 438. Can you step through the "save" macro to see which line is highlighted when it stops?

    I'm sorry, the macro in Excel 2007 works, the problem was that I was using Excel 2003. I have it converted and it works .... only problem is that, hex conversion is incorrect, because when I save the bin file I see only data wrong.
    my new code is:

    Sub SaveAsBinary()
        Dim LastRow     As Long, _
            RowNdx      As Long, _
            ColNdx      As Long, _
            FileNum     As Long, _
            t0          As Double, _
            FName
            
        FName = Application.GetSaveAsFilename
        t0 = Timer
    
        LastRow = Cells(Rows.Count, "R").End(xlUp).Row
        ReDim outarray(1 To LastRow, 1 To 16)
        
        For RowNdx = 1 To LastRow
            For ColNdx = 1 To 16
                ' convert the cell hex back to decimal and replace with the character string
                outarray(RowNdx, ColNdx) = Evaluate("Chr(Clng(RowNdx, ColNdx).Value)))")
            Next ColNdx
        Next RowNdx
        
        'open the file and save the array
        FileNum = FreeFile
        Open FName For Binary As FileNum
        Put FileNum, , outarray
        Close FileNum
        Debug.Print "elapsed time ", Timer - t0
    End Sub
    Attached Images Attached Images

  28. #28
    Forum Guru
    Join Date
    03-02-2006
    Location
    Los Angeles, Ca
    MS-Off Ver
    WinXP/MSO2007;Win10/MSO2016
    Posts
    12,943

    Re: macro import file hex in excel

    O.k. the error 438 is because you don't seem to have the Analysis ToolPak installed on your machine:

    HEX2DEC

    If this function is not available, and returns the #NAME? error, install and load the Analysis ToolPak add-in.

    On the Tools menu, click Add-Ins.
    In the Add-Ins available list, select the Analysis ToolPak box, and then click OK.
    If necessary, follow the instructions in the setup program.
    2. In the line:

    outarray(RowNdx, ColNdx) = Evaluate("Chr(Clng(RowNdx, ColNdx).Value)))")
    you are trying to use the CLng function to convert a value on the worksheet to a long integer. However, you have made a couple of mistakes.

    The Clng function works with decimal values such as 1202.667 converting it to 1203. In your spreadsheet you have strings representing hex numbers such as "FE" in a cell, say D4.

    Since F & E are not decimal digits, were you to write the function correctly you would write Clng(Range("D4").Value) or Clng(Cells(RowNdx,ColNdx).Value) you would get an error.

    But, you wrote the Clng function wrong, so it returns an error value to the Chr() function and it in turn errors since there is no character mapped to those error values.
    The result is that the line simply loads the output array with error values.
    -------------------------
    Also, the lines:
    t0 = Timer, and
    Debug.Print "elapsed time ", Timer - t0 should be deleted, that was just an experiment for me while making changes.
    Last edited by protonLeah; 01-25-2012 at 12:09 AM.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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