目前,我设法开发了一种字母数字的ID。但是我尝试运行了几次,发现有时数值只会显示4位数字(假设没有输入0),所以我缺少一个代码来帮助我将其设置为5位数字,任何人都不会给我那段黄金代码?谢谢

这是我当前的代码

Private Sub Workbook_Open() ' Called every time you open the Excel document
    Dim alpha As String
    Dim numeric As Long
    Dim alphanumeric As String
    Randomize
    numeric = CLng(Rnd() * 99999)   ' CLng converts the floating point number to a long integer
    alpha = "SCC"
    alphanumeric = alpha & numeric

    Dim rowNum As Integer
    Dim colNum As Integer
    rowNum = 6
    colNum = 4

    With ThisWorkbook.Sheets("Sheet1").Cells(rowNum, colNum)
        If (.Value = "") Then
            .Value = alphanumeric
        End If
    End With
End Sub

最佳答案

更改此行代码

alphanumeric = alpha & numeric


进入

alphanumeric = alpha & Format(numeric,"0000") '<~or into any number of zeros you prefer

关于excel - 添加前导“0”以格式化直到5位数字值,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/44063072/

10-13 07:39