vba:如果单元格为空,请添加唯一的ID

我试图添加一个唯一的ID只有当一个范围内的单元格是空白的。 这样它将保留旧的ID,但会为新行创build唯一的ID。 对于下面的代码,我正在尝试使用数字14(2014)创buildID,并在数据集中使用两个其他variables(ADPNumber和PRJNumber)。 然后,id将会因整数“i”而变化,这个整数必须是4位数。 我在下面的代码中得到了运行时错误13,说“Set myCell …”

For Each myCell In ActiveSheet.Range("a9:a89").Cells Dim i i = 1 'i.NumberFormat = "0000" If myCell.Value = "" Then Set myCell = "14" & ADPNumber & PRJNumber & Format(i, "0000") i = i + 1 End If Next myCell 

尝试:

 myCell.Value = "14" & ADPNumber & PRJNumber & Format(i, "0000")