我正在尝试生成唯一的随机序列号,并将其插入到“A”列的每个单元格中,前提是我在“E”列的相应单元格中有一个值,我也使用“E”列的第一个字母在完成的序列号中。. 但是我得到重复的值,例如 SYJ3068 SYJ3068 SNF9678 SNF9678 SNF9678 SGZ5605 SGZ5605 SGZ5605
我已经寻找解决方案但没有成功,请您指出正确的方向,并帮助我修复我的代码,以便每个单元格都获得唯一的序列号。由于我对 VBA 的了解非常有限,我设法想出了这个:
Sub SumIt()
Dim rRandom_Number As Long
Dim rRandom_1st_Letter As String
Dim rRandom_2nd_Letter As String
Dim rRandom_Serial As String
Dim CellValue As String
Dim rCell_New_Value As String
Dim RowCrnt As Integer
Dim RowMax As Integer
Dim rCell As Range
With Sheets("Sheet1")
RowMax = .Cells(Rows.Count, "E").End(xlUp).Row
For RowCrnt = 6 To RowMax
CellValue = .Cells(RowCrnt, 5).Value
If Left(CellValue, 1) <> "" Then
For Each rCell In Range("A6:A" & RowMax)
Rnd -1
Randomize (Timer)
rRandom_Number = Int((9999 + 1 - 1000) * Rnd() + 1000)
rRandom_1st_Letter = Chr(CInt(Int((90 - 65 + 1) * Rnd() + 65)))
rRandom_2nd_Letter = Chr(CInt(Int((90 - 65 + 1) * Rnd() + 65)))
rRandom_Serial = _
rRandom_1st_Letter _
& rRandom_2nd_Letter _
& rRandom_Number
rCell_New_Value = UCase(Left(Trim(CellValue), 1) & rRandom_Serial)
.Cells(RowCrnt, 1).Value = rCell_New_Value
Next
End If
Next
End With
End Sub
非常感谢您的帮助。