ExcelHome技术论坛

 找回密码
 免费注册

QQ登录

只需一步,快速开始

快捷登录

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

[求助] 如何修改代码,使其更快速计算?

[复制链接]

TA的精华主题

TA的得分主题

发表于 2017-1-13 21:00 | 显示全部楼层 |阅读模式
Sub 填色()
Dim i%
With Application
.ScreenUpdating = False
    For i = 1 To 31
    If .Cells(6, i + 6) <> "" Then
        If Weekday(.Cells(6, i + 6), 2) > 5 And .Cells(5, i + 6) = "" Or .Cells(5, i + 6) = "放假" Then
           Range(Cells(7, i + 6), Cells(116, i + 6)).Interior.Color = RGB(141, 180, 226)
        Else
           Range(Cells(7, i + 6), Cells(116, i + 6)).Interior.Color = RGB(255, 255, 255)
        End If
        If Cells(5, i + 6) = "法定假" Then
           Cells(7, i + 6).Resize(110, 1).Interior.Color = RGB(218, 150, 148)
        End If
    Else
       Range(Cells(7, i + 6), Cells(116, i + 6)).Interior.Color = RGB(255, 255, 255)
    End If
    For r = 1 To 110
        If .Cells(r + 6, 4) = "5★" Then
           Range(Cells(r + 6, 1), Cells(r + 6, 6)).Font.Color = RGB(49, 134, 155)
        End If
        If .Cells(r + 6, 4) = "4★S" Then
           Range(Cells(r + 6, 1), Cells(r + 6, 6)).Font.Color = RGB(192, 80, 77)
        End If
        If .Cells(r + 6, 4) = "4★J" Then
           Range(Cells(r + 6, 1), Cells(r + 6, 6)).Font.Color = RGB(255, 0, 0)
        End If
        If .Cells(r + 6, 4) = "3★S" Then
           Range(Cells(r + 6, 1), Cells(r + 6, 6)).Font.Color = RGB(0, 128, 0)
        End If
        If .Cells(r + 6, 4) = "3★J" Then
           Range(Cells(r + 6, 1), Cells(r + 6, 6)).Font.Color = RGB(255, 0, 255)
        End If
        If .Cells(r + 6, 4) = "2★S" Then
           Range(Cells(r + 6, 1), Cells(r + 6, 6)).Font.Color = RGB(226, 107, 10)
        End If
        If .Cells(r + 6, 4) = "2★J" Then
           Range(Cells(r + 6, 1), Cells(r + 6, 6)).Font.Color = RGB(0, 0, 255)
        End If
        If .Cells(r + 6, 4) = "1★" Then
           Range(Cells(r + 6, 1), Cells(r + 6, 6)).Font.Color = RGB(0, 0, 0)
        End If
     Next r
    Next i
End With
SendKeys "{F9}"
End Sub
  1. Sub 填色()
  2. Dim i%
  3. With Application
  4. .ScreenUpdating = False
  5. For i = 1 To 31
  6. If .Cells(6, i + 6) <> "" Then
  7. If Weekday(.Cells(6, i + 6), 2) > 5 And .Cells(5, i + 6) = "" Or .Cells(5, i + 6) = "放假" Then
  8. Range(Cells(7, i + 6), Cells(116, i + 6)).Interior.Color = RGB(141, 180, 226)
  9. Else
  10. Range(Cells(7, i + 6), Cells(116, i + 6)).Interior.Color = RGB(255, 255, 255)
  11. End If
  12. If Cells(5, i + 6) = "法定假" Then
  13. Cells(7, i + 6).Resize(110, 1).Interior.Color = RGB(218, 150, 148)
  14. End If
  15. Else
  16. Range(Cells(7, i + 6), Cells(116, i + 6)).Interior.Color = RGB(255, 255, 255)
  17. End If
  18. For r = 1 To 110
  19. If .Cells(r + 6, 4) = "5★" Then
  20. Range(Cells(r + 6, 1), Cells(r + 6, 6)).Font.Color = RGB(49, 134, 155)
  21. End If
  22. If .Cells(r + 6, 4) = "4★S" Then
  23. Range(Cells(r + 6, 1), Cells(r + 6, 6)).Font.Color = RGB(192, 80, 77)
  24. End If
  25. If .Cells(r + 6, 4) = "4★J" Then
  26. Range(Cells(r + 6, 1), Cells(r + 6, 6)).Font.Color = RGB(255, 0, 0)
  27. End If
  28. If .Cells(r + 6, 4) = "3★S" Then
  29. Range(Cells(r + 6, 1), Cells(r + 6, 6)).Font.Color = RGB(0, 128, 0)
  30. End If
  31. If .Cells(r + 6, 4) = "3★J" Then
  32. Range(Cells(r + 6, 1), Cells(r + 6, 6)).Font.Color = RGB(255, 0, 255)
  33. End If
  34. If .Cells(r + 6, 4) = "2★S" Then
  35. Range(Cells(r + 6, 1), Cells(r + 6, 6)).Font.Color = RGB(226, 107, 10)
  36. End If
  37. If .Cells(r + 6, 4) = "2★J" Then
  38. Range(Cells(r + 6, 1), Cells(r + 6, 6)).Font.Color = RGB(0, 0, 255)
  39. End If
  40. If .Cells(r + 6, 4) = "1★" Then
  41. Range(Cells(r + 6, 1), Cells(r + 6, 6)).Font.Color = RGB(0, 0, 0)
  42. End If
  43. Next r
  44. Next i
  45. End With
  46. SendKeys "{F9}"
  47. End Sub
