Excel精英培训网

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

[练习题] 工作表事件程序练习1(已结束)

  [复制链接]
发表于 2012-2-18 20:52 | 显示全部楼层 |阅读模式
本帖最后由 无聊的疯子 于 2012-2-20 20:22 编辑

这是论坛里的一个实列,拆分后拿来做一个Change事件的练习题

代码要求:
    with 是必需要使用的,套用越多越好。。但不可重复套用
    如 with  Range("A5:F5")   而后又来个 with Range("A5:F5")  这样就重复使用了

结果要求说明
    第一行不能填充颜色
    1.同一行中,D列有日期并且E列没有日期,则A到F填充上红色
    2.同一行中,D列有日期并且E列有日期,   则A到F列填充上淡蓝色
    3.同一行中,D列的日期大于系统日期3天,则A到F列无颜色填充
就三个效果

提示:
    该事件程序为 Worksheet_Change 事件
    关于单元格颜色代码,附件中也有说明
    要多用事件中的 Target 参数
   
答题截止日期:2012年2月20日 20:00  

凡VBA学习小组的成员均可参与练习
回帖注明小组编号与ID,答案正确者,给予一定的评分奖励
重复回复无重复评分

若发现编号与ID不符者可能会受到差评(不填写编号与ID者不给予任何评分)

本贴已设置《仅作者可见》 直接贴代码即可

所需效果如下图,类似于条件格式,
该图为原实列,只需要达到该效果就行,不用管操作的是 D E 列还是 I J 列

效果.gif

Change事件程序练习1.rar (6.59 KB, 下载次数: 175)

评分

参与人数 5 +71 金币 +50 收起 理由
bbhiox + 1 很给力!
半个城市 + 20 + 20 赞一个!
sunjing-zxl + 20 赞一个!
wenchduan + 15 + 30 很给力!
windimi007 + 15 很给力!

查看全部评分

发表于 2012-2-18 22:07 | 显示全部楼层
本帖最后由 冠军欧洲2010 于 2012-2-18 22:10 编辑

游客,如果您要查看本帖隐藏内容请回复


点评

没有使用 with 呢  发表于 2012-2-18 22:54
回复

使用道具 举报

发表于 2012-2-18 23:23 | 显示全部楼层
我只是看得眼花缭乱,只有羡慕的份了。
回复

使用道具 举报

发表于 2012-2-18 23:51 | 显示全部楼层
本帖最后由 jiahua1010 于 2012-2-18 23:56 编辑

A16:jiahua1010
  1. Private Sub Worksheet_Change(ByVal Target As Range)
  2.     For i = 2 To Range("d65536").End(xlUp).Row
  3.         If Cells(i, 4) - Now() > 3 Then
  4.             Range(Cells(i, 1), Cells(i, 6)).Interior.ColorIndex = 0
  5.         ElseIf Cells(i, 4) <> "" And Cells(i, 5) = "" Then
  6.             Range(Cells(i, 1), Cells(i, 6)).Interior.ColorIndex = 3
  7.         ElseIf Cells(i, 4) <> "" And Cells(i, 5) <> "" Then
  8.             Range(Cells(i, 1), Cells(i, 6)).Interior.ColorIndex = 34
  9.         End If
  10.     Next
  11. End Sub
复制代码
  1. Private Sub Worksheet_Change(ByVal Target As Range)
  2.     With Target
  3.     If (.Column = 4 Or .Column = 5) And .Row <> 1 Then
  4.         If Cells(.Row, 4) = "" Or Cells(.Row, 4) - Now() > 3 Then
  5.             Range(Cells(.Row, 1), Cells(.Row, 6)).Interior.ColorIndex = 0
  6.         ElseIf Cells(.Row, 4) <> "" And Cells(.Row, 5) = "" Then
  7.             Range(Cells(.Row, 1), Cells(.Row, 6)).Interior.ColorIndex = 3
  8.         ElseIf Cells(.Row, 4) <> "" And Cells(.Row, 5) <> "" Then
  9.             Range(Cells(.Row, 1), Cells(.Row, 6)).Interior.ColorIndex = 34
  10.         End If
  11.     End If
  12.     End With
  13.    End Sub
