使用Excel vbamacros在序列号中生成密钥

我想知道是否有可能使用Excel VBAmacros生成一个序列号。

我有一个文本文件,其中包含这样一行:

00|?????|AB_20050106_01||||||||||||||||||||||||||| 

是否可以使用Excel VBAmacros来生成序列中的下一个项目,例如:

  • AB_20050106_01
  • AB_20050106_02
  • AB_20050106_03

每当我在Excel中按下button?

最终,我想保存一个新的文件名称将包括此密钥,我不想覆盖文件。

有几个选项可以跟踪序列,但是一种方法是将私有variables保存在可以在其他函数中引用的模块中。 这将适用于索引01-99

 Option Explicit Const COUNTER_PREFIX As String = "AB_" Private m_lngCounter As Long Sub Test() Dim i As Integer For i = 1 To 100 Debug.Print GetNextCounterKey(Format(Now, "yyyyMMdd")) Next End Sub Function GetNextCounterKey(strItem As String) As String m_lngCounter = m_lngCounter + 1 GetNextCounterKey = COUNTER_PREFIX & strItem & "_" & Format(m_lngCounter, "00") End Function 

这是一个关于如何去做的想法。 你需要编辑这个,以适应你的需要,但是,应该做的伎俩。 把你的标题分解成单元格; A1B1C1 。 IE A1 = AA, B1 = 22222222, C1 = 25 ,并将macros分配给一个被调用的button。

 Sub testing1() Dim Pt1 As String, Pt2 As Long, Pt3 As Long, FinalString As String 'Get ranges from excel Pt1 = Range("A1").Value Pt2 = Range("B1").Value Pt3 = Range("C1").Value Pt3 = Pt3 + 1 'Increment pt3 If Pt3 = 100 Then Pt3 = 0 Pt2 = Pt2 + 1 'Increment pt2/pt1 If Pt2 = 100000000 Then Pt2 = 0 Select Case Len(Pt1) Case 1 'char is one letter If UCase(Pt1) = "Z" Then Pt1 = "AA" Else Pt1 = Chr(Asc(Pt1) + 1) End If Case 2 'char is two letters If Right(Pt1, 1) = "Z" Then Pt1 = Chr(Asc(Left(Pt1, 1)) + 1) & "A" Else Pt1 = Left(Pt1, 1) & Chr(Asc(Right(Pt1, 1)) + 1) End If End Select End If End If Pt2s = Format(Pt2, "00000000") 'Make 8 digits Pt3s = Format(Pt3, "00") 'Make 2 digits FinalString = Pt1 + "_" + Pt2s + "_" + Pt3s 'Left these in here so you can see what is going on. MsgBox Pt1 MsgBox Pt2 MsgBox Pt3 MsgBox FinalString 'Set current vals to cells. Range("A1").Value = Pt1 Range("B1").Value = Pt2s Range("C1").Value = Pt3s 'Create File Set fs = CreateObject("Scripting.FileSystemObject") Set a = fs.CreateTextFile("c:\" + FinalString + ".txt", True) a.WriteLine ("Here is your first line.") a.Close End Sub