Excel精英培训网

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

[已解决]删除表格里多余的数字相同的行

[复制链接]
发表于 2013-4-1 14:26 | 显示全部楼层 |阅读模式
请老师编写代码:删除表格里多余的数字相同的行。谢谢!
最佳答案
2013-4-1 15:40
  1. Private Sub CommandButton1_Click()
  2.     Dim arr, arrtemp
  3.     Dim rg As Range
  4.     Dim result()

  5.     On Error Resume Next

  6.     Set rg = Range("a2:g23").SpecialCells(xlCellTypeBlanks)
  7.     If Not rg Is Nothing Then rg = " "
  8.     arr = Range("a2:g23")
  9.     Set rg = Nothing
  10.     Dim dic As Object, str$
  11.     Set dic = CreateObject("scripting.dictionary")

  12.     For i = LBound(arr) To UBound(arr)
  13.         str = Join(WorksheetFunction.Index(arr, i, 0), "#")
  14.         dic(str) = dic(str) + 1
  15.     Next
  16.     i = 0
  17.     ReDim result(1 To dic.Count)
  18.     For Each arrtemp In dic.keys
  19.     i = i + 1
  20.     result(i) = Split(arrtemp, "#")
  21.     Next
  22.     If dic.Count > 0 Then
  23.         Range("j1").CurrentRegion.ClearContents
  24.         Range("j1") = "表2"
  25.         result = WorksheetFunction.Transpose(WorksheetFunction.Transpose(result))
  26.         [j2].Resize(UBound(result), UBound(result, 2)) = result
  27.     End If
  28. End Sub
复制代码

删除表格里多余的数字相同的行.rar

9.98 KB, 下载次数: 12

excel精英培训的微信平台,每天都会发送excel学习教程和资料。扫一扫明天就可以收到新教程
发表于 2013-4-1 14:41 | 显示全部楼层
怎么又是这样的题,周末不是发了一贴的嘛。
回复

使用道具 举报

发表于 2013-4-1 14:43 | 显示全部楼层
[已解决]请老师编写代码
http://www.excelpx.com/thread-298229-1-1.html

你老把这类似的问题这么发题,就有灌水的嫌疑了。
回复

使用道具 举报

 楼主| 发表于 2013-4-1 15:13 | 显示全部楼层
hwc2ycy 发表于 2013-4-1 14:43
[已解决]请老师编写代码
http://www.excelpx.com/thread-298229-1-1.html

你好:你上次发的不是我想的,你把相同数字的行全部删除了,应该删除多余的。谢谢
回复

使用道具 举报

发表于 2013-4-1 15:25 | 显示全部楼层
那就是个参数的问题。

人家给你解决的问题,你也要自己学习下,不说举一反三,起码你要能读懂。

不然次次只会要鱼而不会渔。
回复

使用道具 举报

 楼主| 发表于 2013-4-1 15:30 | 显示全部楼层
hwc2ycy 发表于 2013-4-1 15:25
那就是个参数的问题。

人家给你解决的问题,你也要自己学习下,不说举一反三,起码你要能读懂。

你好:你说对,我是在学,因为我正在过这个入门坎,解决数组和字典的用法。
回复

使用道具 举报

发表于 2013-4-1 15:40 | 显示全部楼层    本楼为最佳答案   
  1. Private Sub CommandButton1_Click()
  2.     Dim arr, arrtemp
  3.     Dim rg As Range
  4.     Dim result()

  5.     On Error Resume Next

  6.     Set rg = Range("a2:g23").SpecialCells(xlCellTypeBlanks)
  7.     If Not rg Is Nothing Then rg = " "
  8.     arr = Range("a2:g23")
  9.     Set rg = Nothing
  10.     Dim dic As Object, str$
  11.     Set dic = CreateObject("scripting.dictionary")

  12.     For i = LBound(arr) To UBound(arr)
  13.         str = Join(WorksheetFunction.Index(arr, i, 0), "#")
  14.         dic(str) = dic(str) + 1
  15.     Next
  16.     i = 0
  17.     ReDim result(1 To dic.Count)
  18.     For Each arrtemp In dic.keys
  19.     i = i + 1
  20.     result(i) = Split(arrtemp, "#")
  21.     Next
  22.     If dic.Count > 0 Then
  23.         Range("j1").CurrentRegion.ClearContents
  24.         Range("j1") = "表2"
  25.         result = WorksheetFunction.Transpose(WorksheetFunction.Transpose(result))
  26.         [j2].Resize(UBound(result), UBound(result, 2)) = result
  27.     End If
  28. End Sub
复制代码

评分

参与人数 1 +3 收起 理由
ymq123 + 3

查看全部评分

回复

使用道具 举报

发表于 2013-4-1 15:40 | 显示全部楼层
字典是对象里最容易掌握的了。
属性加方法一共才10个。
回复

使用道具 举报

发表于 2013-4-1 15:48 | 显示全部楼层
  1. Private Sub CommandButton1_Click()
  2.     Dim arr, arrtemp
  3.     Dim rg As Range
  4.     Dim result()
  5.     Dim i As Integer
  6.     On Error Resume Next
  7.     Application.ScreenUpdating = False

  8.     Set rg = Range("a2:g23").SpecialCells(xlCellTypeBlanks)
  9.     If Not rg Is Nothing Then rg = " "
  10.     Set rg = Nothing

  11.     arr = Range("a2:g23")

  12.     Dim dic As Object, str$
  13.     Set dic = CreateObject("scripting.dictionary")

  14.     For i = LBound(arr) To UBound(arr)
  15.         str = Join(WorksheetFunction.Index(arr, i, 0), "#")
  16.         dic(str) = dic(str) + 1
  17.     Next

  18.     i = 0
  19.     ReDim result(1 To dic.Count)
  20.     For Each arrtemp In dic.keys
  21.         i = i + 1
  22.         result(i) = Split(arrtemp, "#")
  23.     Next
  24.    
  25.    
  26.     If dic.Count > 0 Then
  27.         [j1].CurrentRegion.ClearContents
  28.         [j1] = "表2"
  29.         result = WorksheetFunction.Transpose(WorksheetFunction.Transpose(result))
  30.         [j2].Resize(UBound(result), UBound(result, 2)) = result
  31.     End If
  32.    
  33.     Set dic = Nothing
  34.     Application.ScreenUpdating = True
  35.    
  36. End Sub
复制代码

评分

参与人数 1 +3 收起 理由
ymq123 + 3

查看全部评分

回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-4-27 03:43 , Processed in 0.574264 second(s), 15 queries , Gzip On, Yac On.

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

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