简体   繁体   English

Excel VBA唯一ID生成器

[英]Excel VBA unique ID generator

I am trying to generate unique IDs for topics of discussion. 我正在尝试为讨论主题生成唯一ID。 The data will be like so: 数据如下:

Status    ID        Topic    Commentary
Open      FIL-1     FILM      
Open      FIL-2     FILM
Closed    LAN-1     LANG.
Open      LAN-2     LANG.

The idea is that when on a new row regardless of whether it was added above or below of the last unique ID I use VBA to find the next ID. 这个想法是,当在新行上,无论是在最后一个唯一ID的上方还是下方添加,我都使用VBA来查找下一个ID。 So for example above if I were to add another row at the top with the topic LANG. 例如,如果我要在顶部添加另一行,主题为LANG。 then it would find that LAN-2 is the lastest ID and +1 to it to become LAN-3. 那么它会发现LAN-2是最新的ID而+1是它成为LAN-3。

I got this working when the topics were all the same with the code below (topics were all "FIL" but now there are multiple topics): 当主题与下面的代码完全相同时(主题都是“FIL”,但现在有多个主题),我得到了这个工作:

Private Function getNextID() As String

Dim row As Integer
Dim currentID As Integer

currentID = 0

' 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
        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

Next row

getNextID = "FIL" & "-" & currentID + 1

End Function

Does anyone know how I can set an array with the topic abbreviations used in the ID and use the code i've already written to loop through the same process using the abbreviations in the array to get the next ID for the specific topic being added? 有没有人知道我如何设置一个数组与ID中使用的主题缩写,并使用我已编写的代码循环使用数组中的缩写相同的过程,以获取添加的特定主题的下一个ID?

This code does the trick, except the first entry for some reason (the Evaluate Formula button shows it's working, but right at the end it replaces the value with 0). 此代码可以解决问题,除了第一个条目由于某种原因(评估公式按钮显示它正在工作,但最后它将值替换为0)。

So, manually add the first ID then run the code from row 3 to the last row of your list (you'll also need to add code to ignore empty rows). 因此,手动添加第一个ID然后将代码从第3行运行到列表的最后一行(您还需要添加代码以忽略空行)。

Public Sub Test()

    Dim x As Long

    For x = 3 To 7
        AddID ThisWorkbook.Worksheets("Sheet1").Cells(x, 2)
    Next x

End Sub

Public Sub AddID(Target As Range)

    'Formula using A1 style:
    '=LEFT($C7,3) & "-" & COUNTIF($B$2:INDEX($B:$B,ROW()-1),LEFT($C7,3) & "*")+1

    'Relative column (ID is 1 column left of Topic).
    Target.FormulaR1C1 = "=LEFT(RC[1],3) & ""-"" & COUNTIF(R2C:INDEX(C,ROW()-1), LEFT(RC[1],3) & ""*"")+1"
    'Absolute column (ID is column B, Topic is column C)
    'Target.FormulaR1C1 = "=LEFT(RC3,3) & ""-"" & COUNTIF(R2C2:INDEX(C2,ROW()-1), LEFT(RC3,3) & ""*"")+1"
    Target = Target.Value

End Sub

I adjusted the code you had to include an array like you needed, it does mean you will have to pass into your procedure the name of the topic you are requesting an ID for, this could be automated if needed but its hard to know what the bigger picture is for you project so I have left it as this: - 我调整了你需要包含一个数组所需的代码,这意味着你必须将你要求提供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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM