Excel VBA 自动添加新行并递增ID
2017-05-06 18:56
711 查看
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 Len(IdVal) = LastZeroPosInId Then IdValPart = 0 Else IdValPart = Right(IdVal, Len(IdVal) - LastZeroPosInId) End If NewId = IdValPart + 1 ''Remove one zero, if NewId lengthen. If Len(NewId) + Len(IdPrefix) > Len(IdVal) Then IdPrefix = Left(IdPrefix, Len(IdPrefix) - 1) End If Cells(Line + i - 1, 2).Value = IdPrefix & NewId End If ''Ip: 202.123.176.63 IpVal = Cells(Line + i - 1, 4).Value LastDotPos = InStrRev(IpVal, ".") IpPrefix = Left(IpVal, LastDotPos) IpValPart = Right(IpVal, Len(IpVal) - LastDotPos) NewIp = IpValPart + 1 Cells(Line + i - 1, 4).Value = IpPrefix & NewIp Next i ''ActiveWorkbook.Save End Sub
处理前:
处理后:
这样可以从繁重的重复劳动中解放出来。
相关文章推荐
- 建立存储过程 ID 自动添加 yymmdd+四位序列
- 在ASP.NET+ORACLE添加数据记录并让ID自动增量
- mysql获取刚插入(添加)记录的自动编号id
- Oracle数据库创建表ID字段的自动递增
- 返回最新添加的学生的自动编号Id
- [导入]在ASP.NET+ORACLE添加数据记录并让ID自动增量
- 在SQL2008中,如何让id自动生成并自动递增?如何让时间默认生成?
- oracle 怎样设置自动递增的的字段,也就是设置自动递增的ID 主键
- mysql获取刚插入(添加)记录的自动编号id
- 在SQL2008中,如何让id自动生成并自动递增?如何让时间默认生成?
- asp.net access添加返回自递增id的实现方法第1/3页
- asp中通过addnew添加内容后取得当前文章的自递增ID的方法
- asp中通过addnew添加内容后取得当前文章的自递增ID的方法
- mysql获取刚插入(添加)记录的自动编号id
- Oracle数据库创建表ID字段的自动递增
- 在asp.net2中针对不支持cookie的APP自动添加sessionid到URL中
- 控件之DataGrid----实现自动添加新行和数据交换
- 黄聪:PHP+MySql获取自动增长字段的新添加记录ID值
- 在winsshd 中添加id_rsa.pub 实现Windows 服务器主机自动信任Linux 客户端
- 在SQL2008中,如何让id自动生成并自动递增?如何让时间默认生成?