复制代码

TA的精华主题

TA的得分主题

发表于 2017-1-13 21:30 来自手机 | 显示全部楼层
If .Cells(r + 6, 4) = "5★" Then
Range(Cells(r + 6, 1), Cells(r + 6, 6)).Font.Color = RGB(49, 134, 155)
elseIf  .Cells(r + 6, 4) = "4★S" Then
Range(Cells(r + 6, 1), Cells(r + 6, 6)).Font.Color = RGB(192, 80, 77)
End If

用elseif串联起来,对于同一个单元格判断

评分

1

查看全部评分

TA的精华主题

TA的得分主题

 楼主| 发表于 2017-1-13 23:39 来自手机 | 显示全部楼层
Edifier_1989 发表于 2017-1-13 21:30
If .Cells(r + 6, 4) = "5★" Then
Range(Cells(r + 6, 1), Cells(r + 6, 6)).Font.Color = RGB(49, 134,  ...

您这是不是没有写完整呀?我是不是该接着写?

TA的精华主题

TA的得分主题

发表于 2017-1-14 07:24 来自手机 | 显示全部楼层
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用  · 内置多项VBA编程加强工具       ★ 免费下载 ★      ★使用手册
是的,下面也要改

TA的精华主题

TA的得分主题

发表于 2017-1-14 07:28 | 显示全部楼层
sampsonpon 发表于 2017-1-13 23:39
您这是不是没有写完整呀?我是不是该接着写?

至少可以这样吧:
For r = 1 To 110
        If .Cells(r + 6, 4) = "5★" Then
           Range(Cells(r + 6, 1), Cells(r + 6, 6)).Font.Color = RGB(49, 134, 155)
        elseIf .Cells(r + 6, 4) = "4★S" Then
           Range(Cells(r + 6, 1), Cells(r + 6, 6)).Font.Color = RGB(192, 80, 77)
        elseIf .Cells(r + 6, 4) = "4★J" Then
           Range(Cells(r + 6, 1), Cells(r + 6, 6)).Font.Color = RGB(255, 0, 0)
       elseIf .Cells(r + 6, 4) = "3★S" Then
           Range(Cells(r + 6, 1), Cells(r + 6, 6)).Font.Color = RGB(0, 128, 0)
        elseIf .Cells(r + 6, 4) = "3★J" Then
           Range(Cells(r + 6, 1), Cells(r + 6, 6)).Font.Color = RGB(255, 0, 255)
        elseIf .Cells(r + 6, 4) = "2★S" Then
           Range(Cells(r + 6, 1), Cells(r + 6, 6)).Font.Color = RGB(226, 107, 10)
        elseIf .Cells(r + 6, 4) = "2★J" Then
           Range(Cells(r + 6, 1), Cells(r + 6, 6)).Font.Color = RGB(0, 0, 255)
        else
           Range(Cells(r + 6, 1), Cells(r + 6, 6)).Font.Color = RGB(0, 0, 0)
        End If

评分

1

查看全部评分

TA的精华主题

TA的得分主题

发表于 2017-1-14 07:49 | 显示全部楼层
好像没必要嵌套循环吧,去掉嵌套循环应该快不少

TA的精华主题

TA的得分主题

 楼主| 发表于 2017-1-14 10:06 来自手机 | 显示全部楼层
yangyangzhifeng 发表于 2017-1-14 07:49
好像没必要嵌套循环吧,去掉嵌套循环应该快不少

不嵌套的话,代码如何写?

TA的精华主题

TA的得分主题

发表于 2017-1-14 10:11 | 显示全部楼层
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件       ★免费下载 ★       ★ 使用帮助
sampsonpon 发表于 2017-1-14 10:06
不嵌套的话,代码如何写?

你没上传附件,所以大家不好处理。

TA的精华主题

TA的得分主题

发表于 2017-1-14 10:17 | 显示全部楼层
sampsonpon 发表于 2017-1-14 10:06
不嵌套的话,代码如何写?

直接把for r循环拉到外面不行吗?代码并没有赋值语句,两段循环没有倚赖关系吧

TA的精华主题

TA的得分主题

 楼主| 发表于 2017-1-14 10:32 来自手机 | 显示全部楼层
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件       ★免费下载 ★       ★ 使用帮助
lsc900707 发表于 2017-1-14 10:11
你没上传附件,所以大家不好处理。

今天休息没有上班,明天传个附件给您看看。谢谢您!
您需要登录后才可以回帖 登录 | 免费注册

本版积分规则

关闭

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

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

GMT+8, 2024-4-20 12:11 , Processed in 0.052119 second(s), 10 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 1999-2023 Wooffice Inc.

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

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

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