VBA Excel:Dec2Hex不止一个地方

在一个macros(如下所示)中,我将一行单元格从十进制转换为hex。 当我使用正常的Dec2Hex转换(不是在VBA),我能够得到两个地方(8 – > 08)。 但是,当我使用Dec2Hex转换IN VBA时,我似乎无法获得2个地方,直到我真的需要字符(8 – > 8但是10 – > 0A)。 我怎么能强制最后一个字符,而不只是添加一个0.谢谢!

跟进

我注意到一个有趣的事情是,如果你看看macros,我在每个hex值之后添加一个空格。 但是,在查看生成的文件时,只有在存在两个字符时才会添加空格。

样品

F3 07 00 31 FA 10 //

F3 07 00 31 FB 20 //

F3 07 00 31 FC 00 //

F3 25 00 31 FF 830B 5114 // < – 注意奇数间距。 它应该看起来像… 08 03 0B …

F3 07 00 31 FA 11 //

Private Sub CommandButton3_Click() Dim fso As New FileSystemObject Dim stream As TextStream Dim FilePath As String Dim saveDialog As Variant Dim hexVal As String Dim updateRate As String '"N" is selected elsewhere by the user and simply dictates how far down a row cells are active For i = 4 To N + 3 Cells(3, i) = Application.WorksheetFunction.Dec2Hex(Cells(2, i), 2) & " " Next i For i = 4 To N + 3 hexVal = hexVal & Cells(3, i) Next i updateRate = ComboBox1.Value saveDialog = Application.GetSaveAsFilename(FileFilter:="Script Files(*.us1),*.us1") Set stream = fso.OpenTextFile(saveDialog, ForWriting, True) stream.WriteLine "F3 07 00 31 FA 10 // " stream.WriteLine "F3 07 00 31 FB " + updateRate + " // " stream.WriteLine "F3 07 00 31 FC 00 // " stream.WriteLine "F3 25 00 31 FF " + hexVal + "// " stream.WriteLine "F3 07 00 31 FA 11 // " stream.Close End Sub 

我认为的问题是,单元格3,我是数字或一般。 这些都会截断前导零。 但是,如果将单元格格式更改为TEXT,则在写入文件之前,前导零不会被截断。