Excel精英培训网

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

[已解决]菜鸟求助:下面代码如何修改?

[复制链接]
发表于 2015-12-26 17:36 | 显示全部楼层 |阅读模式
本帖最后由 alxixi520 于 2015-12-26 17:40 编辑

请教:下面代码如何将数据改为由D3,D4开始?并且将“六,日”对应列指定不同颜色。

Private Sub Worksheet_Change(ByVal Target As Range)
    Dim i As Integer
    Dim yy As Integer, mm As Integer
    Dim rng As Range
    Set rng = Target.Cells(1, 1)
    If rng.Address = "$AB$1" And VBA.IsDate(rng) Then
        Me.Rows(3).ClearContents
        Me.Rows(4).ClearContents
        yy = Year(rng)
        mm = Month(rng)
        For i = 1 To VBA.DateSerial(yy, mm + 1, 1) - VBA.DateSerial(yy, mm, 1)
            Me.Cells(3, i) = i
            Me.Cells(4, i) = Application.WorksheetFunction.Choose(VBA.Weekday(DateSerial(yy, mm, i)), _
                "日", "一", "二", "三", "四", "五", "六")
        Next
    End If
End Sub


最佳答案
2015-12-26 19:07
Private Sub Worksheet_Change(ByVal Target As Range)
    Dim i As Integer
    Dim yy As Integer, mm As Integer
    Application.EnableEvents = False
    If Target.Address = "$AB$1" And VBA.IsDate(Target) Then
        Me.Rows(3).ClearContents
        Me.Rows(4).Clear
        yy = Year(Target)
        mm = Month(Target)
        For i = 1 To VBA.DateSerial(yy, mm + 1, 1) - VBA.DateSerial(yy, mm, 1)
            Me.Cells(3, i + 3) = i
            Me.Cells(4, i + 3) = Application.WorksheetFunction.Choose(VBA.Weekday(DateSerial(yy, mm, i)), _
                "日", "一", "二", "三", "四", "五", "六")
           If Cells(4, i + 3) = "六" Then Cells(4, i + 3).Interior.Color = 65535
           If Cells(4, i + 3) = "日" Then Cells(4, i + 3).Interior.Color = 225
        Next
    End If
    Application.EnableEvents = True
End Sub
1.jpg
发表于 2015-12-26 19:07 | 显示全部楼层    本楼为最佳答案   
Private Sub Worksheet_Change(ByVal Target As Range)
    Dim i As Integer
    Dim yy As Integer, mm As Integer
    Application.EnableEvents = False
    If Target.Address = "$AB$1" And VBA.IsDate(Target) Then
        Me.Rows(3).ClearContents
        Me.Rows(4).Clear
        yy = Year(Target)
        mm = Month(Target)
        For i = 1 To VBA.DateSerial(yy, mm + 1, 1) - VBA.DateSerial(yy, mm, 1)
            Me.Cells(3, i + 3) = i
            Me.Cells(4, i + 3) = Application.WorksheetFunction.Choose(VBA.Weekday(DateSerial(yy, mm, i)), _
                "日", "一", "二", "三", "四", "五", "六")
           If Cells(4, i + 3) = "六" Then Cells(4, i + 3).Interior.Color = 65535
           If Cells(4, i + 3) = "日" Then Cells(4, i + 3).Interior.Color = 225
        Next
    End If
    Application.EnableEvents = True
End Sub

评分

参与人数 1 +1 收起 理由
alxixi520 + 1 赞一个

查看全部评分

回复

使用道具 举报

 楼主| 发表于 2015-12-26 21:02 | 显示全部楼层
本帖最后由 alxixi520 于 2015-12-26 21:20 编辑
zjdh 发表于 2015-12-26 19:07
Private Sub Worksheet_Change(ByVal Target As Range)
    Dim i As Integer
    Dim yy As Integer, mm ...

谢谢您好。差不多达到我需要的效果了。
           If Cells(4, i + 3) = "六" Then Cells(4, i + 3).Interior.ColorIndex = 27
           If Cells(4, i + 3) = "日" Then Cells(4, i + 3).Interior.ColorIndex = 3
           If Cells(4, i + 3) = "一" Then Cells(4, i + 3).Interior.ColorIndex = 4
           If Cells(4, i + 3) = "二" Then Cells(4, i + 3).Interior.ColorIndex = 4
           If Cells(4, i + 3) = "三" Then Cells(4, i + 3).Interior.ColorIndex = 4
           If Cells(4, i + 3) = "四" Then Cells(4, i + 3).Interior.ColorIndex = 4
           If Cells(4, i + 3) = "五" Then Cells(4, i + 3).Interior.ColorIndex = 4

这些有没有简化的方法?
回复

使用道具 举报

发表于 2015-12-26 21:53 | 显示全部楼层
本帖最后由 zjdh 于 2015-12-26 21:55 编辑

1.  If Cells(4, i + 3) = "六" Then Cells(4, i + 3).Interior.ColorIndex = 27
     If Cells(4, i + 3) = "日" Then Cells(4, i + 3).Interior.ColorIndex = 3
    If InStr("一二三四五", Cells(4, i + 3)) Then Cells(4, i + 3).Interior.ColorIndex = 4

2. Cells(4, i + 3).Interior.ColorIndex = 4
    If Cells(4, i + 3) = "六" Then Cells(4, i + 3).Interior.ColorIndex = 27
    If Cells(4, i + 3) = "日" Then Cells(4, i + 3).Interior.ColorIndex = 3
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-4-27 06:08 , Processed in 0.304200 second(s), 16 queries , Gzip On, Yac On.

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

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