ExcelHome技术论坛

 找回密码
 免费注册

QQ登录

只需一步,快速开始

快捷登录

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

[求助] 求助帮忙看下这些代码有办法简化吗?

[复制链接]

TA的精华主题

TA的得分主题

发表于 2018-10-18 21:00 | 显示全部楼层 |阅读模式
本帖最后由 小年玩卡 于 2018-10-18 22:00 编辑

Private Sub Worksheet_Change(ByVal Target As Range)
Dim i&
If Target.Column <= 7 Then

   If Target.Row > 5 And Target.Row <= 52 Then
      For i = 6 To 52
         Cells(i, 12) = Cells(i, 2) - Cells(i, 2) * Cells(i, 6) - Cells(i, 7)
         Cells(i, 11) = Cells(i, 2) - Cells(i, 12)
      Next
   End If


   If Target.Row > 53 And Target.Row <= 100 Then
      For i = 54 To 100
         Cells(i, 12) = Cells(i, 2) - Cells(i, 2) * Cells(i, 6) - Cells(i, 7)
         Cells(i, 11) = Cells(i, 2) - Cells(i, 12)
      Next
   End If
If Target.Row > 101 And Target.Row <= 148 Then
      For i = 102 To 148
         Cells(i, 12) = Cells(i, 2) - Cells(i, 2) * Cells(i, 6) - Cells(i, 7)
         Cells(i, 11) = Cells(i, 2) - Cells(i, 12)
      Next
   End If
If Target.Row > 149 And Target.Row <= 196 Then
      For i = 150 To 196
         Cells(i, 12) = Cells(i, 2) - Cells(i, 2) * Cells(i, 6) - Cells(i, 7)
         Cells(i, 11) = Cells(i, 2) - Cells(i, 12)
      Next
   End If
If Target.Row > 245 And Target.Row <= 293 Then
      For i = 246 To 292
         Cells(i, 12) = Cells(i, 2) - Cells(i, 2) * Cells(i, 6) - Cells(i, 7)
         Cells(i, 11) = Cells(i, 2) - Cells(i, 12)
      Next
   End If
If Target.Row > 293 And Target.Row <= 340 Then
      For i = 294 To 340
         Cells(i, 12) = Cells(i, 2) - Cells(i, 2) * Cells(i, 6) - Cells(i, 7)
         Cells(i, 11) = Cells(i, 2) - Cells(i, 12)
      Next
   End If
If Target.Row > 341 And Target.Row <= 388 Then
      For i = 342 To 388
         Cells(i, 12) = Cells(i, 2) - Cells(i, 2) * Cells(i, 6) - Cells(i, 7)
         Cells(i, 11) = Cells(i, 2) - Cells(i, 12)
      Next
   End If
If Target.Row > 389 And Target.Row <= 436 Then
      For i = 390 To 436
         Cells(i, 12) = Cells(i, 2) - Cells(i, 2) * Cells(i, 6) - Cells(i, 7)
         Cells(i, 11) = Cells(i, 2) - Cells(i, 12)
      Next
   End If
If Target.Row > 437 And Target.Row <= 484 Then
      For i = 438 To 484
         Cells(i, 12) = Cells(i, 2) - Cells(i, 2) * Cells(i, 6) - Cells(i, 7)
         Cells(i, 11) = Cells(i, 2) - Cells(i, 12)
      Next
   End If
If Target.Row > 485 And Target.Row <= 532 Then

      For i = 486 To 532
         Cells(i, 12) = Cells(i, 2) - Cells(i, 2) * Cells(i, 6) - Cells(i, 7)
         Cells(i, 11) = Cells(i, 2) - Cells(i, 12)
      Next
   End If
If Target.Row > 533 And Target.Row <= 580 Then
      For i = 534 To 580
         Cells(i, 12) = Cells(i, 2) - Cells(i, 2) * Cells(i, 6) - Cells(i, 7)
         Cells(i, 11) = Cells(i, 2) - Cells(i, 12)
      Next
   End If

End Sub

能简化吗?现在循环下来计算也变慢了,如何优化?

TA的精华主题

TA的得分主题

发表于 2018-10-18 21:15 | 显示全部楼层
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件       ★免费下载 ★       ★ 使用帮助
呃…………
各种参数都是写死的,不好改吧。
另外不知道需求,没附件,只看代码也确定不了

