0

通过使用 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

问题是,由于 ASCII 字符从 65 到 90,因此我只能获得 alpha 序列。相反,我还需要数值(从 48 到 57 的 ASCII)。怎么做?或者,你知道另一种方法吗?

4

1 回答 1

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
于 2013-06-18T15:32:16.563 回答