Excel精英培训网

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

[已解决]Worksheet_SelectionChange求优化代码

[复制链接]
发表于 2013-2-26 17:08 | 显示全部楼层 |阅读模式
  1. Private Sub Worksheet_SelectionChange(ByVal Target As Range)
  2. Dim x%, y&, z&, br, ran$, ar
  3. ran = Target.Range("d6:d10", "e6:e10").Column
  4. br = Sheet2.Range("a65536").End(xlUp).Row
  5. With Sheet2
  6. For x = 6 To 10
  7. For y = 3 To br
  8. ar = .Cells(y, 256).End(3).Column
  9. For z = 19 To ar Step 8
  10. If Cells(x, 4) = .Cells(y, 4) Then
  11. Cells(x, 2) = .Cells(y, 5)
  12. Cells(x, 3) = .Cells(y, 6)
  13. Cells(x, 8) = .Cells(y, 9)
  14. Cells(x, 9) = .Cells(y, 10)
  15. Cells(x, 10) = .Cells(y, 11)
  16. End If
  17. If Cells(x, 4) = .Cells(y, 4) And .Cells(y, z) <> "" Then
  18. Cells(x, 5) = .Cells(y, z)
  19. End If
  20. Next z
  21. If Cells(x, 4) = .Cells(y, 4) And .Cells(y, 15) = "" Then
  22. Cells(x, 5) = .Cells(y, 7)

  23. End If
  24. Next y
  25. If Range("F" & x) > 0 Then
  26. Range("G" & x) = Range("F" & x) / 20
  27. End If
  28. Next x
  29. End With
  30. End Sub
复制代码
Target.zip (17.11 KB, 下载次数: 6)
excel精英培训的微信平台,每天都会发送excel学习教程和资料。扫一扫明天就可以收到新教程
发表于 2013-2-26 17:29 | 显示全部楼层
出库件数500是怎么来的。表里没看出来。
回复

使用道具 举报

 楼主| 发表于 2013-2-26 17:38 | 显示全部楼层
hwc2ycy 发表于 2013-2-26 17:29
出库件数500是怎么来的。表里没看出来。

是需要出库的件数,需要多少,输入多少,不能大于总库存。
回复

使用道具 举报

发表于 2013-2-26 17:47 | 显示全部楼层
是当在D列输入车号时,其他列的数据自动显示出来,F列输入的内容必需小于库存数,否则就清空F列么?
回复

使用道具 举报

 楼主| 发表于 2013-2-26 17:51 | 显示全部楼层
hwc2ycy 发表于 2013-2-26 17:47
是当在D列输入车号时,其他列的数据自动显示出来,F列输入的内容必需小于库存数,否则就清空F列么?

也可以清空,也可以不清空,因为在E列看得见总库存数,不会把出库数大于总库存数。
回复

使用道具 举报

发表于 2013-2-26 19:30 | 显示全部楼层
你这吨位列也是错的啊。
回复

使用道具 举报

发表于 2013-2-26 19:33 | 显示全部楼层
选择事件不建议这样使用,最好限制一下选择的范围,不然,随便选择一个单元格都要计算一下,

要是公司的电脑累死了还没事,自己的电脑累死了就亏大了!!

点评

电脑就是用来累的,累死了换新的。  发表于 2013-2-26 20:01
回复

使用道具 举报

 楼主| 发表于 2013-2-26 19:43 | 显示全部楼层
hwc2ycy 发表于 2013-2-26 19:30
你这吨位列也是错的啊。

没有错啊, If Range("F" & x) > 0 Then                    Range("G" & x) = Range("F" & x) / 20每件重量50KG,1吨就是20件,除下来就是吨位了



回复

使用道具 举报

发表于 2013-2-26 19:54 | 显示全部楼层    本楼为最佳答案   
  1. Private Sub Worksheet_SelectionChange(ByVal Target As Range)

  2.     Dim rg As Range
  3.     Dim arr(1 To 9)
  4.     Dim arrPos, i
  5.     arrPos = Array(5, 6, 4, 7, 1, 8, 9, 10, 11)
  6.     If Target.Count > 1 Then Exit Sub
  7.     If Len(Target.Value) = 0 Then Exit Sub

  8.     Select Case Target.Column
  9.         
  10.         Case 4
  11.             '处理第4列
  12.             With Sheet2
  13.                 Set rg = .Columns("d").Find(what:=Target.Value, lookat:=xlWhole)
  14.                 If rg Is Nothing Then
  15.                     '清空当前行内容
  16.                     Cells(Target.Row, "b").Resize(, 9) = arr
  17.                     Exit Sub
  18.                 End If
  19.                 Application.EnableEvents = False
  20.                 irow = rg.Row
  21.                 For i = LBound(arrPos) To UBound(arrPos)
  22.                     arr(i + 1) = .Cells(irow, arrPos(i))
  23.                 Next
  24.             End With
  25.             '保留当前行的5,6列内容
  26.             arr(5) = Cells(Target.Row, "f")
  27.             arr(6) = Cells(Target.Row, "g")
  28.             Cells(Target.Row, "b").Resize(, 9) = arr

  29.         Case 6
  30.             '处理第6列
  31.             With Sheet2
  32.                 Set rg = .Columns("d").Find(what:=Target.Offset(, -2).Value, lookat:=xlWhole)
  33.                 If rg Is Nothing Then
  34.                     Cells(Target.Row, "b").Resize(, 9) = arr
  35.                     Exit Sub
  36.                 End If
  37.                 Application.EnableEvents = False
  38.                 irow = rg.Row
  39.                 For i = LBound(arrPos) To UBound(arrPos)
  40.                     arr(i + 1) = .Cells(irow, arrPos(i))
  41.                 Next
  42.             End With
  43.             arr(5) = Cells(Target.Row, "f")
  44.             arr(6) = Cells(Target.Row, "g")
  45.             Application.EnableEvents = False
  46.             Cells(Target.Row, "b").Resize(, 9) = arr
  47.         Case Else
  48.             Exit Sub
  49.     End Select
  50.     Application.EnableEvents = True
  51. End Sub