TA的精华主题

TA的得分主题

 楼主| 发表于 2018-10-18 21:26 | 显示全部楼层
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件       ★免费下载 ★       ★ 使用帮助
Mapleaf12138 发表于 2018-10-18 21:15
呃…………
各种参数都是写死的,不好改吧。
另外不知道需求,没附件,只看代码也确定不了

那可能简化不了,要的就是这个固定单元格对固定单元格的效果

TA的精华主题

TA的得分主题

 楼主| 发表于 2018-10-18 22:00 | 显示全部楼层

TA的精华主题

TA的得分主题

发表于 2018-10-18 23:12 | 显示全部楼层
'模块写个子过程
Sub js(a, b)
Application.EnableEvents = False   '调用这个子过程的时候暂时关闭工作表的change事件
Dim i
      For i = a To b
         Cells(i, 12) = Cells(i, 2) - Cells(i, 2) * Cells(i, 6) - Cells(i, 7)
         Cells(i, 11) = Cells(i, 2) - Cells(i, 12)
      Next
Application.EnableEvents = True
End Sub

'工作表事件过程参考以下
Private Sub Worksheet_Change(ByVal Target As Range)
Dim i&, r
If Target.Column > 7 Then Exit Sub
r = Target.Row
      Select Case r
      Case 6 To 51
            Call js(6, 52)
      Case 54 To 100
            Call js(54, 100)
      Case 102 To 148
            Call js(102, 148)
      Case 150 To 196
            Call js(150, 196)
      Case 246 To 293
            Call js(246, 292)
      Case 294 To 340
            Call js(294, 340)
            '''''继续完成剩下的
            '''''
      End Select
End Sub

TA的精华主题

TA的得分主题

发表于 2018-10-18 23:26 | 显示全部楼层
n=(target.row-6)/48
target.rwo=52 ,n=0 ,0*48+5=5
target.row=100, n=1 ,1*48+5=53
target.row=148, n=2 ,2*48+5=101

TA的精华主题

TA的得分主题

 楼主| 发表于 2018-10-19 16:29 | 显示全部楼层
excelvlookup 发表于 2018-10-18 23:26
n=(target.row-6)/48
target.rwo=52 ,n=0 ,0*48+5=5
target.row=100, n=1 ,1*48+5=53

这个没有看懂

TA的精华主题

TA的得分主题

 楼主| 发表于 2018-10-19 16:31 | 显示全部楼层
yxbaju 发表于 2018-10-18 23:12
'模块写个子过程
Sub js(a, b)
Application.EnableEvents = False   '调用这个子过程的时候暂时关闭工作 ...

谢谢,这个代码能否提高运行速度,感觉每次循环完,反应慢了一点点

TA的精华主题

TA的得分主题

发表于 2018-10-19 17:46 | 显示全部楼层
变慢的最根本原因是用了Worksheet_Change事件,如改用Worksheet_SelectionChange事件会快很多,具体原因自己去想。
代码简化如下:没附件未测试
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Target.Column <= 7 Then
    ar = Array(5, 53, 101, 149, 245, 293, 341, 389, 437, 485, 533)
    br = Array(52, 100, 148, 196, 293, 340, 388, 436, 484, 532, 580)
    a = Target.Row
    For i = 0 To UBound(ar)
        If a > ar(i) And a <= br(i) Then
            q = ar(i): z = br(i)
            Exit For
        End If
    Next
    For i = q + 1 To z
        Cells(i, 12) = Cells(i, 2) - Cells(i, 2) * Cells(i, 6) - Cells(i, 7)
        Cells(i, 11) = Cells(i, 2) - Cells(i, 12)
    Next
End If
End Sub

TA的精华主题

TA的得分主题

 楼主| 发表于 2018-10-19 19:43 | 显示全部楼层
excelvlookup 发表于 2018-10-19 17:46
变慢的最根本原因是用了Worksheet_Change事件,如改用Worksheet_SelectionChange事件会快很多,具体原因自 ...

好的,非常感谢
您需要登录后才可以回帖 登录 | 免费注册

本版积分规则

关闭

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

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

GMT+8, 2024-4-25 13:01 , Processed in 0.042328 second(s), 10 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 1999-2023 Wooffice Inc.

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

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

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