Excel精英培训网

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

请教老师:如何删除指定列和数?

[复制链接]
发表于 2017-6-25 09:47 | 显示全部楼层 |阅读模式
请教各位老师:如何将指定列及指定的数据删除?同时保持数据的连接性。具体请见例子: 如何删除指定列和数?.rar (8.11 KB, 下载次数: 10)
发表于 2017-6-25 12:11 | 显示全部楼层
  1. Sub CSL()
  2. For y = 1 To 35
  3. If Cells(1, y) <> Sheet2.Cells(1, 1) And Cells(1, y) <> Sheet2.Cells(1, 2) And Cells(1, y) <> Sheet2.Cells(1, 3) And Cells(1, y) <> Sheet2.Cells(1, 4) And Cells(1, y) <> Sheet2.Cells(1, 5) And Cells(1, y) <> Sheet2.Cells(1, 6) And Cells(1, y) <> Sheet2.Cells(1, 7) And Cells(1, y) <> Sheet2.Cells(1, 8) Then
  4. If Cells(1, y) = "" Then
  5. y = 35
  6. Else

  7. Columns(y).Delete
  8. y = y - 1
  9. End If

  10. End If

  11. Next
  12. hs = Sheet1.UsedRange.Rows.Count
  13. For y1 = 1 To 8
  14. For x = 2 To hs
  15. If Cells(x, y1) <> Sheet2.Cells(1, 1) And Cells(x, y1) <> Sheet2.Cells(1, 2) And Cells(x, y1) <> Sheet2.Cells(1, 3) And Cells(x, y1) <> Sheet2.Cells(1, 4) And Cells(x, y1) <> Sheet2.Cells(1, 5) And Cells(x, y1) <> Sheet2.Cells(1, 6) And Cells(x, y1) <> Sheet2.Cells(1, 7) And Cells(x, y1) <> Sheet2.Cells(1, 8) Then

  16. If Cells(x, y1) = "" Then
  17. x = hs
  18. Else
  19. Range(Cells(x, y1), Cells(x, y1)).Select
  20. Selection.Delete Shift:=xlUp
  21. x = x - 1
  22. End If

  23. End If
  24. Next
  25. Next

  26. End Sub


复制代码

评分

参与人数 1 +1 收起 理由
lygyjt + 1 很给力

查看全部评分

回复

使用道具 举报

发表于 2017-6-25 12:12 | 显示全部楼层
回复

使用道具 举报

发表于 2017-6-26 11:50 | 显示全部楼层
  1. Sub aaa()
  2. Dim arr, i&, d As Object, rng As Range, c
  3. Set d = CreateObject("scripting.dictionary")
  4. arr = Sheets(2).[a1].CurrentRegion
  5. For i = 1 To UBound(arr, 2)
  6.   d(arr(1, i)) = ""
  7. Next i
  8. arr = Sheets(1).[a1].CurrentRegion
  9. For i = 1 To UBound(arr, 2)
  10.   If Not d.exists(arr(1, i)) Then If rng Is Nothing Then Set rng = Columns(i) Else Set rng = Union(rng, Columns(i))
  11. Next i
  12. rng.Delete
  13. Set rng = Nothing
  14. For Each c In Sheets(1).[a1].CurrentRegion.Offset(1)
  15.   If Not d.exists(c.Value) Then If rng Is Nothing Then Set rng = c Else Set rng = Union(rng, c)
  16. Next c
  17. rng.Delete Shift:=xlUp
  18. End Sub
复制代码

评分

参与人数 1 +1 收起 理由
lygyjt + 1 很给力

查看全部评分

回复

使用道具 举报

 楼主| 发表于 2017-7-8 14:57 | 显示全部楼层
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-4-18 21:56 , Processed in 0.548274 second(s), 15 queries , Gzip On, Yac On.

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

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