|
本帖最后由 hwc2ycy 于 2013-3-27 12:26 编辑
- Sub 模式复制()
- Dim i As Long
- Dim lStart As Long
- Dim lEnd As Long
- Dim lRow As Long
- Dim rg As Range
- Dim lDstRow As Long
- Dim lSrcCol As String
- lSrcCol = "l"
- Columns("k").Copy Columns("p")
- lStart = GetPosition(lSrcCol, "T#", 1)
- lRow = Cells(Rows.Count, lSrcCol).End(xlUp).Row
- Do While lStart < lRow
- If lStart = 0 Then Exit Sub
- lEnd = GetPosition(lSrcCol, "T#", lStart + 1)
- If lEnd = 0 Then Exit Sub
- Set rg = Range("p:p").Find(what:=Cells(lStart, lSrcCol), lookat:=xlWhole)
- If rg Is Nothing Then Exit Sub
- lDstRow = GetPosition("p", "T#", rg.Row + 1)
- If lDstRow = 0 Then Exit Sub
- Application.ScreenUpdating = False
- Range(Cells(lStart + 1, lSrcCol), Cells(lEnd - 1, lSrcCol)).Copy
- Cells(lDstRow, "p").Insert shift:=xlDown
- Application.CutCopyMode = False
- Application.ScreenUpdating = True
- lStart = GetPosition(lSrcCol, "T#", lEnd)
- Loop
- End Sub
- Function GetPosition(lCol, Pattern As String, Optional lRow As Long = 1) As Long
- '参数lCol,查找的指定列
- '参数Pattern,匹配表达式,可用通配符,通配符格式与LIKE相同
- '参数lRow,指定行,默认为1
- '---------------------------------------------------------------------------------------
- ' Procedure : GetPosition
- ' Author : hwc2ycy
- ' Date : 2013/3/27
- ' Purpose : 在某列从指定行开始查找匹配的行的位置,
- '---------------------------------------------------------------------------------------
- '
- Dim lLastRow As Long
- lLastRow = Cells(Rows.Count, lCol).End(xlUp).Row
- Dim i As Long
- i = lRow
- Do While Not (Cells(i, lCol) Like Pattern Or Cells(i, lCol) Like Pattern & "#" Or Cells(i, lCol) Like "M30") And i <= lLastRow
- i = i + 1
- Loop
- If i <= lLastRow Then GetPosition = i
- End Function
复制代码 |
|