在excel中创建字母数字序列

时间:2013-06-18 15:15:01

标签: excel excel-vba excel-formula excel-2010 vba

通过使用excel,我需要创建一个由0000到ZZZZ的4个字母/数字的字母数字序列。 我创建了一个像下面那样的小宏:

Sub a()
Dim iChar1 As Integer, iChar2 As Integer, iChar3 As Integer, iChar4 As Integer
Dim sOutput As String
For iChar1 = 65 To 90
For iChar2 = 65 To 90
For iChar3 = 65 To 90
For iChar4 = 65 To 90
sOutput = Chr(iChar1) & Chr(iChar2) & Chr(iChar3) & iChar4

ActiveCell.Value = sOutput
ActiveCell.Offset(1, 0).Activate
Next: Next: Next: Next

End Sub

问题在于,通过这种方式,我只获得了从65到90的ASCII caracheters的alpha序列。相反,我还需要数值,(ASCII从48到57)。 怎么做?或者,你知道另一种方式吗?

1 个答案:

答案 0 :(得分:2)

也许是这样,使用输入数组和输出数组而不是逐个单元地写

Sub a()
   Dim vChars
   Dim iChar1 As Integer, iChar2 As Integer, iChar3 As Integer, iChar4 As Integer
   Dim sOut(1 To 839808, 1 To 2) As String
   Dim lCounter As Long
   Dim lCol As Long
   vChars = Array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", _
                           "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z")
   lCounter = 1
   lCol = 1
   For iChar1 = 0 To 35
      For iChar2 = 0 To 35
         For iChar3 = 0 To 35
            For iChar4 = 0 To 35
               sOut(lCounter, lCol) = vChars(iChar1) & vChars(iChar2) & vChars(iChar3) & vChars(iChar4)
               If lCounter = 839808 Then
                  lCol = 2
                  lCounter = 1
               Else
                  lCounter = lCounter + 1
               End If

            Next iChar4
         Next iChar3
      Next iChar2
   Next iChar1
Range("A1").Resize(839808, 2).Value = sOut
End Sub