复制代码
  1.    Private Sub Worksheet_Change(ByVal Target As Range)
  2.     With Target
  3.     If (.Column = 4 Or .Column = 5) And .Row <> 1 Then
  4.         If Cells(.Row, 4) = "" Or Cells(.Row, 4) - Now() > 3 Then
  5.             Range("a" & .Row & ":f" & .Row).Interior.ColorIndex = 0
  6.         ElseIf Cells(.Row, 4) <> "" And Cells(.Row, 5) = "" Then
  7.             Range("a" & .Row & ":f" & .Row).Interior.ColorIndex = 3
  8.         ElseIf Cells(.Row, 4) <> "" And Cells(.Row, 5) <> "" Then
  9.             Range("a" & .Row & ":f" & .Row).Interior.ColorIndex = 34
  10.         End If
  11.     End If
  12.     End With
  13.    End Sub
复制代码



评分

参与人数 1 +3 收起 理由
无聊的疯子 + 3 测试通过,给予奖励

查看全部评分

回复

使用道具 举报

发表于 2012-2-18 23:59 | 显示全部楼层
C12:hrpotter
  1. Private Sub Worksheet_Change(ByVal Target As Range)
  2.     Dim i As Long
  3.     On Error Resume Next
  4.     i = Target.Row
  5.     If i > 1 Then
  6.         With Range("a" & i & ":f" & i)
  7.             If Cells(i, 4) - Now() > 3 Then
  8.                 .Interior.ColorIndex = 0
  9.             ElseIf IsDate(Cells(i, 4)) And IsDate(Cells(i, 5)) = False Then
  10.                 .Interior.ColorIndex = 3
  11.             ElseIf IsDate(Cells(i, 4)) And IsDate(Cells(i, 5)) Then
  12.                 .Interior.ColorIndex = 34
  13.             Else
  14.                 .Interior.ColorIndex = 0
  15.             End If
  16.         End With
  17.     End If
  18. End Sub
复制代码

评分

参与人数 1 +3 收起 理由
无聊的疯子 + 3 测试通过,符合要求,奖励一点

查看全部评分

回复

使用道具 举报

发表于 2012-2-19 01:33 | 显示全部楼层
本帖最后由 happym8888 于 2012-2-19 14:28 编辑
  1. Private Sub Worksheet_Change(ByVal Target As Range)
  2. With Target
  3. If .Row = 1 Then Exit Sub
  4. If Cells(.Row, 4) <> "" Then
  5. If Day(Cells(.Row, 4)) - Day(Now()) > 3 Then
  6. Range("A" & .Row & ":F" & .Row).Interior.ColorIndex = 0
  7. ElseIf Cells(.Row, 5) <> "" Then
  8. Range("A" & .Row & ":F" & .Row).Interior.ColorIndex = 34
  9. ElseIf Cells(.Row, 5) = "" Then
  10. Range("A" & .Row & ":F" & .Row).Interior.ColorIndex = 3
  11. End If
  12. End If
  13. End With
  14. End Sub
复制代码
C17:Happym8888

点评

测试通过,没有组别和论坛ID,不给予评分!  发表于 2012-2-19 14:24

评分

参与人数 1 +3 收起 理由
无聊的疯子 + 3 下不为列

查看全部评分

回复

使用道具 举报

