ExcelHome技术论坛

 找回密码
 免费注册

QQ登录

只需一步,快速开始

快捷登录

搜索
EH技术汇-专业的职场技能充电站 妙哉!函数段子手趣味讲函数 Excel服务器-会Excel,做管理系统 Excel Home精品图文教程库
HR薪酬管理数字化实战 Excel 2021函数公式学习大典 Excel数据透视表实战秘技 打造核心竞争力的职场宝典
300集Office 2010微视频教程 数据工作者的案头书 免费直播课集锦 ExcelHome出品 - VBA代码宝免费下载
用ChatGPT与VBA一键搞定Excel WPS表格从入门到精通 Excel VBA经典代码实践指南
查看: 1037|回复: 1

单元格填充颜色求助

[复制链接]

TA的精华主题

TA的得分主题

发表于 2020-3-17 22:32 | 显示全部楼层 |阅读模式
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件       ★免费下载 ★       ★ 使用帮助
本帖最后由 crega123 于 2020-3-19 02:42 编辑

我有一个退票表,当日订票的,填充黄色(当日是指工作簿名称的数字部分);其它订票日期(都是当日之前的日期),按发车时间项升序后,分别按不同发车日期填充不同颜色。但有两个可能性,第一个可能是,没有当日订票的车票;第二个可能是,没有其它订票日期的车票,这些要用到Go to语句。如果全都有,我做好了代码,但以上两个可能性,不知怎么用语句:

Sub 退票汇总()
   Dim m%, t As String, y As Long, arng As Range, a%, b%
    For m = 2 To [A2].CurrentRegion.Rows.Count  'm为2到K列有数据的所有行号,在订票时间后面标明"退票"字样
        w = Range("a1").End(xlDown).Row
        Cells(m, 11) = "退票"
    Next
    Range("M2").Formula = "=LEFT(J2,10)"
    For i = 2 To Range("j2").End(xlDown).Row
        Cells(i, 13) = Format(Left(Cells(i, 10), 10), "yyyymmdd")
    Next
    For q = 2 To Range("j2").End(xlDown).Row
        t = ActiveWorkbook.Name
        y = val(Mid(t, 5))
        u = Application.Max(Range("M" & q))
        If y = u Then
            If arng Is Nothing Then
                Set arng = Range("M" & q)
            Else
                Set arng = Union(arng, Range("M" & q))
                arng.Select
                a = Selection.Rows(1).Row
                b = Selection.Rows(Selection.Rows.Count).Row
                Range(Cells(a, 1), Cells(b, 11)).Interior.ColorIndex = 6
            End If
        End If
    Next
    With Range(Cells(2, 1), Cells(a, 12).Offset(-1, 0))
        .Sort Key1:=Range("d1"), Order1:=xlAscending, Header:=xlYes
    End With
    Dim e%, color%
    Set d = CreateObject("scripting.dictionary")
    S = Cells(a, 1).Offset(-1, 0).Row()
    For e = 2 To S
        dt = Format(Cells(e, 4), "yyyymmdd")
        If d.Exists(dt) Then
            Set d(dt) = Union(d(dt), Cells(e, 1).Resize(1, 11))
        Else
            Set d(dt) = Cells(e, 1).Resize(1, 11)
        End If
    Next
    For j = 0 To d.Count - 1
        d.items()(j).Interior.ColorIndex = j + 37 Mod 56
    Next j

    If u < y Then
        With Range("a1").CurrentRegion
            .Sort Key1:=Range("d1"), Order1:=xlAscending, Header:=xlYes
        End With
        Dim k%, color2%
        Set d2 = CreateObject("scripting.dictionary")
        s1 = Range("a1").End(xlUp).Row
        For e1 = 2 To s1
            dt = Format(Cells(e1, 4), "yyyymmdd")
            If d.Exists(dt1) Then
                Set d(dt1) = Union(d2(dt1), Cells(e1, 1).Resize(1, 11))
            Else
                Set d2(dt1) = Cells(e1, 1).Resize(1, 11)
            End If
        Next
        For j1 = 0 To d2.Count - 1
            d2.items()(j1).Interior.ColorIndex = j1 + 37 Mod 56
        Next j1
    End If
    Application.ScreenUpdating = True
