Sub ListIndex()
Dim i, j, k
Dim ex_i, ex_j
Dim ex_a, ex_b
Dim startrow As Integer, startcol As Integer
Dim endrow As Integer, endcol As Integer
'MsgBox ActiveSheet.UsedRange.rows.Count
'MsgBox ActiveSheet.UsedRange.columns.Count
i = 2
j = 0
k = 2
For i = 2 To ActiveSheet.UsedRange.rows.Count
Set a = Cells(i, 1)
If InStr(a, "message") Then
If j > 0 Then
'MsgBox "i=" & i & "j=" & j & "k=" & k
start_i = k + 1
end_i = i - 1
start_j = k + 1
end_j = i - 1 - 1
'MsgBox "start_i=" & start_i & "end_i=" & end_i & "start_j=" & start_j & "end_j=" & end_j
For ex_i = start_i To end_i
For ex_j = start_j To end_j
Set ex_a = Cells(ex_j, 16)
Set ex_b = Cells(ex_j + 1, 16)
'MsgBox "ex_a=" & ex_a & "ex_b=" & ex_b
If ex_a > ex_b Then
'MsgBox "ex_j=" & ex_j
rows(ex_j & ":" & ex_j).Select
Selection.Cut
rows(ex_j + 2 & ":" & ex_j + 2).Select
Selection.Insert Shift:=xlDown
End If
Next
Next
End If
k = i
j = 0
Else
'ActiveSheet.Cells(i, 1) = j
j = j + 1
Set b = Cells(i, 16)
If InStr(b, "-") Then
rightStr = Split(b, "-")
ActiveSheet.Cells(i, 16) = Val(rightStr(UBound(rightStr)))
Else
ActiveSheet.Cells(i, 16) = Val(b)
End If
End If
Next
For i = 2 To ActiveSheet.UsedRange.rows.Count
Set a = Cells(i, 1)
If InStr(a, "message") Then
j = 0
Else
ActiveSheet.Cells(i, 1) = j
j = j + 1
End If
Next
ActiveWorkbook.Save
End Sub
VBA之ListIndex
最新推荐文章于 2024-04-27 07:45:00 发布