标签:VBA
在网上看到的一段程序,不知道会在哪里可以用到。然而,不妨碍将其辑录于此,研究其程序代码。
这段程序以二进制形式列出了单元格的文本(前200个字符)。所作用的单元格是所选择的单元格,或者是所选单元格区域左上角的单元格。
VBA程序代码如下:
Sub ShowBinary()
Const sTitle As String = "单元格文本的二进制列表: "
Dim sInp As String
Dim sOut As String
Dim i As Long
Dim sAdr As String
If TypeName(Selection) <> "Range" Then
MsgBox "不适用于单元格区域以外的选择!", _
vbInformation, sTitle & TypeName(Selection)
Exit Sub '---------------------------------------------------------->
End If
sInp = Selection(1, 1).Text
sAdr = Selection(1, 1).Address(False, False)
If Len(sInp) = 0 Then
MsgBox "单元格文本为空", vbInformation, sTitle & sAdr
Exit Sub '---------------------------------------------------------->
End If
For i = 1 To Len(sInp)
If i Mod 10 = 1 Then
If i = 201 Then
sOut = sOut & vbLf & "..."
Exit For
Else
sOut = sOut & vbLf & Format(i, "000: ")
End If
End If
sOut = sOut & Format(Asc(Mid(sInp, i, 1)), " 000")
If i Mod 5 = 0 Then sOut = sOut & " "
Next i
MsgBox Mid(sOut, 2), vbInformation, sTitle & sAdr
End Sub