End Sub
   


这是原来的,附件是做好的。

这是原来的,附件是做好的。

退票表.zip

8.5 KB, 下载次数: 0

TA的精华主题

TA的得分主题

 楼主| 发表于 2020-3-19 13:07 | 显示全部楼层
我已做好了,有人能帮我修改一下代码吗?
Sub 退票汇总()
    Application.ScreenUpdating = False
    Range("K1").CurrentRegion.Select
    With Selection
        .Sort Key1:=Range("j1"), Order1:=xlAscending, Header:=xlYes
    End With
    Dim m%, t As String, y As Long, arng As Range, a%, b%
    For m = 2 To [A2].CurrentRegion.Rows.Count  'm为2到K列有数据的所有行号,在订票时间后面标明"退票"字样
        w = Range("a1").End(xlDown).Row
        Cells(m, 11) = "退票"
    Next
    Range("M2").Formula = "=LEFT(J2,10)"
    For i = 2 To Range("j2").End(xlDown).Row
        Cells(i, 13) = Format(Left(Cells(i, 10), 10), "yyyymmdd")
    Next
    For q = 2 To Range("j2").End(xlDown).Row
        t = ActiveWorkbook.Name
        y = val(Mid(t, 5))
        u = Application.Max(Range("M" & q))
        If y = u Then
            If arng Is Nothing Then
                Set arng = Range("M" & q)
            Else
                Set arng = Union(arng, Range("M" & q))
                arng.Select
                a = Selection.Rows(1).Row
                b = Selection.Rows(Selection.Rows.Count).Row
                Range(Cells(a, 1), Cells(b, 11)).Interior.ColorIndex = 6
            End If
        End If
    Next
    p = Range("a1").CurrentRegion.Rows(2).Row
    With Range(Cells(p, 1), Cells(a, 12).Offset(-1, 0))
        .Sort Key1:=Range("d" & p - 1), Order1:=xlAscending, Header:=xlNo
    End With
    Dim ee%, color1%
    Set dd = CreateObject("scripting.dictionary")
    S = Cells(a, 1).Offset(-1, 0).Row()
    For ee = p To S
        dt1 = Format(Cells(ee, 4), "yyyymmdd")
        If dd.Exists(dt1) Then
            Set dd(dt1) = Union(dd(dt1), Cells(ee, 1).Resize(1, 11))
        Else
            Set dd(dt1) = Cells(ee, 1).Resize(1, 11)
        End If
    Next
    For jj = 0 To dd.Count - 1
        dd.items()(jj).Interior.ColorIndex = jj + 37 Mod 56
    Next jj
   
    Set MRG = Range("M:M").Find(y)
    If MRG Is Nothing Then
        Range("a1").CurrentRegion.Select
        With Selection
            .Sort Key1:=Range("d1"), Order1:=xlAscending, Header:=xlYes
        End With
        g = 2
        h = Range("a1").End(xlDown).Row
        Dim e%, color%
        Set d = CreateObject("scripting.dictionary")
        For e = g To h
            dt = Format(Cells(e, 4), "yyyymmdd")
            If d.Exists(dt) Then
                Set d(dt) = Union(d(dt), Cells(e, 1).Resize(1, 11))
            Else
                Set d(dt) = Cells(e, 1).Resize(1, 11)
            End If
        Next
        For j = 0 To d.Count - 1
            d.items()(j).Interior.ColorIndex = j + 37 Mod 56
        Next j
    End If
    End Sub

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

本版积分规则

关闭

最新热点上一条 /1 下一条

手机版|关于我们|联系我们|ExcelHome

GMT+8, 2024-4-19 01:39 , Processed in 0.038455 second(s), 12 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 1999-2023 Wooffice Inc.

沪公网安备 31011702000001号 沪ICP备11019229号-2

本论坛言论纯属发表者个人意见,任何违反国家相关法律的言论,本站将协助国家相关部门追究发言者责任!     本站特聘法律顾问:李志群律师

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