Excel精英培训网

 找回密码
 注册
数据透视表40+个常用小技巧,让你一次学会!
查看: 1701|回复: 7

[已解决]添加数据的VBA代码

[复制链接]
发表于 2016-5-6 11:59 | 显示全部楼层 |阅读模式
本帖最后由 安全网 于 2016-5-7 11:17 编辑

怎样给A3:A65536列的单元格内的数据如果不够六位,设置怎样的VBA可以立即在前面添加0或者后面添加Z。
最佳答案
2016-5-7 10:21
  1. Sub qcw()
  2. Application.ScreenUpdating = False
  3. Dim r, i, m
  4. r = Range("a65536").End(xlUp).Row
  5. For i = 3 To r
  6. If Len(Cells(i, 1)) <= 5 Then
  7. If Right(Cells(i, 1), 1) <> "z" Then
  8. m = 5 - Len(Cells(i, 1))
  9. Cells(i, 1) = String(m, "0") & Cells(i, 1) & "z"
  10. Else
  11. m = 6 - Len(Cells(i, 1))
  12. Cells(i, 1) = String(m, "0") & Cells(i, 1)
  13. End If
  14. End If
  15. Next
  16. Application.ScreenUpdating = True
  17. End Sub
复制代码
excel精英培训的微信平台,每天都会发送excel学习教程和资料。扫一扫明天就可以收到新教程
发表于 2016-5-6 13:29 | 显示全部楼层
本帖最后由 七彩屋 于 2016-5-6 13:34 编辑

那是要在前面加还是在后面加?
判断字数可以用Len函数
回复

使用道具 举报

 楼主| 发表于 2016-5-6 14:53 | 显示全部楼层
七彩屋 发表于 2016-5-6 13:29
那是要在前面加还是在后面加?
判断字数可以用Len函数

先在后面加,加了还是不够位就在前面加0


回复

使用道具 举报

发表于 2016-5-6 16:48 | 显示全部楼层
加了0还不够6位的如何操作?
回复

使用道具 举报

 楼主| 发表于 2016-5-6 17:08 | 显示全部楼层
七彩屋 发表于 2016-5-6 16:48
加了0还不够6位的如何操作?

继续加零
回复

使用道具 举报

发表于 2016-5-6 17:43 | 显示全部楼层
  1. Sub qcw()
  2. Application.ScreenUpdating = False
  3. Dim r, i, m
  4. r = Range("a65536").End(xlUp).Row
  5. For i = 3 To r
  6. If Len(Cells(i, 1)) <= 5 Then
  7. m = 5 - Len(Cells(i, 1))
  8. Cells(i, 1) = String(m, "0") & Cells(i, 1) & "z"
  9. End If
  10. Next
  11. Application.ScreenUpdating = True
  12. End Sub
复制代码
回复

使用道具 举报

 楼主| 发表于 2016-5-7 09:06 | 显示全部楼层
七彩屋 发表于 2016-5-6 17:43

如果后面已经有了Z,还是会自动加Z,能否识别有就不加
回复

使用道具 举报

发表于 2016-5-7 10:21 | 显示全部楼层    本楼为最佳答案   
  1. Sub qcw()
  2. Application.ScreenUpdating = False
  3. Dim r, i, m
  4. r = Range("a65536").End(xlUp).Row
  5. For i = 3 To r
  6. If Len(Cells(i, 1)) <= 5 Then
  7. If Right(Cells(i, 1), 1) <> "z" Then
  8. m = 5 - Len(Cells(i, 1))
  9. Cells(i, 1) = String(m, "0") & Cells(i, 1) & "z"
  10. Else
  11. m = 6 - Len(Cells(i, 1))
  12. Cells(i, 1) = String(m, "0") & Cells(i, 1)
  13. End If
  14. End If
  15. Next
  16. Application.ScreenUpdating = True
  17. End Sub
复制代码
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|Archiver|Excel精英培训 ( 豫ICP备11015029号 )

GMT+8, 2024-5-20 15:06 , Processed in 0.177478 second(s), 9 queries , Gzip On, Yac On.

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表