Excel VBA 自动添加新行并递增ID
用Excel 2010的VBA功能来处理数据,主要根据IP网段和网段IP个数,来生成每个IP地址,并进行ID编号。
Sub AutoInsert()
Line = 1531
Count = Cells(Line, 5).Value
''Count = 5
For i = 2 To Count
''Copy
Range(Cells(Line + i - 2, 1), Cells(Line + i - 2, 6)).Select
Selection.Copy
''Insert
Range(Cells(Line + i - 1, 1), Cells(Line + i - 1, 6)).Rows.Insert
'' Change Id and Ip Cell Value
''Id: ESP004001
IdVal = Cells(Line + i - 1, 2).Value
If i = 2 And InStr(IdVal, "ESP") = 0 Then
''FH00318 ---> FH00318001
Cells(Line + i - 1, 2).Value = IdVal & "001"
Else
LastZeroPosInId = InStrRev(IdVal, "0")
IdPrefix = Left(IdVal, LastZeroPosInId)
If L