复制代码
回复

使用道具 举报

发表于 2013-2-26 19:58 | 显示全部楼层
本帖最后由 hwc2ycy 于 2013-2-26 19:59 编辑
  1. Private Sub Worksheet_Change(ByVal Target As Range)
  2.     Dim rg As Range
  3.     Dim arr(1 To 9)
  4.     Dim arrPos, i As Byte
  5.     arrPos = Array(5, 6, 4, 7, 1, 8, 9, 10, 11)
  6.     If Target.Count > 1 Then Exit Sub
  7.     If Len(Target.Value) = 0 Then Exit Sub

  8.     Select Case Target.Column
  9.         
  10.         Case 4
  11.             '处理第4列
  12.             With Sheet2
  13.                 Set rg = .Columns("d").Find(what:=Target.Value, lookat:=xlWhole)
  14.                 If rg Is Nothing Then
  15.                     '清空当前行内容
  16.                     Cells(Target.Row, "b").Resize(, 9) = arr
  17.                     Exit Sub
  18.                 End If
  19.                 irow = rg.Row
  20.                 For i = LBound(arrPos) To UBound(arrPos)
  21.                     arr(i + 1) = .Cells(irow, arrPos(i))
  22.                 Next
  23.             End With
  24.             '保留当前行的5,6列内容
  25.             arr(5) = Cells(Target.Row, "f")
  26.             arr(6) = Cells(Target.Row, "g")
  27.             Cells(Target.Row, "b").Resize(, 9) = arr

  28.         Case 6
  29.             '处理第6列
  30.             With Sheet2
  31.                 Set rg = .Columns("d").Find(what:=Target.Offset(, -2).Value, lookat:=xlWhole)
  32.                 If rg Is Nothing Then
  33.                     Cells(Target.Row, "b").Resize(, 9) = arr
  34.                     Exit Sub
  35.                 End If
  36.                 irow = rg.Row
  37.                 For i = LBound(arrPos) To UBound(arrPos)
  38.                     arr(i + 1) = .Cells(irow, arrPos(i))
  39.                 Next
  40.             End With
  41.             arr(5) = Cells(Target.Row, "f")
  42.             arr(6) = Cells(Target.Row, "g")
  43.             Cells(Target.Row, "b").Resize(, 9) = arr
  44.         Case Else
  45.             Exit Sub
  46.     End Select
  47. End Sub

  48. Private Sub Worksheet_SelectionChange(ByVal Target As Range)

  49.     Dim rg As Range
  50.     Dim arr(1 To 9)
  51.     Dim arrPos, i As Byte
  52.     arrPos = Array(5, 6, 4, 7, 1, 8, 9, 10, 11)
  53.     If Target.Count > 1 Then Exit Sub
  54.     If Len(Target.Value) = 0 Then Exit Sub

  55.     Select Case Target.Column
  56.         
  57.         Case 4
  58.             '处理第4列
  59.             With Sheet2
  60.                 Set rg = .Columns("d").Find(what:=Target.Value, lookat:=xlWhole)
  61.                 If rg Is Nothing Then
  62.                     '清空当前行内容
  63.                     Cells(Target.Row, "b").Resize(, 9) = arr
  64.                     Exit Sub
  65.                 End If
  66.                 'Application.EnableEvents = False
  67.                 irow = rg.Row
  68.                 For i = LBound(arrPos) To UBound(arrPos)
  69.                     arr(i + 1) = .Cells(irow, arrPos(i))
  70.                 Next
  71.             End With
  72.             '保留当前行的5,6列内容
  73.             arr(5) = Cells(Target.Row, "f")
  74.             arr(6) = Cells(Target.Row, "g")
  75.             Cells(Target.Row, "b").Resize(, 9) = arr

  76.         Case 6
  77.             '处理第6列
  78.             With Sheet2
  79.                 Set rg = .Columns("d").Find(what:=Target.Offset(, -2).Value, lookat:=xlWhole)
  80.                 If rg Is Nothing Then
  81.                     Cells(Target.Row, "b").Resize(, 9) = arr
  82.                     Exit Sub
  83.                 End If
  84.                 irow = rg.Row
  85.                 For i = LBound(arrPos) To UBound(arrPos)
  86.                     arr(i + 1) = .Cells(irow, arrPos(i))
  87.                 Next
  88.             End With
  89.             arr(5) = Cells(Target.Row, "f")
  90.             arr(6) = Cells(Target.Row, "g")
  91.             Cells(Target.Row, "b").Resize(, 9) = arr
  92.         Case Else
  93.             Exit Sub
  94.     End Select
  95. End Sub
复制代码
其实监视 Change事件是否更好了。

回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-4-20 08:14 , Processed in 0.372898 second(s), 11 queries , Gzip On, Yac On.

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

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