Excel精英培训网

 找回密码
 注册
数据透视表40+个常用小技巧,让你一次学会!
12
返回列表 发新帖
楼主: fangniuji

[已解决]移动位置问题

[复制链接]
 楼主| 发表于 2012-12-19 11:19 | 显示全部楼层
hwc2ycy 发表于 2012-12-19 11:02
晚上再帮你改哈。

谢谢,谢谢!!!!
excel精英培训的微信平台,每天都会发送excel学习教程和资料。扫一扫明天就可以收到新教程
回复

使用道具 举报

发表于 2012-12-19 20:24 | 显示全部楼层
  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
复制代码

评分

参与人数 1 +3 收起 理由
fangniuji + 3 赞一个!

查看全部评分

回复

使用道具 举报

发表于 2012-12-19 20:25 | 显示全部楼层
原来的答案确实没考虑这个,有不少BUG。
回复

使用道具 举报

 楼主| 发表于 2012-12-19 20:32 | 显示全部楼层
hwc2ycy 发表于 2012-12-19 20:25
原来的答案确实没考虑这个,有不少BUG。

Option Explicit
这什么意思

回复

使用道具 举报

发表于 2012-12-19 20:40 | 显示全部楼层
Option Explicit,要求变量声明。
没有声明就会报错的。

回复

使用道具 举报

 楼主| 发表于 2012-12-19 20:51 | 显示全部楼层
hwc2ycy 发表于 2012-12-19 20:40
Option Explicit,要求变量声明。
没有声明就会报错的。

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

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-5-4 22:41 , Processed in 0.273616 second(s), 12 queries , Gzip On, Yac On.

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

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