尝试
Sub Sample()
Dim sVal As String
sVal = Application.Evaluate("=DEC2BIN(7, 4)")
Debug.Print sVal
End Sub
或者,如果你不想硬编码Places
然后用这个
Sub Sample()
Dim sVal As String
Dim nPlace as Long
nPlace = 4
sVal = Application.Evaluate("=DEC2BIN(7," & nPlace & ")")
Debug.Print sVal
End Sub
这会给你0111
,而不是111
编辑
如果你想将它输出到Excel单元格,然后你有两个选择
A)格式的单元格为文字,然后用
Sub Sample()
Dim sVal As String
Dim nPlace As Long
'~~> Number of places
nPlace = 4
sVal = Application.Evaluate("=DEC2BIN(7," & nPlace & ")")
ThisWorkbook.Sheets("Sheet1").Range("A1").Value = sVal
End Sub
另外,您可以使用.Numberformat
通过格式化单元格码。
OR
B)使用'
在Excel
Sub Sample()
Dim sVal As String
Dim nPlace As Long
'~~> Number of places
nPlace = 4
sVal = Application.Evaluate("=DEC2BIN(7," & nPlace & ")")
ThisWorkbook.Sheets("Sheet1").Range("A2").Value = "'" & sVal
End Sub
你现在正在做输入数据之前(显示 – SheetJS