Excel精英培训网

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

[已解决]EXCEL数提取的问题

[复制链接]
发表于 2011-8-14 00:34 | 显示全部楼层 |阅读模式
请各位朋友帮帮忙,我想通过在查询表中输入责任人的姓名查询出与该责任人相关的所有业务单据,不知如何来做,现将数据传上来希望各位不吝赐教,谢谢了!
最佳答案
2011-8-14 00:58
  1. Private Sub Worksheet_Change(ByVal Target As Range)
  2. Dim iCnt As Integer
  3. Dim sNme As String
  4. Dim iCur As Integer
  5. Dim iMaxRw As Integer
  6. If Target.Row = 1 And Target.Column = 2 Then
  7.     sNme = Sheet1.Cells(1, 2).Value
  8.     iCur = 3
  9.     Sheet1.Range("A1").CurrentRegion.Offset(2, 0).Clear
  10.     iMaxRw = Sheet2.Range("B65536").End(xlUp).Row
  11.         For iCnt = 1 To iMaxRw
  12.         If Sheet2.Cells(iCnt, 8) = sNme Then
  13.             Sheet1.Cells(iCur, 1).Value = Sheet2.Cells(iCnt, 2).Value
  14.             Sheet1.Cells(iCur, 2).Value = Sheet2.Cells(iCnt, 3).Value
  15.             Sheet1.Cells(iCur, 3).Value = Sheet2.Cells(iCnt, 4).Value
  16.             Sheet1.Cells(iCur, 4).Value = Sheet2.Cells(iCnt, 5).Value
  17.             Sheet1.Cells(iCur, 5).Value = Sheet2.Cells(iCnt, 6).Value
  18.             Sheet1.Cells(iCur, 6).Value = Sheet2.Cells(iCnt, 7).Value
  19.             Sheet1.Cells(iCur, 7).Value = Sheet2.Cells(iCnt, 9).Value
  20.             Sheet1.Cells(iCur, 8).Value = Sheet2.Cells(iCnt, 10).Value
  21.             iCur = iCur + 1
  22.         End If
  23.         Next
  24. End If
  25. End Sub
复制代码
将这段代码放到sheet1的Worksheet_Change事件中。

PS:查询里的I列也是“科目”是否重复?
我这里没有给其添加内容

数据.rar

58.04 KB, 下载次数: 25

发表于 2011-8-14 00:58 | 显示全部楼层    本楼为最佳答案   
  1. Private Sub Worksheet_Change(ByVal Target As Range)
  2. Dim iCnt As Integer
  3. Dim sNme As String
  4. Dim iCur As Integer
  5. Dim iMaxRw As Integer
  6. If Target.Row = 1 And Target.Column = 2 Then
  7.     sNme = Sheet1.Cells(1, 2).Value
  8.     iCur = 3
  9.     Sheet1.Range("A1").CurrentRegion.Offset(2, 0).Clear
  10.     iMaxRw = Sheet2.Range("B65536").End(xlUp).Row
  11.         For iCnt = 1 To iMaxRw
  12.         If Sheet2.Cells(iCnt, 8) = sNme Then
  13.             Sheet1.Cells(iCur, 1).Value = Sheet2.Cells(iCnt, 2).Value
  14.             Sheet1.Cells(iCur, 2).Value = Sheet2.Cells(iCnt, 3).Value
  15.             Sheet1.Cells(iCur, 3).Value = Sheet2.Cells(iCnt, 4).Value
  16.             Sheet1.Cells(iCur, 4).Value = Sheet2.Cells(iCnt, 5).Value
  17.             Sheet1.Cells(iCur, 5).Value = Sheet2.Cells(iCnt, 6).Value
  18.             Sheet1.Cells(iCur, 6).Value = Sheet2.Cells(iCnt, 7).Value
  19.             Sheet1.Cells(iCur, 7).Value = Sheet2.Cells(iCnt, 9).Value
  20.             Sheet1.Cells(iCur, 8).Value = Sheet2.Cells(iCnt, 10).Value
  21.             iCur = iCur + 1
  22.         End If
  23.         Next
  24. End If
  25. End Sub
复制代码
将这段代码放到sheet1的Worksheet_Change事件中。

PS:查询里的I列也是“科目”是否重复?
我这里没有给其添加内容

回复

使用道具 举报

发表于 2011-8-14 03:23 | 显示全部楼层
本帖最后由 fjmxwrs 于 2011-8-14 03:26 编辑
  1. Private Sub Worksheet_Change(ByVal Target As Range)
  2. Dim arr, arr1(), y&, x&, i&
  3. If Target.Address = "$B$1" Then
  4. With Sheets("数据")
  5. arr = .Range("A2:J" & .Range("A65536").End(xlUp).Row).Value
  6. End With
  7. For x = 1 To UBound(arr)
  8. If arr(x, 8) = Target.Value Then
  9. i = i + 1
  10. ReDim Preserve arr1(1 To 9, 1 To i)
  11. For y = 1 To 7
  12. arr1(y, i) = arr(x, y)
  13. Next y
  14. arr1(8, i) = arr(x, 9)
  15. arr1(9, i) = arr(x, 10)
  16. End If
  17. Next x
  18. Range("A3").Resize(65534, 9).ClearContents
  19. Range("A3").Resize(65534, 9).Borders.LineStyle = 0
  20. Range("A3").Resize(UBound(arr1, 2), UBound(arr1)) = Application.Transpose(arr1)
  21. Range("A3").Resize(UBound(arr1, 2), UBound(arr1)).Borders.LineStyle = 1
  22. End If
  23. End Sub

  24. Private Sub Worksheet_SelectionChange(ByVal Target As Range)
  25. Dim arr, d As Object
  26. Set d = CreateObject("scripting.dictionary")
  27. If Target.Address = "$B$1" Then
  28. With Sheets("数据")
  29. arr = .Range("H2:H" & .Range("H65536").End(xlUp).Row)
  30. End With
  31. For x = 1 To UBound(arr)
  32. d(arr(x, 1)) = ""
  33. Next x
  34. With Selection.Validation
  35. .Delete
  36. .Add Type:=xlValidateList, Formula1:=Join(d.keys, ",")
  37. End With
  38. End If
  39. End Sub
复制代码
pn.rar (71.24 KB, 下载次数: 11)
回复

使用道具 举报

 楼主| 发表于 2011-8-14 09:29 | 显示全部楼层
二楼、三楼的朋友做得都很好,我都试过了。谢谢你们。
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-5-12 02:04 , Processed in 0.187267 second(s), 6 queries , Gzip On, Yac On.

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

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