Excel精英培训网

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

[已解决]此代码如何修改

[复制链接]
发表于 2011-8-10 13:16 | 显示全部楼层 |阅读模式
本帖最后由 Aкon 于 2011-8-10 14:01 编辑

请高手们帮我看一下这段代码,如何不受12个月限制:谢谢!
Dim i As Long, n As Long
  Dim arr
  If DTPicker5 = "" Then GoTo ErrHandler
  arr = Split(DTPicker5)
  Dim DD As Date
  If arr(0) > 12 Then MsgBox "请检查您输入的月份,不能超过12月。": Exit Sub
  If arr(0) > 12 Then MsgBox "请检查您输入的日期,不能超过31号。": Exit Sub
  DD = Year(Date) & "-" & arr(0) & "-" & arr(1)
  
  Me.ListView1.ListItems.Clear
ErrHandler:
  For i = 3 To Sheet1.[b2].End(4).Row
      If DateDiff("d", Sheet1.Cells(i, "B"), DD) = 0 Or TextBox1 = "" Then
         n = n + 1
2:    'Listview添加数据
         With Me.ListView1.ListItems.Add()
           .Text = Format(Sheet1.Cells(i, "B"), "yyyy年m月d日")  '第一项添加日期
           .SubItems(1) = Sheet1.Cells(i, "C") '报表第二项为C列,以下略
           .SubItems(2) = Sheet1.Cells(i, "D")
           .SubItems(3) = Sheet1.Cells(i, "E")
           .SubItems(4) = Sheet1.Cells(i, "F")
         End With
      End If
  Next
  If n = 0 Then MsgBox "你输入的查询日期无记录,请重新输入您要查询的日期!": TextBox1 = "": TextBox1.SetFocus
End Sub
最佳答案
2011-8-10 14:59
回复 Aкon 的帖子

  1. Private Sub CommandButton1_Click()
  2.     Dim i As Long, n As Long
  3.     Dim arr
  4.     If DTPicker5 = "" Then Exit Sub
  5.     Dim DD As Date
  6.     DD = DTPicker5.Value
  7.     Me.ListView1.ListItems.Clear
  8.     For i = 3 To Sheet1.[b2].End(4).Row
  9.         If Sheet1.Cells(i, "B") = DD Then
  10.             n = n + 1
  11.             'Listview添加数据
  12.             With Me.ListView1.ListItems.Add()
  13.                 .Text = Format(Sheet1.Cells(i, "B"), "yyyy年m月d日")  '第一项添加日期
  14.                 .SubItems(1) = Sheet1.Cells(i, "C")    '报表第二项为C列,以下略
  15.                 .SubItems(2) = Sheet1.Cells(i, "D")
  16.                 .SubItems(3) = Sheet1.Cells(i, "E")
  17.                 .SubItems(4) = Sheet1.Cells(i, "F")
  18.             End With
  19.         End If
  20.     Next
  21.     If n = 0 Then
  22.         MsgBox "你输入的查询日期无记录,请重新输入您要查询的日期!"
  23.         Me.ListView1.ListItems.Clear
  24.         DTPicker5 = Date
  25.         DTPicker5.SetFocus
  26.     End If
  27. End Sub
复制代码

问题.rar

15.26 KB, 下载次数: 12

excel精英培训的微信平台,每天都会发送excel学习教程和资料。扫一扫明天就可以收到新教程
发表于 2011-8-10 13:30 | 显示全部楼层
回复

使用道具 举报

发表于 2011-8-10 14:59 | 显示全部楼层    本楼为最佳答案   
回复 Aкon 的帖子

  1. Private Sub CommandButton1_Click()
  2.     Dim i As Long, n As Long
  3.     Dim arr
  4.     If DTPicker5 = "" Then Exit Sub
  5.     Dim DD As Date
  6.     DD = DTPicker5.Value
  7.     Me.ListView1.ListItems.Clear
  8.     For i = 3 To Sheet1.[b2].End(4).Row
  9.         If Sheet1.Cells(i, "B") = DD Then
  10.             n = n + 1
  11.             'Listview添加数据
  12.             With Me.ListView1.ListItems.Add()
  13.                 .Text = Format(Sheet1.Cells(i, "B"), "yyyy年m月d日")  '第一项添加日期
  14.                 .SubItems(1) = Sheet1.Cells(i, "C")    '报表第二项为C列,以下略
  15.                 .SubItems(2) = Sheet1.Cells(i, "D")
  16.                 .SubItems(3) = Sheet1.Cells(i, "E")
  17.                 .SubItems(4) = Sheet1.Cells(i, "F")
  18.             End With
  19.         End If
  20.     Next
  21.     If n = 0 Then
  22.         MsgBox "你输入的查询日期无记录,请重新输入您要查询的日期!"
  23.         Me.ListView1.ListItems.Clear
  24.         DTPicker5 = Date
  25.         DTPicker5.SetFocus
  26.     End If
  27. End Sub
复制代码

回复

使用道具 举报

 楼主| 发表于 2011-8-10 15:13 | 显示全部楼层
谢谢!那么帅老师。
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-5-23 19:23 , Processed in 0.236536 second(s), 6 queries , Gzip On, Yac On.

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

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