Excel精英培训网

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

[已解决]计算日期天数的VBA代码求助

[复制链接]
发表于 2017-9-26 11:03 | 显示全部楼层 |阅读模式
本帖最后由 网络人 于 2017-9-26 14:32 编辑

求助现在的时间与单元格时间差的VBA代码
最佳答案
2017-9-26 11:17
  1. Sub aaa()
  2. With Sheet1
  3. .Range("Ai4:Ai10000").ClearContents
  4. For i = 4 To .Range("e65536").End(3).Row
  5.         If .Cells(i, 15) <> "" Then
  6.         If .Cells(i, 17) = "" Then
  7.             D = Date
  8.         Else
  9.             D = .Cells(i, 17)
  10.         End If
  11.         .Cells(i, 35) = D - .Cells(i, 15)
  12.         End If
  13. Next
  14. End With
  15. With Sheet2
  16. .Range("Ai4:Ai10000").ClearContents
  17. For i = 4 To .Range("e65536").End(3).Row
  18.         If .Cells(i, 17) <> "" Then
  19.         If .Cells(i, 18) = "" Then
  20.             D = Date
  21.         Else
  22.             D = .Cells(i, 18)
  23.         End If
  24.         .Cells(i, 35) = D - .Cells(i, 17)
  25.         End If
  26. Next
  27. End With
  28. End Sub
复制代码

工作簿2.rar

80.04 KB, 下载次数: 20

发表于 2017-9-26 11:17 | 显示全部楼层    本楼为最佳答案   
  1. Sub aaa()
  2. With Sheet1
  3. .Range("Ai4:Ai10000").ClearContents
  4. For i = 4 To .Range("e65536").End(3).Row
  5.         If .Cells(i, 15) <> "" Then
  6.         If .Cells(i, 17) = "" Then
  7.             D = Date
  8.         Else
  9.             D = .Cells(i, 17)
  10.         End If
  11.         .Cells(i, 35) = D - .Cells(i, 15)
  12.         End If
  13. Next
  14. End With
  15. With Sheet2
  16. .Range("Ai4:Ai10000").ClearContents
  17. For i = 4 To .Range("e65536").End(3).Row
  18.         If .Cells(i, 17) <> "" Then
  19.         If .Cells(i, 18) = "" Then
  20.             D = Date
  21.         Else
  22.             D = .Cells(i, 18)
  23.         End If
  24.         .Cells(i, 35) = D - .Cells(i, 17)
  25.         End If
  26. Next
  27. End With
  28. End Sub
复制代码
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-4-26 21:11 , Processed in 0.383127 second(s), 10 queries , Gzip On, Yac On.

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

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