Excel精英培训网

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

[已解决]这样的筛选复制要怎样改啊?请朋友帮忙看看吧!!

[复制链接]
发表于 2014-10-28 13:11 | 显示全部楼层 |阅读模式
这是我记的一个流水账,想在里面设计一个按键(里面有一个按键是符合条件变色用的),如果表1的L1单元格输入的是“邓发财”,那么复制表1中含有邓发财的项到表3(像我表2手动筛选复制的一样),请高手朋友给看看吧,谢谢啦 求助流水账记录.rar (19.69 KB, 下载次数: 7)
excel精英培训的微信平台,每天都会发送excel学习教程和资料。扫一扫明天就可以收到新教程
发表于 2014-10-28 14:51 | 显示全部楼层
表格设计不太好,不需要使用到合并单元格,另外,同一个单元格里其实也是可以换行来实现的。
回复

使用道具 举报

发表于 2014-10-28 15:16 | 显示全部楼层
  1. Sub test2()
  2.     Dim str$, arr, result()
  3.     Dim i&, j&, k&, lStart&, lEnd&, lTemp&, l&
  4.     str = Sheet6.Range("l1").Value
  5.     If Len(str) = 0 Then
  6.         MsgBox "查找内容为空"
  7.         Exit Sub
  8.     End If
  9.     arr = Sheet6.Range("a1").CurrentRegion.Value
  10.     If Not IsArray(arr) Then
  11.         MsgBox "无数据可操作"
  12.         Exit Sub
  13.     End If
  14.     ReDim result(1 To UBound(arr), 1 To 7)
  15.     For i = LBound(arr) + 1 To UBound(arr)
  16.         If InStr(1, arr(i, 4), str) Then
  17.             lStart = i
  18.             lEnd = i
  19.             '起始行
  20.             If Len(arr(i, 1)) Then
  21.                 lStart = i
  22.             Else
  23.                 Do
  24.                     lStart = lStart - 1
  25.                 Loop Until Len(arr(lStart, 1))
  26.             End If

  27.             If i < UBound(arr) Then
  28.                 If Len(arr(i + 1, 1)) = 0 Then
  29.                     Do While Len(arr(lEnd, 1)) > 0
  30.                         lEnd = lEnd + 1
  31.                     Loop
  32.                 End If
  33.                 Debug.Print i, lStart, lEnd
  34.             Else
  35.                 If i = UBound(arr) Then
  36.                     lStart = i
  37.                     If Len(arr(i, 1)) = 0 Then
  38.                         Do
  39.                             lStart = lStart - 1
  40.                         Loop Until Len(arr(lStart, 1))
  41.                     Else
  42.                         lStart = i
  43.                     End If
  44.                 End If
  45.                 Debug.Print i, lStart, lEnd
  46.             End If
  47.             For lTemp = lStart To lEnd
  48.                 k = k + 1
  49.                 For l = 1 To 4
  50.                     result(k, l) = arr(lTemp, l)
  51.                 Next
  52.                 For l = 10 To 12
  53.                     result(k, l - 5) = arr(lTemp, l)
  54.                 Next
  55.             Next
  56.             i = lEnd
  57.         End If
  58.     Next
  59.     Application.ScreenUpdating = False
  60.     Sheet1.Cells(Rows.Count, 1).End(xlUp).Resize(k, UBound(result, 2)).Value = result
  61.     Sheet1.Columns("a:g").AutoFit
  62.     MsgBox "筛选完成"
  63. End Sub
复制代码
回复

使用道具 举报

发表于 2014-10-28 15:25 | 显示全部楼层    本楼为最佳答案   
本帖最后由 hwc2ycy 于 2014-10-28 15:32 编辑
  1. Sub test3()
  2.     Dim str$, arr, result()
  3.     Dim i&, j&, k&, lStart&, lEnd&, lTemp&, l&
  4.     str = Sheet6.Range("l1").Value
  5.     If Len(str) = 0 Then
  6.         MsgBox "查找内容为空"
  7.         Exit Sub
  8.     End If
  9.     arr = Sheet6.Range("a1").CurrentRegion.Value
  10.     If Not IsArray(arr) Then
  11.         MsgBox "无数据可操作"
  12.         Exit Sub
  13.     End If
  14.     ReDim result(1 To UBound(arr), 1 To 7)
  15.     For i = LBound(arr) + 1 To UBound(arr)
  16.         If InStr(1, arr(i, 4), str) Then
  17.             lStart = i
  18.             lEnd = i
  19.             Do While Len(arr(lStart, 1)) = 0
  20.                 lStart = lStart - 1
  21.             Loop
  22.             If lEnd < UBound(arr) Then
  23.                 Do While Len(arr(lEnd + 1, 1)) = 0
  24.                     lEnd = lEnd + 1
  25.                     If lEnd = UBound(arr) Then
  26.                         Exit Do
  27.                     End If
  28.                 Loop
  29.             Else
  30.                 lEnd = i
  31.             End If

  32.             Debug.Print i, lStart, lEnd
  33.             For lTemp = lStart To lEnd
  34.                 k = k + 1
  35.                 For l = 1 To 4
  36.                     result(k, l) = arr(lTemp, l)
  37.                 Next
  38.                 For l = 10 To 12
  39.                     result(k, l - 5) = arr(lTemp, l)
  40.                 Next
  41.             Next
  42.             i = lEnd
  43.         End If
  44.     Next
  45.     Application.ScreenUpdating = False
  46.     Sheet1.Cells(Rows.Count, 4).End(xlUp).Offset(1, -3).Resize(k, UBound(result, 2)).Value = result
  47.     Sheet1.Columns("a:g").AutoFit
  48.     MsgBox "筛选完成"
  49. End Sub
复制代码
回复

使用道具 举报

发表于 2014-10-28 15:34 | 显示全部楼层
求助流水账记录.rar (27.28 KB, 下载次数: 7)
回复

使用道具 举报

 楼主| 发表于 2014-10-28 16:35 | 显示全部楼层
hwc2ycy 发表于 2014-10-28 15:34

谢谢版主!这么快就给问题解决了!!!
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-4-27 00:36 , Processed in 0.330303 second(s), 10 queries , Gzip On, Yac On.

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

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