Excel精英培训网

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

[已解决]复制问题

[复制链接]
发表于 2013-9-12 12:15 | 显示全部楼层 |阅读模式
数据源A列在M30上加一个M09,然后再加%到M30之间数据,最后再加M48到%之间数据,结果如B列,接着把%以上T后面C数据对应添加%以下T数据中,如C列。最后在原M30和%之间数据T后面加Z-0.02,M09 下复制%到M30之间数据中T后面加Z-0.3,原M48到%之间复制数据T后面加Z-0.0253,最后结果如D列,谢谢
最佳答案
2013-9-12 21:51
改进下,把标记检测只在代码入口中做检测,避免后面重复的检测。
  1. Sub Main2()
  2.     Dim lFind As Long, lFind2 As Long
  3.     Dim lFindPercent As Long, lFindM30 As Long

  4.     If FindPattern(1, "M48") = 0 Or FindPattern(1, "M30") = 0 Or FindPattern(1, "%") = 0 Then
  5.         MsgBox "A列数据缺少 M48 或 M30 或 % 标记"
  6.         Exit Sub
  7.     End If

  8.     Application.ScreenUpdating = False

  9.     lFind = FindPattern(1, "M30")
  10.     Cells(lFind, 1).Insert shift:=xlDown
  11.     Cells(lFind, 1).Value = "M09"
  12.     lFindPercent = FindPattern(1, "%")
  13.     lFind = FindPattern(1, "M09")
  14.     lFind2 = FindPattern(1, "M30")
  15.     Range("a" & lFindPercent + 1 & ":a" & lFind - 1).Copy
  16.     Range("a" & lFind2).Insert shift:=xlDown

  17.     Application.CutCopyMode = False

  18.     lFind = FindPattern(1, "M48")
  19.     lFindPercent = FindPattern(1, "%")
  20.     lFind2 = FindPattern(1, "M30")
  21.     Range("a" & lFind + 1 & ":a" & lFindPercent - 1).Copy
  22.     Range("a" & lFind2).Insert shift:=xlDown

  23.     Application.CutCopyMode = False

  24.     lFindPercent = FindPattern(1, "%")

  25.     Dim i As Long, l As Long
  26.     Dim arr
  27.     Dim strTemp As String
  28.     arr = Range("a1:b" & lFindPercent - 1).Value
  29.     For i = LBound(arr) To UBound(arr)
  30.         strTemp = arr(i, 1)
  31.         If strTemp Like "T*C.*" Then
  32.             l = Val(Mid(strTemp, 2, InStr(strTemp, "C") - 1))
  33.             lFind = FindPattern(1, "T" & l)
  34.             Do While lFind
  35.                 If lFind Then
  36.                     Cells(lFind, 1).Value = Cells(lFind, 1).Value & Mid(strTemp, InStr(strTemp, "C"))
  37.                 End If
  38.                 lFind = FindPattern(1, "T" & l)
  39.             Loop
  40.         End If
  41.     Next

  42.     lFindPercent = FindPattern(1, "%")
  43.     lFind = FindPattern(1, "M09")
  44.     For i = lFindPercent + 1 To lFind - 1
  45.         strTemp = Cells(i, 1).Value
  46.         If strTemp Like "T*C.*" Then
  47.             Cells(i, 1).Value = strTemp & "Z-0.02"
  48.         End If
  49.     Next

  50.     lFind2 = FindPattern(1, Cells(lFind - 1, 1).Value, 2)
  51.     For i = lFind + 1 To lFind2 - 1
  52.         strTemp = Cells(i, 1).Value
  53.         If strTemp Like "T*C.*" Then
  54.             Cells(i, 1).Value = strTemp & "Z-0.3"
  55.         End If
  56.     Next

  57.     lFind = FindPattern(1, "M30")
  58.     For i = lFind2 + 1 To lFind - 1
  59.         strTemp = Cells(i, 1).Value
  60.         If strTemp Like "T*C.*" Then
  61.             Cells(i, 1).Value = strTemp & "Z-0.0253"
  62.         End If
  63.     Next

  64.     Columns(1).AutoFit
  65.     Application.ScreenUpdating = True
  66.     MsgBox "处理完成"
  67. End Sub

  68. Function FindPattern(lCol As Long, strPattern As String, Optional Direction As Byte = 1)
  69.     On Error Resume Next
  70.     Dim rg As Range
  71.     Set rg = Columns(lCol).Find(what:=strPattern, LookAt:=xlWhole, SearchDirection:=Direction)
  72.     If rg Is Nothing Then
  73.         FindPattern = 0
  74.     Else
  75.         FindPattern = rg.Row
  76.     End If
  77. End Function
复制代码

深浅孔.rar

11.26 KB, 下载次数: 10

