Excel VBA唯一ID生成器

Excel VBA唯一ID生成器,excel,vba,Excel,Vba,我正在尝试为讨论的主题生成唯一ID。数据如下所示: Status ID Topic Commentary Open FIL-1 FILM Open FIL-2 FILM Closed LAN-1 LANG. Open LAN-2 LANG. 其想法是,当在新行上时,无论它是添加在最后一个唯一ID的上方还是下方,我都使用VBA来查找下一个ID。例如,上面如果我在顶部添加另一行,主题为LANG

我正在尝试为讨论的主题生成唯一ID。数据如下所示:

Status    ID        Topic    Commentary
Open      FIL-1     FILM      
Open      FIL-2     FILM
Closed    LAN-1     LANG.
Open      LAN-2     LANG.
其想法是,当在新行上时,无论它是添加在最后一个唯一ID的上方还是下方,我都使用VBA来查找下一个ID。例如,上面如果我在顶部添加另一行,主题为LANG。那么它将发现LAN-2是最新的ID,并且+1将成为LAN-3

当主题与下面的代码完全相同时,我实现了这一点(主题都是“FIL”,但现在有多个主题):

私有函数getNextID()作为字符串
将行设置为整数
Dim currentID为整数
当前ID=0
“绕成一排
行=最小行到最大行
'仅使用非空的行
如果是工作表(讨论).cells(行,ID).Value“”,则
如果Mid$(工作表(讨论).cells(行,ID).Value,InStr(3,工作表(讨论).cells(行,ID).Value),“-”+1)>当前ID,则
currentID=Mid$(工作表(讨论).cells(行,ID).Value,InStr(3,工作表(讨论).cells(行,ID).Value),“-”+1)
如果结束
如果结束
下一排
getNextID=“FIL”&“-”和currentID+1
端函数

有人知道我如何使用ID中使用的主题缩写设置数组,并使用我已经编写的代码,使用数组中的缩写循环完成相同的过程,以获取要添加的特定主题的下一个ID吗

除了出于某种原因的第一个条目(Evaluate Formula按钮显示它正在工作,但就在最后,它将值替换为0)之外,这段代码完成了这项工作

因此,手动添加第一个ID,然后将代码从第3行运行到列表的最后一行(您还需要添加代码以忽略空行)


除了出于某种原因的第一个条目(Evaluate Formula按钮显示它正在工作,但在末尾它将值替换为0)之外,这段代码完成了这项工作

因此,手动添加第一个ID,然后将代码从第3行运行到列表的最后一行(您还需要添加代码以忽略空行)


我对代码进行了调整,使其包含所需的数组,这确实意味着您必须将您请求ID的主题的名称传递到您的过程中,如果需要,这可以自动执行,但很难知道您项目的总体情况,因此我将其保留为:-

Private Function getNextID(ByVal StrTopic As String) As String
Static AryTopics(2, 1)     As String
Dim row                     As Integer
Dim currentID               As Integer
Dim LngCounter              As Long
currentID = 0

'By having the array declared static and being a fixed size, it will only get built once
'then rememebered
If AryTopics(0, 0) = "" Then
    AryTopics(0, 0) = "FILM"
    AryTopics(0, 1) = "FIL"
    AryTopics(1, 0) = "LANG."
    AryTopics(1, 1) = "LAN"
    AryTopics(2, 0) = "GEOG."
    AryTopics(2, 1) = "GEO"
End If

'The topic must be passed into the proce to know what to get the ID for
'This gets the related topic code from the array
For LngCounter = 0 To UBound(AryTopics, 1)
    If AryTopics(LngCounter, 0) = Trim(UCase(StrTopic)) Then
        StrTopic = AryTopics(LngCounter, 1)
        Exit For
    End If
Next

