连续序列号生成(包括重复值)

时间:2020-09-08 15:48:32

标签: excel vba

我很困惑,希望得到专家的帮助。

我正在尝试创建一个VBA宏,该宏基于 A B 列中分配唯一序列号 >。我设法提出了一个简单的宏,该宏为电子表格中的每一行分配了一个序列号(无论其值如何)。但是,我的工作簿在 A 列中也包含重复项,我希望唯一序列可以重复到下一个新字符串。

Sub serialgenerator()
Dim LastRow As Long
LastRow = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Row
Cells(2, 2).Formula = CLng("1")
Cells(2, 2).Autofill Destination:=Range(Cells(2, 2), Cells(LastRow, 2)), Type:=xlFillSeries
End Sub

enter image description here

非常感谢大家的帮助。

干杯。

弗洛里安

3 个答案:

答案 0 :(得分:0)

唯一(字典,数组)

Option Explicit

Sub SerialGenerator()
    
    ' Define Last Row.
    Dim LastRow As Long
    LastRow = Cells(Rows.Count, 1).End(xlUp).Row
    
    ' Write Source Range to Data Array.
    Dim rng As Range: Set rng = Range(Cells(2, 1), Cells(LastRow, 1))
    Dim Data As Variant: Data = rng.Value
    
    ' Using the dictionary, get the unique values and at the same time
    ' write them to Data Array overwriting the original values with
    ' the unique values for each row.
    Dim dict As Object: Set dict = CreateObject("Scripting.Dictionary")
    Dim Curr As Variant, i As Long, j As Long
    For i = 1 To UBound(Data)
        Curr = Data(i, 1)
        If Not IsError(Curr) Then
            If Not IsEmpty(Curr) Then
                If Not dict.Exists(Curr) Then
                    j = j + 1
                    dict(Curr) = j
                End If
                Data(i, 1) = dict(Curr)
            'Else ' IsEmpty(Curr)
            End If
        'Else ' IsError(Curr)
            'data(i,1) = Empty
        End If
    Next i
    
    ' Write values from Data Array to Target Range.
    rng.Offset(, 1).Value = Data

    ' Inform user.
    MsgBox "Done."

End Sub

答案 1 :(得分:0)

这可以解决问题:

$ heroku local web
[OKAY] Loaded ENV .env File as KEY=VALUE Format
10:45:21 web.1   |  > pinpo-parse-server@4.2.9 start:dyno:web /Users/percypyan/Developer/projects/pinpo/pinpo-parse-server
10:45:21 web.1   |  > npm run start:process:${WEB_PROCESS_NAME}
10:45:22 web.1   |  > pinpo-parse-server@4.2.9 start:process:parse-server /Users/percypyan/Developer/projects/pinpo/pinpo-parse-server
10:45:22 web.1   |  > node --optimize_for_size --gc_interval=100 lib/apps/parse-server
10:45:24 web.1   |  Error: Must provide Source. Received: undefined.
10:45:24 web.1   |      at devAssert (/Users/percypyan/Developer/projects/pinpo/pinpo-parse-server/node_modules/graphql/jsutils/devAssert.js:12:11)
10:45:24 web.1   |      at new Parser (/Users/percypyan/Developer/projects/pinpo/pinpo-parse-server/node_modules/graphql/language/parser.js:80:67)
10:45:24 web.1   |      at Object.parse (/Users/percypyan/Developer/projects/pinpo/pinpo-parse-server/node_modules/graphql/language/parser.js:35:16)
10:45:24 web.1   |      at Object.<anonymous> (/Users/percypyan/Developer/projects/pinpo/pinpo-parse-server/node_modules/graphql-tools/dist/stitching/introspectSchema.js:39:42)
10:45:24 web.1   |      at Module._compile (internal/modules/cjs/loader.js:759:30)
10:45:24 web.1   |      at Object.Module._extensions..js (internal/modules/cjs/loader.js:770:10)
10:45:24 web.1   |      at Module.load (internal/modules/cjs/loader.js:628:32)
10:45:24 web.1   |      at Function.Module._load (internal/modules/cjs/loader.js:555:12)
10:45:24 web.1   |      at Function.wrappedLoad [as _load] (/Users/percypyan/Developer/projects/pinpo/pinpo-parse-server/node_modules/newrelic/lib/shimmer.js:481:24)
10:45:24 web.1   |      at Module.require (internal/modules/cjs/loader.js:666:19)
10:45:24 web.1   |  npm
10:45:24 web.1   |   ERR! code ELIFECYCLE
10:45:24 web.1   |  npm ERR! errno 1
10:45:24 web.1   |  npm
10:45:24 web.1   |   ERR! pinpo-parse-server@4.2.9 start:process:parse-server: `node --optimize_for_size --gc_interval=100 lib/apps/parse-server`
10:45:24 web.1   |  npm ERR! Exit status 1
10:45:24 web.1   |  npm ERR!
10:45:24 web.1   |  npm ERR! Failed at the pinpo-parse-server@4.2.9 start:process:parse-server script.
10:45:24 web.1   |  npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
10:45:24 web.1   |  npm ERR! A complete log of this run can be found in:
10:45:24 web.1   |  npm ERR!     /Users/percypyan/.npm/_logs/2020-09-09T08_45_24_589Z-debug.log
10:45:24 web.1   |  npm
10:45:24 web.1   |   ERR! code ELIFECYCLE
10:45:24 web.1   |  npm ERR! errno 1
10:45:24 web.1   |  npm 
10:45:24 web.1   |  ERR! pinpo-parse-server@4.2.9 start:dyno:web: `npm run start:process:${WEB_PROCESS_NAME}`
10:45:24 web.1   |  npm ERR! Exit status 1
10:45:24 web.1   |  npm ERR! 
10:45:24 web.1   |  npm
10:45:24 web.1   |   ERR! Failed at the pinpo-parse-server@4.2.9 start:dyno:web script.
10:45:24 web.1   |  npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
10:45:24 web.1   |  npm ERR! A complete log of this run can be found in:
10:45:24 web.1   |  npm ERR!     /Users/percypyan/.npm/_logs/2020-09-09T08_45_24_655Z-debug.log
[DONE] Killing all processes with signal  SIGINT
10:45:24 web.1   Exited with exit code null

如果一个单元格与其先前的单元格不同,则唯一序列号会增加,否则保持不变。

答案 2 :(得分:0)

只要您对A列进行了排序,它们就按数字顺序排列(就像10,10,11,12,12,13而不是10,11,12,10,13,12等)那么这段代码很简单:

Dim lastrow As Long, Counter As Long

lastrow = Range("A" & Rows.Count).End(xlUp).Row
Counter = 0

For i = 2 To lastrow
    If Range("A" & i) <> Range("A" & i - 1) Then
        Counter = Counter + 1
        Range("B" & i) = Counter
    Else
        Range("B" & i) = Counter
    End If
Next i