excel精英培训的微信平台,每天都会发送excel学习教程和资料。扫一扫明天就可以收到新教程
发表于 2013-9-12 14:13 | 显示全部楼层
能在帖子里发一个数据吗?你给的附件打不开
回复

使用道具 举报

发表于 2013-9-12 21:31 | 显示全部楼层
  1. Sub main()
  2.     Dim lFind As Long, lFind2 As Long
  3.     Dim lFindPercent As Long, lFindM30 As Long
  4.     Application.ScreenUpdating = False
  5.    
  6.     lFind = FindPattern(1, "M30")
  7.     If lFind Then
  8.         Cells(lFind, 1).Insert shift:=xlDown
  9.         Cells(lFind, 1).Value = "M09"
  10.     Else
  11.         MsgBox "没有找到 M30 标记"
  12.     End If
  13.    
  14.     lFindPercent = FindPattern(1, "%")
  15.     lFind = FindPattern(1, "M09")
  16.     lFind2 = FindPattern(1, "M30")
  17.     If lFindPercent > 0 And lFind2 > 0 Then
  18.         Range("a" & lFindPercent + 1 & ":a" & lFind - 1).Copy
  19.         Range("a" & lFind2).Insert shift:=xlDown
  20.     Else
  21.         MsgBox "没有找到 % 或 M30 标记"
  22.     End If
  23.    
  24.     Application.CutCopyMode = False

  25.     lFind = FindPattern(1, "M48")
  26.     lFindPercent = FindPattern(1, "%")
  27.     lFind2 = FindPattern(1, "M30")
  28.     If lFindPercent > 0 And lFind2 > 0 Then
  29.         Range("a" & lFind + 1 & ":a" & lFindPercent - 1).Copy
  30.         Range("a" & lFind2).Insert shift:=xlDown
  31.     Else
  32.         MsgBox "没有找到 % 或 M30 标记"
  33.     End If
  34.    
  35.     Application.CutCopyMode = False
  36.     lFindPercent = FindPattern(1, "%")
  37.     Dim i As Long, l As Long
  38.     Dim arr
  39.     Dim strTemp As String
  40.     arr = Range("a1:b" & lFindPercent - 1).Value
  41.     For i = LBound(arr) To UBound(arr)
  42.         strTemp = arr(i, 1)
  43.         If strTemp Like "T*C.*" Then
  44.             l = Val(Mid(strTemp, 2, InStr(strTemp, "C") - 1))
  45.             lFind = FindPattern(1, "T" & l)
  46.             Do While lFind
  47.                 If lFind Then
  48.                     Cells(lFind, 1).Value = Cells(lFind, 1).Value & Mid(strTemp, InStr(strTemp, "C"))
  49.                 End If
  50.                 lFind = FindPattern(1, "T" & l)
  51.             Loop
  52.         End If
  53.     Next
  54.    
  55.     lFindPercent = FindPattern(1, "%")
  56.     lFind = FindPattern(1, "M09")
  57.     If lFindPercent > 0 And lFind > 0 Then
  58.         For i = lFindPercent + 1 To lFind - 1
  59.             strTemp = Cells(i, 1).Value
  60.             If strTemp Like "T*C.*" Then
  61.                 Cells(i, 1).Value = strTemp & "Z-0.02"
  62.             End If
  63.         Next
  64.     Else
  65.         MsgBox "没有找到 M09 或 % 标记"
  66.     End If
  67.    
  68.     lFind2 = FindPattern(1, Cells(lFind - 1, 1).Value, 2)
  69.     If lFind2 Then
  70.         For i = lFind + 1 To lFind2 - 1
  71.             strTemp = Cells(i, 1).Value
  72.             If strTemp Like "T*C.*" Then
  73.                 Cells(i, 1).Value = strTemp & "Z-0.3"
  74.             End If
  75.         Next
  76.     Else
  77.         MsgBox "没有找到标记"
  78.     End If
  79.    
  80.     lFind = FindPattern(1, "M30")
  81.     If lFind Then
  82.         For i = lFind2 + 1 To lFind - 1
  83.             strTemp = Cells(i, 1).Value
  84.             If strTemp Like "T*C.*" Then
  85.                 Cells(i, 1).Value = strTemp & "Z-0.0253"
  86.             End If
  87.         Next
  88.     Else
  89.         MsgBox "没有找到 M30 标记"
  90.     End If
  91.     Columns(1).AutoFit
  92.     Application.ScreenUpdating = True
  93.     MsgBox "处理完成"
  94. End Sub

  95. Function FindPattern(lCol As Long, strPattern As String, Optional Direction As Byte = 1)
  96.     On Error Resume Next
  97.     Dim rg As Range
  98.     Set rg = Columns(lCol).Find(what:=strPattern, LookAt:=xlWhole, SearchDirection:=Direction)
  99.     If rg Is Nothing Then
  100.         FindPattern = 0
  101.     Else
  102.         FindPattern = rg.Row
  103.     End If
  104. End Function