发表于 2012-2-19 10:44 | 显示全部楼层
D组学委:windimi007
疯子,看效果图,有红色后面就有“过期”,怎么感觉怪怪的?
就用了一个WITH,应该可以吧!
  1. Private Sub Worksheet_Change(ByVal Target As Range)
  2.     Dim rw&
  3.     rw = Target.Row
  4.     If rw > 1 Then
  5.         Application.EnableEvents = False
  6.         With Cells(rw, 1).Resize(, 6)
  7.             .Interior.ColorIndex = 0
  8.             Cells(rw, 7) = ""
  9.             If IsDate(Cells(rw, 4)) Then
  10.                 If Cells(rw, 4) - Date <= 3 Then
  11.                     If IsDate(Cells(rw, 5)) Then
  12.                         .Interior.ColorIndex = 34
  13.                     Else
  14.                         .Interior.ColorIndex = 3
  15.                         Cells(rw, 7) = "过期"
  16.                     End If
  17.                 End If
  18.             End If
  19.         End With
  20.         Application.EnableEvents = True
  21.     End If
  22. End Sub
复制代码

Change事件程序练习1-windimi007.rar

9.17 KB, 下载次数: 27

点评

测试通过,那个效果是原件,录动画片的时候没有处理那一块,可以不管的!  发表于 2012-2-19 14:21

评分

参与人数 1 +3 收起 理由
无聊的疯子 + 3 测试通过,符合要求,奖励一点

查看全部评分

回复

使用道具 举报

发表于 2012-2-19 11:47 | 显示全部楼层
本帖最后由 dsjohn 于 2012-2-20 16:55 编辑

Private Sub Worksheet_Change(ByVal Target As Range)
Dim Rg
Rg = Target.Row
    Range("a1:f1").Interior.ColorIndex = 0
    With Range(Cells(Rg, 1), Cells(Rg, 6))
        If IsDate(Cells(Rg, 4).Value) And Not IsDate(Cells(Rg, 5).Value) Then
            .Interior.ColorIndex = 3
        ElseIf Cells(Rg, 4) - Cells(Rg, 2) > 3 Then
            .Interior.ColorIndex = 0
        ElseIf IsDate(Cells(Rg, 4).Value) And IsDate(Cells(Rg, 5).Value) Then
            .Interior.ColorIndex = 34
        End If
    End With
End Sub

B017: dsjohn
谢谢了

点评

虽然不是和系统日期对比,但是结果正确!  发表于 2012-2-19 14:17

评分

参与人数 1 +3 收起 理由
无聊的疯子 + 3

查看全部评分

回复

使用道具 举报

发表于 2012-2-19 12:28 | 显示全部楼层
E05:linch92413 交答案
Private Sub Worksheet_Change(ByVal Target As Range)
   Dim N As Long
   N = Target.Row
   With Range(Cells(N, 1), Cells(N, 5))
      If Cells(N, 4) <> "" And Cells(N, 5) = "" Then
         .Interior.ColorIndex = 3
      ElseIf Cells(N, 4) <> "" And Cells(N, 5) <> "" Then
         .Interior.ColorIndex = 34
      ElseIf Cells(N, 4).Value - Date > 3 Then
         .Interior.ColorIndex = 0
      End If
   End With
End Sub
Change事件程序练习1(linch92413).rar (9.67 KB, 下载次数: 13)

点评

代码很好,但填充无色代码起不了作用!  发表于 2012-2-19 14:06
回复

使用道具 举报

发表于 2012-2-19 14:28 | 显示全部楼层
本帖最后由 一缕忧兰 于 2012-2-20 18:07 编辑

A07:一缕忧兰
Private Sub Worksheet_Change(ByVal Target As Range)
With Target
    With Range(Cells(.Row, "A"), Cells(.Row, "F")).Interior
        If Range("D" & Target.Row) <> "" And Range("D" & Target.Row) - Date > 3 Then
                  .ColorIndex = 0
        ElseIf Range("E" & Target.Row) = "" Then
                 .ColorIndex = 3
        ElseIf Range("E" & Target.Row) <> "" Then
                 .ColorIndex = 34
        End If
     End With
End With
End Sub

点评

代码有问题,操作不可逆  发表于 2012-2-20 12:39

评分

参与人数 1 +3 收起 理由
无聊的疯子 + 3

查看全部评分

回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-4-26 07:58 , Processed in 0.297354 second(s), 9 queries , Gzip On, Yac On.

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

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