Excel精英培训网

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

[已解决]移动位置问题

[复制链接]
发表于 2012-11-28 22:20 | 显示全部楼层 |阅读模式
帮忙一下,我想把A列%下T4一组坐标放在T1之前,T99一组坐标放在T01之后根据G2到I2设定参数,,H2前G2如有数据,I2后没有,就执行G2,总之G2后I2,有数据就执行,没数据就不执行。(一组是T* 上下相邻  ,如T1一组是与T1与T2之间 A列A9,A10。或T与M30 相邻为一组如T99一组是A22,A23(M30是结束意思)结果如B列,谢谢
最佳答案
2012-11-29 11:02
本帖最后由 hwc2ycy 于 2012-12-19 20:27 编辑
  1. Option Explicit
  2. Sub 移位3()
  3.     '2012-12-19 hwc2ycy 修改
  4.     Dim str1$, str2$, str3$
  5.     Dim pos1&, pos2&, pos3&
  6.     Dim i&, i3&
  7.         
  8.     str1 = UCase(Range("g2"))
  9.     str2 = UCase(Range("h2"))
  10.     str3 = UCase(Range("i2"))
  11.     On Error Resume Next
  12.     ' 数据的局限性,必须以非数字开始,其后必须是数字
  13.     If Len(str2) = 0 Then MsgBox "H2单元格无数据": Exit Sub
  14.     If Len(str1) > 2 Then str1 = Left(str1, 1) & Right(str1, Len(str1) - 1) * 1
  15.     If Len(str2) > 2 Then str2 = Left(str2, 1) & Right(str2, Len(str2) - 1) * 1
  16.     If Len(str3) > 2 Then str3 = Left(str3, 1) & Right(str3, Len(str3) - 1) * 1

  17.    
  18.     If Len(str1) > 0 Then
  19.         pos2 = Range("a:a").Find(str2, , , xlWhole).Row
  20.         If Err.Number <> 0 Then MsgBox "H2数据无法找到": Exit Sub

  21.         pos1 = Range("a:a").Find(str1, , , xlWhole).Row
  22.         If Err.Number <> 0 Then MsgBox "G2数据无法找到": Exit Sub

  23.         i = pos1 + 1
  24.         Do Until Cells(i, 1) Like "T??" Or Cells(i, 1) Like "T?" Or Len(Cells(i, 1)) = 0 Or Cells(i, 1) Like "%" Or Cells(i, 1) Like "M30"
  25.             i = i + 1
  26.         Loop
  27.         
  28.         Application.ScreenUpdating = False
  29.         'G2置前
  30.         If pos2 <> i Then
  31.             Range("a" & pos1 & ":a" & i - 1).Cut
  32.             Range("a" & pos2).Insert xlShiftDown
  33.         End If
  34.         Application.ScreenUpdating = True
  35.     End If
  36.         
  37.     If Len(str3) > 0 Then
  38.         pos3 = Range("a:a").Find(str3, , , xlWhole).Row
  39.         If Err.Number <> 0 Then MsgBox "I2数据无法找到": Exit Sub
  40.         '取后置行
  41.         i = pos3 + 1
  42.         Do Until Cells(i, 1) Like "T??" Or Cells(i, 1) Like "T?" Or Cells(i, 1) Like "M30" Or Cells(i, 1) Like "%"
  43.             i = i + 1
  44.         Loop
  45.         i3 = i - 1
  46.         
  47.         '中间行
  48.         pos2 = Range("a:a").Find(str2, , , xlWhole).Row
  49.         If Err.Number <> 0 Then MsgBox "H2数据无法找到": Exit Sub
  50.         i = pos2 + 1
  51.         Do Until Cells(i, 1) Like "T??" Or Cells(i, 1) Like "T?" Or Len(Cells(i, 1)) = 0 Or Cells(i, 1) Like "%" Or Cells(i, 1) Like "M30"
  52.             i = i + 1
  53.         Loop
  54.         
  55.         Application.ScreenUpdating = False
  56.         If i <> pos3 Then
  57.             Range("a" & pos3 & ":a" & i3).Cut
  58.             Range("a" & i).Insert xlShiftDown
  59.         End If
  60.         Application.ScreenUpdating = True
  61.     End If
  62. End Sub
复制代码

移动位置.rar

11.28 KB, 下载次数: 13

发表于 2012-11-29 11:02 | 显示全部楼层    本楼为最佳答案   
本帖最后由 hwc2ycy 于 2012-12-19 20:27 编辑
  1. Option Explicit
  2. Sub 移位3()
  3.     '2012-12-19 hwc2ycy 修改
  4.     Dim str1$, str2$, str3$
  5.     Dim pos1&, pos2&, pos3&
  6.     Dim i&, i3&
  7.         
  8.     str1 = UCase(Range("g2"))
  9.     str2 = UCase(Range("h2"))
  10.     str3 = UCase(Range("i2"))
  11.     On Error Resume Next
  12.     ' 数据的局限性,必须以非数字开始,其后必须是数字
  13.     If Len(str2) = 0 Then MsgBox "H2单元格无数据": Exit Sub
  14.     If Len(str1) > 2 Then str1 = Left(str1, 1) & Right(str1, Len(str1) - 1) * 1
  15.     If Len(str2) > 2 Then str2 = Left(str2, 1) & Right(str2, Len(str2) - 1) * 1
  16.     If Len(str3) > 2 Then str3 = Left(str3, 1) & Right(str3, Len(str3) - 1) * 1

  17.    
  18.     If Len(str1) > 0 Then
  19.         pos2 = Range("a:a").Find(str2, , , xlWhole).Row
  20.         If Err.Number <> 0 Then MsgBox "H2数据无法找到": Exit Sub

  21.         pos1 = Range("a:a").Find(str1, , , xlWhole).Row
  22.         If Err.Number <> 0 Then MsgBox "G2数据无法找到": Exit Sub

  23.         i = pos1 + 1
  24.         Do Until Cells(i, 1) Like "T??" Or Cells(i, 1) Like "T?" Or Len(Cells(i, 1)) = 0 Or Cells(i, 1) Like "%" Or Cells(i, 1) Like "M30"
  25.             i = i + 1
  26.         Loop
  27.         
  28.         Application.ScreenUpdating = False
  29.         'G2置前
  30.         If pos2 <> i Then
  31.             Range("a" & pos1 & ":a" & i - 1).Cut
  32.             Range("a" & pos2).Insert xlShiftDown
  33.         End If
  34.         Application.ScreenUpdating = True
  35.     End If
  36.         
  37.     If Len(str3) > 0 Then
  38.         pos3 = Range("a:a").Find(str3, , , xlWhole).Row
  39.         If Err.Number <> 0 Then MsgBox "I2数据无法找到": Exit Sub
  40.         '取后置行
  41.         i = pos3 + 1
  42.         Do Until Cells(i, 1) Like "T??" Or Cells(i, 1) Like "T?" Or Cells(i, 1) Like "M30" Or Cells(i, 1) Like "%"
  43.             i = i + 1
  44.         Loop
  45.         i3 = i - 1
  46.         
  47.         '中间行
  48.         pos2 = Range("a:a").Find(str2, , , xlWhole).Row
  49.         If Err.Number <> 0 Then MsgBox "H2数据无法找到": Exit Sub
  50.         i = pos2 + 1
  51.         Do Until Cells(i, 1) Like "T??" Or Cells(i, 1) Like "T?" Or Len(Cells(i, 1)) = 0 Or Cells(i, 1) Like "%" Or Cells(i, 1) Like "M30"
  52.             i = i + 1
  53.         Loop
  54.         
  55.         Application.ScreenUpdating = False
  56.         If i <> pos3 Then
  57.             Range("a" & pos3 & ":a" & i3).Cut
  58.             Range("a" & i).Insert xlShiftDown
  59.         End If
  60.         Application.ScreenUpdating = True
  61.     End If
  62. End Sub
复制代码
回复

使用道具 举报

 楼主| 发表于 2012-11-29 12:02 | 显示全部楼层
hwc2ycy 发表于 2012-11-29 11:02

谢谢你的帮忙,可能我表述有问题,G2有数据,就执行G2放前,I2有数据就执行I2,G2和I2同时有就同时执行,现在问题是G2和I2同时有才执行,谢谢你改一下,谢谢。
回复

使用道具 举报

发表于 2012-11-29 12:24 | 显示全部楼层
楼主,要么你自己改改,也练练。我看你也是08年注册的ID嘛。
回复

使用道具 举报

 楼主| 发表于 2012-11-29 12:43 | 显示全部楼层
hwc2ycy 发表于 2012-11-29 12:24
楼主,要么你自己改改,也练练。我看你也是08年注册的ID嘛。

再帮忙一下,制造业比较忙,现在比较闲,老板逼得搞资料,谢谢啦,平时很少上网,谢谢!!
回复

使用道具 举报

发表于 2012-11-29 14:18 | 显示全部楼层
本帖最后由 hwc2ycy 于 2012-11-29 15:13 编辑
  1. Option Explicit
  2. Sub 移位2()
  3.     Dim str1$, str2$, str3$
  4.     Dim pos1&, pos2&, pos3&
  5.     Dim i&, i3&
  6.         
  7.     str1 = Range("g2")
  8.     str2 = Range("h2")
  9.     str3 = Range("i2")
  10.     On Error Resume Next
  11.     If Len(str1) = 0 Then MsgBox "G2单元格无数据": Exit Sub
  12.     If Len(str2) > 2 Then str2 = Left(str2, 1) & Right(str2, Len(str2) - 1) * 1
  13.     ' 数据的局限性,必须以非数字开始
  14.     pos2 = Range("a:a").Find(str2, , , xlWhole).Row
  15.     If Err.Number <> 0 Then MsgBox "H2数据无法找到": Exit Sub
  16.         
  17.     Application.ScreenUpdating = False
  18.     If Len(str1) > 0 Then
  19.         If Len(str1) > 2 Then str1 = Left(str1, 1) & Right(str1, Len(str1) - 1) * 1
  20.         pos1 = Range("a:a").Find(str1, , , xlWhole).Row
  21.         If Err.Number <> 0 Then MsgBox "G2数据无法找到": Exit Sub

  22.         i = pos1 + 1
  23.         Do Until Cells(i, 1) Like "T??" Or Cells(i, 1) Like "T?" Or Len(Cells(i, 1)) = 0 Or Cells(i, 1) Like "%"
  24.             i = i + 1
  25.         Loop
  26.         'G2置前
  27.         Range("a" & pos1 & ":a" & i - 1).Cut
  28.         If pos2 <> i Then Range("a" & pos2).Insert xlShiftDown
  29.         Application.CutCopyMode = False
  30.     End If
  31.         
  32.     If Len(str3) > 0 Then
  33.         If Len(str3) > 2 Then str3 = Left(str3, 1) & Right(str3, Len(str3) - 1) * 1
  34.         pos3 = Range("a:a").Find(str3, , , xlWhole).Row
  35.         If Err.Number <> 0 Then MsgBox "I2数据无法找到": Exit Sub
  36.         '取后置行
  37.         i = pos3 + 1
  38.         
  39.         Do Until Cells(i, 1) Like "T??" Or Cells(i, 1) Like "T?" Or Cells(i, 1) Like "M30" Or Cells(i, 1) Like "%"
  40.             i = i + 1
  41.         Loop
  42.         i3 = i - 1
  43.         
  44.         '中间行
  45.         i = pos2 + 1
  46.         Do Until Cells(i, 1) Like "T??" Or Cells(i, 1) Like "T?" Or Len(Cells(i, 1)) = 0 Or Cells(i, 1) Like "%"
  47.             i = i + 1
  48.         Loop
  49.                     
  50.         Range("a" & pos3 & ":a" & i3).Cut
  51.         If i <> pos3 Then Range("a" & i).Insert xlShiftDown
  52.         Application.CutCopyMode = False
  53.     End If
  54.     Application.ScreenUpdating = True
  55. End Sub
复制代码
回复

使用道具 举报

 楼主| 发表于 2012-11-29 14:37 | 显示全部楼层
hwc2ycy 发表于 2012-11-29 14:18

谢谢谢你的帮忙,谢谢!!!
回复

使用道具 举报

 楼主| 发表于 2012-11-29 20:01 | 显示全部楼层
hwc2ycy 发表于 2012-11-29 14:18

(M30是结束意思,M30位置不管他们怎么移动,一定要在数据最后面,这一点老师一定帮我考虑进去,得出数据才正确数据,我没表述清楚,抱歉!!!)谢谢!!
回复

使用道具 举报

 楼主| 发表于 2012-12-19 10:24 | 显示全部楼层
hwc2ycy 发表于 2012-11-29 14:18

我再用的时候发现问题如G2是T99时结果如B列,M30是结束意思,不能移动,这个帮我改一下,谢谢!!另I2(T3)前面有H2(T01)没G2(T99)时也可执行,谢谢这两个问题改一下,非常谢谢!!!

移动位置小问题.rar

20.31 KB, 下载次数: 4

回复

使用道具 举报

发表于 2012-12-19 11:02 | 显示全部楼层
fangniuji 发表于 2012-12-19 10:24
我再用的时候发现问题如G2是T99时结果如B列,M30是结束意思,不能移动,这个帮我改一下,谢谢!!另I2( ...

晚上再帮你改哈。
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-4-25 15:43 , Processed in 0.388130 second(s), 11 queries , Gzip On, Yac On.

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

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