Skip to content

Instantly share code, notes, and snippets.

@tsunakan
Created July 4, 2016 00:20
Show Gist options
  • Save tsunakan/e72020a354247348419539552baa4a91 to your computer and use it in GitHub Desktop.
Save tsunakan/e72020a354247348419539552baa4a91 to your computer and use it in GitHub Desktop.
バイナリファイルをバイナリエディタのように表示
Option Explicit
Sub ReadBinary2()
Dim myByte As Byte
Dim myRange As Range
Dim myFileName As String
Dim r As Long
Dim c As Long
'セルのエラーを無視、セルの初期化
Application.ErrorCheckingOptions.BackgroundChecking = False
Set myRange = Range("A2").End(xlDown)
Range(Cells(2, 1), Cells(myRange.Row, 34)).Clear
'ファイルを開く
ChDir ThisWorkbook.Path & "\"
myFileName = Application.GetOpenFilename()
Open myFileName For Binary As #1
Cells(2, 1).Value = "'000000"
Cells(2, 1).Interior.Color = RGB(192, 192, 192)
r = 2
c = 2
'バイナリファイルを読み込みつつセルに書き込み
Do While Not EOF(1)
Get #1, , myByte
Cells(r, c).Value = "'" & CStr(Application.WorksheetFunction.Dec2Hex(myByte, 2))
Cells(r, c).HorizontalAlignment = xlCenter
c = c + 1
If c >= 18 Then
c = 2
r = r + 1
'左端の番地名
Cells(r, 1).Value = "'" & CStr(Application.WorksheetFunction.Dec2Hex((r - 2) * 16, 6))
Cells(r, 1).Interior.Color = RGB(192, 192, 192)
End If
Loop
Close #1
c = c - 1
If c = 1 Then
c = 17
r = r - 1
End If
'文字コードに対応する文字を表示
Range(Cells(2, 19), Cells(r, 34)).Value = "=CHAR(HEX2DEC(B2))"
If c <> 17 Then
Range(Cells(r, c + 18), Cells(r, 34)).Clear
End If
End Sub
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment