Excel精英培训网

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

[已解决]我执行这个宏,出现如图情况,帮我改改,谢谢!!

[复制链接]
发表于 2012-11-25 13:45 | 显示全部楼层 |阅读模式
我执行这个宏,出现如图情况,帮我改改,谢谢!!
最佳答案
2012-11-25 14:03
  1. Sub zhengli()
  2.   Dim i As Long, j As Long, k As Long
  3.     Dim x As Long, y As Long, z As Long
  4.     Dim s1 As String, s2 As String
  5.     Dim rg As Range
  6.     On Error Resume Next
  7.     Application.ScreenUpdating = False
  8.     Application.Calculation = xlCalculationManual
  9.     z = Range("a:a").Find("%").Row
  10.     For i = 1 To z - 2
  11.         For j = i + 1 To z - 1
  12.             If Right(Trim(Cells(i, 1)), 6) = Right(Trim(Cells(j, 1)), 6) Then
  13.               If Cells(j, 5) <> "标识孔" Then
  14.                 s1 = "T" & Val(Mid(Cells(i, 1), 2, 2))
  15.                 s2 = "T" & Val(Mid(Cells(j, 1), 2, 2))
  16.                 Set rg = Range("a:a").Find(s2, , , xlWhole)
  17.                 If Not rg Is Nothing Then
  18.                     x = rg.Row
  19.                     
  20.                 'If Not Range("a:a").Find(s2, , , xlWhole) Is Nothing Then
  21.                 'x = Range("a:a").Find(s2, , , xlWhole).Row
  22.                     y = x
  23.                     Do
  24.                         y = y + 1
  25.                     Loop Until Len(Trim(Cells(y, 1))) < 4
  26.                     Range(Cells(x, 1), Cells(y - 1, 1)).Cut
  27.                     Range("a:a").Find(s1, , , xlWhole).Insert shift:=xlDown
  28.                     Set rg = Nothing
  29.                     Exit For
  30.                 End If
  31.               End If
  32.             End If
  33.         Next
  34.     Next
  35.     Application.Calculation = xlCalculationAutomatic
  36.     Application.ScreenUpdating = True
  37. End Sub
复制代码
代码结果楼主就自己验证下吧。
未命名1.JPG

Book1.rar

740.93 KB, 下载次数: 12

excel精英培训的微信平台,每天都会发送excel学习教程和资料。扫一扫明天就可以收到新教程
发表于 2012-11-25 13:59 | 显示全部楼层
因为找不到啊,既然没有找到单元格,何来的.ROW呢。
回复

使用道具 举报

发表于 2012-11-25 14:03 | 显示全部楼层    本楼为最佳答案   
  1. Sub zhengli()
  2.   Dim i As Long, j As Long, k As Long
  3.     Dim x As Long, y As Long, z As Long
  4.     Dim s1 As String, s2 As String
  5.     Dim rg As Range
  6.     On Error Resume Next
  7.     Application.ScreenUpdating = False
  8.     Application.Calculation = xlCalculationManual
  9.     z = Range("a:a").Find("%").Row
  10.     For i = 1 To z - 2
  11.         For j = i + 1 To z - 1
  12.             If Right(Trim(Cells(i, 1)), 6) = Right(Trim(Cells(j, 1)), 6) Then
  13.               If Cells(j, 5) <> "标识孔" Then
  14.                 s1 = "T" & Val(Mid(Cells(i, 1), 2, 2))
  15.                 s2 = "T" & Val(Mid(Cells(j, 1), 2, 2))
  16.                 Set rg = Range("a:a").Find(s2, , , xlWhole)
  17.                 If Not rg Is Nothing Then
  18.                     x = rg.Row
  19.                     
  20.                 'If Not Range("a:a").Find(s2, , , xlWhole) Is Nothing Then
  21.                 'x = Range("a:a").Find(s2, , , xlWhole).Row
  22.                     y = x
  23.                     Do
  24.                         y = y + 1
  25.                     Loop Until Len(Trim(Cells(y, 1))) < 4
  26.                     Range(Cells(x, 1), Cells(y - 1, 1)).Cut
  27.                     Range("a:a").Find(s1, , , xlWhole).Insert shift:=xlDown
  28.                     Set rg = Nothing
  29.                     Exit For
  30.                 End If
  31.               End If
  32.             End If
  33.         Next
  34.     Next
  35.     Application.Calculation = xlCalculationAutomatic
  36.     Application.ScreenUpdating = True
  37. End Sub
复制代码
代码结果楼主就自己验证下吧。
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-4-20 17:39 , Processed in 0.267824 second(s), 10 queries , Gzip On, Yac On.

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

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