' Loop round rows
For row = MIN_ROW To MAX_ROW

    ' Only use rows which are not blank
    If Worksheets(DISCUSS).Cells(row, ID).Value <> "" Then

        'This checks to see if the ID starts with the related topic code we care about, if it does then we keep checking
        If Left(Trim(UCase(Worksheets(DISCUSS).Cells(row, ID).Value)), Len(StrTopic) + 1) = StrTopic & "-" Then

            If Mid$(Worksheets(DISCUSS).Cells(row, ID).Value, InStr(3, Worksheets(DISCUSS).Cells(row, ID).Value, "-") + 1) > currentID Then
                currentID = Mid$(Worksheets(DISCUSS).Cells(row, ID).Value, InStr(3, Worksheets(DISCUSS).Cells(row, ID).Value, "-") + 1)
            End If

        End If
    End If

Next row

'Output include the topic code
getNextRiskID = StrTopic & "-" & currentID + 1

End Function
私有函数getNextID(ByVal StrTopic作为字符串)作为字符串
静态主题(2,1)作为字符串
将行设置为整数
Dim currentID为整数
暗LngCounter尽可能长
当前ID=0
'通过将数组声明为静态且大小固定,它将只生成一次
“那么我记得了
如果(0,0)=“”,那么
主题(0,0)=“电影”
(0,1)=“FIL”
(1,0)=“郎”
(1,1)=“局域网”
(2,0)=“GEOG”
主题(2,1)=“地理”
如果结束
'必须将主题传递到进程中,才能知道获取ID的目的
'这将从数组中获取相关主题代码
对于LngCounter=0到UBound(1)
如果AryTopics(LngCounter,0)=Trim(UCase(StrTopic)),则
StrTopic=AryTopics(LngCounter,1)
退出
如果结束
下一个
“绕成一排
行=最小行到最大行
'仅使用非空的行
如果是工作表(讨论).Cells(行,ID).Value“”,则
'这将检查ID是否以我们关心的相关主题代码开头,如果以相关主题代码开头,我们将继续检查
如果左(Trim(UCase(工作表(讨论).Cells(行,ID.Value)),Len(StrTopic)+1)=StrTopic&“-”
如果Mid$(工作表(讨论).Cells(行,ID).Value,InStr(3,工作表(讨论).Cells(行,ID).Value),“-”+1)>当前ID,则
currentID=Mid$(工作表(讨论).Cells(行,ID).Value,InStr(3,工作表(讨论).Cells(行,ID).Value),“-”+1)
如果结束
如果结束
如果结束
下一排
'输出包括主题代码
getNextRiskID=StrTopic&“-”¤tID+1
端函数

我对代码进行了调整,您必须包含所需的数组,这确实意味着您必须将请求ID的主题的名称传递到您的过程中,如果需要,这可以自动执行,但很难知道您的项目的总体情况,因此我将其保留为:-

Private Function getNextID(ByVal StrTopic As String) As String
Static AryTopics(2, 1)     As String
Dim row                     As Integer
Dim currentID               As Integer
Dim LngCounter              As Long
currentID = 0

'By having the array declared static and being a fixed size, it will only get built once
'then rememebered
If AryTopics(0, 0) = "" Then
    AryTopics(0, 0) = "FILM"
    AryTopics(0, 1) = "FIL"
    AryTopics(1, 0) = "LANG."
    AryTopics(1, 1) = "LAN"
    AryTopics(2, 0) = "GEOG."
    AryTopics(2, 1) = "GEO"
End If

'The topic must be passed into the proce to know what to get the ID for
'This gets the related topic code from the array
For LngCounter = 0 To UBound(AryTopics, 1)
    If AryTopics(LngCounter, 0) = Trim(UCase(StrTopic)) Then
        StrTopic = AryTopics(LngCounter, 1)
        Exit For
    End If
Next

' Loop round rows
For row = MIN_ROW To MAX_ROW

    ' Only use rows which are not blank
    If Worksheets(DISCUSS).Cells(row, ID).Value <> "" Then

        'This checks to see if the ID starts with the related topic code we care about, if it does then we keep checking
        If Left(Trim(UCase(Worksheets(DISCUSS).Cells(row, ID).Value)), Len(StrTopic) + 1) = StrTopic & "-" Then

            If Mid$(Worksheets(DISCUSS).Cells(row, ID).Value, InStr(3, Worksheets(DISCUSS).Cells(row, ID).Value, "-") + 1) > currentID Then
                currentID = Mid$(Worksheets(DISCUSS).Cells(row, ID).Value, InStr(3, Worksheets(DISCUSS).Cells(row, ID).Value, "-") + 1)
            End If

        End If
    End If

Next row

'Output include the topic code
getNextRiskID = StrTopic & "-" & currentID + 1

End Function
私有函数getNextID(ByVal StrTopic作为字符串)作为字符串
静态主题(2,1)作为字符串
将行设置为整数
Dim currentID为整数
暗LngCounter尽可能长
当前ID=0
'通过将数组声明为静态且大小固定,它将只生成一次
“那么我记得了
如果(0,0)=“”,那么
主题(0,0)=“电影”
(0,1)=“FIL”
(1,0)=“郎”
(1,1)=“局域网”
(2,0)=“GEOG”
主题(2,1)=“地理”
如果结束
'必须将主题传递到进程中,才能知道获取ID的目的
'这将从数组中获取相关主题代码
对于LngCounter=0到UBound(1)
如果AryTopics(LngCounter,0)=Trim(UCase(StrTopic)),则
StrTopic=AryTopics(LngCounter,1)
退出
如果结束
下一个
“绕成一排
行=最小行到最大行
'仅使用非空的行
如果是工作表(讨论).Cells(行,ID).Value“”,则
'这将检查ID是否以我们关心的相关主题代码开头,如果以相关主题代码开头,我们将继续检查
如果左(Trim(UCase(工作表(讨论).Cells(行,ID.Value)),Len(StrTopic)+1)=StrTopic&“-”
如果Mid$(工作表(讨论).Cells(行,ID).Value,InStr(3,工作表(讨论).Cells(行,ID).Value),“-”+1)>当前ID,则
currentID=Mid$(工作表(讨论).Cells(行,ID).Value,InStr(3,工作表(讨论).Cells(行,ID).Value),“-”+1)
如果结束
如果结束
如果结束
下一排
'输出包括主题代码
getNextRiskID=StrTopic&“-”¤tID+1
端函数
Private Function getNextID(ByVal StrTopic As String) As String
Static AryTopics(2, 1)     As String
Dim row                     As Integer
Dim currentID               As Integer
Dim LngCounter              As Long
currentID = 0

'By having the array declared static and being a fixed size, it will only get built once
'then rememebered
If AryTopics(0, 0) = "" Then
    AryTopics(0, 0) = "FILM"
    AryTopics(0, 1) = "FIL"
    AryTopics(1, 0) = "LANG."
    AryTopics(1, 1) = "LAN"
    AryTopics(2, 0) = "GEOG."
    AryTopics(2, 1) = "GEO"
End If

'The topic must be passed into the proce to know what to get the ID for
'This gets the related topic code from the array
For LngCounter = 0 To UBound(AryTopics, 1)
    If AryTopics(LngCounter, 0) = Trim(UCase(StrTopic)) Then
        StrTopic = AryTopics(LngCounter, 1)
        Exit For
    End If
Next

' Loop round rows
For row = MIN_ROW To MAX_ROW

    ' Only use rows which are not blank
    If Worksheets(DISCUSS).Cells(row, ID).Value <> "" Then

        'This checks to see if the ID starts with the related topic code we care about, if it does then we keep checking
        If Left(Trim(UCase(Worksheets(DISCUSS).Cells(row, ID).Value)), Len(StrTopic) + 1) = StrTopic & "-" Then

            If Mid$(Worksheets(DISCUSS).Cells(row, ID).Value, InStr(3, Worksheets(DISCUSS).Cells(row, ID).Value, "-") + 1) > currentID Then
                currentID = Mid$(Worksheets(DISCUSS).Cells(row, ID).Value, InStr(3, Worksheets(DISCUSS).Cells(row, ID).Value, "-") + 1)
            End If

        End If
    End If

Next row

'Output include the topic code
getNextRiskID = StrTopic & "-" & currentID + 1

End Function