我在excel中有一个表,其中有一个名为“节点ID”的列,该表应自动填充一个标识每一行“名称”的唯一值。
要求是,每当用户在表中添加新行时(无论用户在何处添加列,都不必在表末尾),单元格都将填充新的(不存在)数。我还想保留已经存在的行的节点ID(因此不被修改)。另外,请成为自动生成的列,并且用户无法修改。他们将添加一行,填充其他列,并且将在不执行任何操作的情况下生成此单元格。
例如:
Country,Name,Node ID
FR,ParentA,1
FR,ChildA,2
FR,ChildB,3
如果我在中间添加一行,结果应该是:
FR,ParentA,1
FR,ChildA,2
UK,ParentB,4
FR,ChildB,3
以便先前的节点保留其节点ID。任何想法?预先感谢!
答案 0 :(得分:0)
问题在于randbetween是一个易失函数,这意味着每次excel重新计算时都会重新计算。我们可以将其关闭。但我认为以下其中一项效果更好:
用户定义的功能:
输出ID为NUM:
Private Declare PtrSafe Function GetTickCount Lib "kernel32" () As Long
Function UNIQUEID() As String
Dim numKey As Collection
Dim rndNum As Long
Dim rndNumSplit() As String
Dim num As Variant
rndNum = Int(Date)
UNIQUEID = rndNum * Right(GetTickCount, 3)
Do While Len(UNIQUEID) < 8
UNIQUEID = UNIQUEID + Right(GetTickCount, 1)
Loop
End Function
输出ID作为STRING:
Private Declare PtrSafe Function GetTickCount Lib "kernel32" () As Long
Function UNIQUEID() As String
Dim numKey As Collection
Dim rndNum As Long
Dim rndNumSplit() As String
Dim num As Variant
Set numKey = New Collection
numKey.Add "Z", "0"
numKey.Add "A", "1"
numKey.Add "B", "2"
numKey.Add "C", "3"
numKey.Add "D", "4"
numKey.Add "E", "5"
numKey.Add "F", "6"
numKey.Add "G", "7"
numKey.Add "H", "8"
numKey.Add "I", "9"
numKey.Add "W", ""
rndNum = Int(Date)
rndNum = rndNum * Right(GetTickCount, 3)
rndNumSplit = Split(StrConv(rndNum, vbUnicode), Chr$(0))
For Each num In rndNumSplit
UNIQUEID = UNIQUEID + numKey(num)
Next num
End Function
您可以使用此公式,然后删除A列中的“是”以对randbetween生成的值进行硬设置。
=IF(AND($A$3="YES",C3<>""),RANDBETWEEN(1,100000),B3)