复制代码
回复

使用道具 举报

发表于 2013-9-12 21:32 | 显示全部楼层
用A列数据测试后,无问题。
实际使用前,建议先备份A列数据。
切记,不要重复测试,否则产生的错误无法估量。

回复

使用道具 举报

发表于 2013-9-12 21:51 | 显示全部楼层    本楼为最佳答案   
改进下,把标记检测只在代码入口中做检测,避免后面重复的检测。
  1. Sub Main2()
  2.     Dim lFind As Long, lFind2 As Long
  3.     Dim lFindPercent As Long, lFindM30 As Long

  4.     If FindPattern(1, "M48") = 0 Or FindPattern(1, "M30") = 0 Or FindPattern(1, "%") = 0 Then
  5.         MsgBox "A列数据缺少 M48 或 M30 或 % 标记"
  6.         Exit Sub
  7.     End If

  8.     Application.ScreenUpdating = False

  9.     lFind = FindPattern(1, "M30")
  10.     Cells(lFind, 1).Insert shift:=xlDown
  11.     Cells(lFind, 1).Value = "M09"
  12.     lFindPercent = FindPattern(1, "%")
  13.     lFind = FindPattern(1, "M09")
  14.     lFind2 = FindPattern(1, "M30")
  15.     Range("a" & lFindPercent + 1 & ":a" & lFind - 1).Copy
  16.     Range("a" & lFind2).Insert shift:=xlDown

  17.     Application.CutCopyMode = False

  18.     lFind = FindPattern(1, "M48")
  19.     lFindPercent = FindPattern(1, "%")
  20.     lFind2 = FindPattern(1, "M30")
  21.     Range("a" & lFind + 1 & ":a" & lFindPercent - 1).Copy
  22.     Range("a" & lFind2).Insert shift:=xlDown

  23.     Application.CutCopyMode = False

  24.     lFindPercent = FindPattern(1, "%")

  25.     Dim i As Long, l As Long
  26.     Dim arr
  27.     Dim strTemp As String
  28.     arr = Range("a1:b" & lFindPercent - 1).Value
  29.     For i = LBound(arr) To UBound(arr)
  30.         strTemp = arr(i, 1)
  31.         If strTemp Like "T*C.*" Then
  32.             l = Val(Mid(strTemp, 2, InStr(strTemp, "C") - 1))
  33.             lFind = FindPattern(1, "T" & l)
  34.             Do While lFind
  35.                 If lFind Then
  36.                     Cells(lFind, 1).Value = Cells(lFind, 1).Value & Mid(strTemp, InStr(strTemp, "C"))
  37.                 End If
  38.                 lFind = FindPattern(1, "T" & l)
  39.             Loop
  40.         End If
  41.     Next

  42.     lFindPercent = FindPattern(1, "%")
  43.     lFind = FindPattern(1, "M09")
  44.     For i = lFindPercent + 1 To lFind - 1
  45.         strTemp = Cells(i, 1).Value
  46.         If strTemp Like "T*C.*" Then
  47.             Cells(i, 1).Value = strTemp & "Z-0.02"
  48.         End If
  49.     Next

  50.     lFind2 = FindPattern(1, Cells(lFind - 1, 1).Value, 2)
  51.     For i = lFind + 1 To lFind2 - 1
  52.         strTemp = Cells(i, 1).Value
  53.         If strTemp Like "T*C.*" Then
  54.             Cells(i, 1).Value = strTemp & "Z-0.3"
  55.         End If
  56.     Next

  57.     lFind = FindPattern(1, "M30")
  58.     For i = lFind2 + 1 To lFind - 1
  59.         strTemp = Cells(i, 1).Value
  60.         If strTemp Like "T*C.*" Then
  61.             Cells(i, 1).Value = strTemp & "Z-0.0253"
  62.         End If
  63.     Next

  64.     Columns(1).AutoFit
  65.     Application.ScreenUpdating = True
  66.     MsgBox "处理完成"
  67. End Sub

  68. Function FindPattern(lCol As Long, strPattern As String, Optional Direction As Byte = 1)
  69.     On Error Resume Next
  70.     Dim rg As Range
  71.     Set rg = Columns(lCol).Find(what:=strPattern, LookAt:=xlWhole, SearchDirection:=Direction)
  72.     If rg Is Nothing Then
  73.         FindPattern = 0
  74.     Else
  75.         FindPattern = rg.Row
  76.     End If
  77. End Function
复制代码
回复

使用道具 举报

发表于 2013-9-12 23:26 | 显示全部楼层
好长一串代码,看不懂什么意思。
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-4-19 20:25 , Processed in 1.228133 second(s), 13 queries , Gzip On, Yac On.

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

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