字符串的标准模式类似于16-000q。使用前导零从q创建4位数字符串

时间:2017-01-16 11:43:18

标签: vba excel-vba excel

目前,我正在使用

Cells(i, 6) & ("-000") & (q) 

生成我的代码。但我意识到,当q成为2位数的int(即11)时,输出就像16-00011,我不想要。我希望输出像16-0011

1 个答案:

答案 0 :(得分:0)

使用Format

尝试此操作
Dim myOutput as String
myOutput = Cells(i, 6) & "-" & Format(q, "0000")

' If Cells(i, 6) = 16, and q = 11 then
' >> myOutput = "16-0011"

' If Cells(i, 6) = 16, and q = 9 then
' >> myOutput = "16-0009"

Format的文档: https://msdn.microsoft.com/en-us/library/office/gg251755.aspx