我正在尝试为每个学生ID为262015的学生创建一个新的学生ID。他们的学生ID在L列。新的学生证必须符合以下要求:
这是我到目前为止的代码,我只需要在我的代码中满足所有这些要求。希望有人可以提供帮助:)
Sub newstudentid()
Dim rng As Range, cell As Range
Set rng = Range("L2:L18288")
For Each cell In Range
If Left(cell.Value, 6) = "262015" Then
最后,我需要为所有受影响的学生制作一个名为AdminExport.csv的CSV文件。 CSV文件必须包含PERSON_ID,它位于A列,旧的student_ID和新的,以及Enroll_period,位于E列。
答案 0 :(得分:0)
你能试试这段代码吗? 评论在代码中。
Sub newstudentid()
Dim rng As Range, cell As Range
Set rng = Range("L2:L18288")
For Each cell In rng
If Left(cell.Value, 6) = "262015" Then
' start with 18 and faculty ID
Start = "18"
Faculty = cell.Offset(0, -3).Value
'get a random number and format it to five digits
random = Format(Application.RandBetween(0, 99999), "00000")
'While the number exist in range
' CLng(Start & random & Facility) asumes the IDs are formated as numbers and not as strings
While (Not IsError(Application.Match(CLng(Start & random), rng.Value, 0)))
'create a new random number and format to five digits
random = Format(Application.RandBetween(0, 99999), "00000")
Wend
'paste the new ID at cell.value
cell.Value = Start & random & Faculty
End If
Next cell
End Sub