|
发表于 2013-9-12 21:51
|
显示全部楼层
本楼为最佳答案
改进下,把标记检测只在代码入口中做检测,避免后面重复的检测。- Sub Main2()
- Dim lFind As Long, lFind2 As Long
- Dim lFindPercent As Long, lFindM30 As Long
- If FindPattern(1, "M48") = 0 Or FindPattern(1, "M30") = 0 Or FindPattern(1, "%") = 0 Then
- MsgBox "A列数据缺少 M48 或 M30 或 % 标记"
- Exit Sub
- End If
- Application.ScreenUpdating = False
- lFind = FindPattern(1, "M30")
- Cells(lFind, 1).Insert shift:=xlDown
- Cells(lFind, 1).Value = "M09"
- lFindPercent = FindPattern(1, "%")
- lFind = FindPattern(1, "M09")
- lFind2 = FindPattern(1, "M30")
- Range("a" & lFindPercent + 1 & ":a" & lFind - 1).Copy
- Range("a" & lFind2).Insert shift:=xlDown
- Application.CutCopyMode = False
- lFind = FindPattern(1, "M48")
- lFindPercent = FindPattern(1, "%")
- lFind2 = FindPattern(1, "M30")
- Range("a" & lFind + 1 & ":a" & lFindPercent - 1).Copy
- Range("a" & lFind2).Insert shift:=xlDown
- Application.CutCopyMode = False
- lFindPercent = FindPattern(1, "%")
- Dim i As Long, l As Long
- Dim arr
- Dim strTemp As String
- arr = Range("a1:b" & lFindPercent - 1).Value
- For i = LBound(arr) To UBound(arr)
- strTemp = arr(i, 1)
- If strTemp Like "T*C.*" Then
- l = Val(Mid(strTemp, 2, InStr(strTemp, "C") - 1))
- lFind = FindPattern(1, "T" & l)
- Do While lFind
- If lFind Then
- Cells(lFind, 1).Value = Cells(lFind, 1).Value & Mid(strTemp, InStr(strTemp, "C"))
- End If
- lFind = FindPattern(1, "T" & l)
- Loop
- End If
- Next
- lFindPercent = FindPattern(1, "%")
- lFind = FindPattern(1, "M09")
- For i = lFindPercent + 1 To lFind - 1
- strTemp = Cells(i, 1).Value
- If strTemp Like "T*C.*" Then
- Cells(i, 1).Value = strTemp & "Z-0.02"
- End If
- Next
- lFind2 = FindPattern(1, Cells(lFind - 1, 1).Value, 2)
- For i = lFind + 1 To lFind2 - 1
- strTemp = Cells(i, 1).Value
- If strTemp Like "T*C.*" Then
- Cells(i, 1).Value = strTemp & "Z-0.3"
- End If
- Next
- lFind = FindPattern(1, "M30")
- For i = lFind2 + 1 To lFind - 1
- strTemp = Cells(i, 1).Value
- If strTemp Like "T*C.*" Then
- Cells(i, 1).Value = strTemp & "Z-0.0253"
- End If
- Next
- Columns(1).AutoFit
- Application.ScreenUpdating = True
- MsgBox "处理完成"
- End Sub
- Function FindPattern(lCol As Long, strPattern As String, Optional Direction As Byte = 1)
- On Error Resume Next
- Dim rg As Range
- Set rg = Columns(lCol).Find(what:=strPattern, LookAt:=xlWhole, SearchDirection:=Direction)
- If rg Is Nothing Then
- FindPattern = 0
- Else
- FindPattern = rg.Row
- End If
- End Function
复制代码 |
|