當前位置:成語大全網 - 新華字典 - VBA中,如何對下面的數據進行快速的排序和檢查對錯

VBA中,如何對下面的數據進行快速的排序和檢查對錯

Sub iSortAndCheck()

'</*----設置參數--開始--

Dim sh As Worksheet: Set sh = ActiveSheet '操作的工作表(ActiveSheet:當前活動工作表)

Dim iCol As String: iCol = "G" '定義排序的列標

Dim iRow As Long: iRow = 2 '定義開始排序的行(標題行下面的第壹行)

Dim iFml: iFml = Array("20,3", "15,4", "6,2", "1,4") '獲取排序字段,從後往前取值

'(例"20,3",表示'0402-60-PH1-4-0445-001-001'中第20個字符開始取3個字符,即“001”)

Dim strEx: strEx = Split("^\d{4}$,^\d{2}$,PH1,^[24-7]$,^\d{4}$,^\d{3}$,^\d{3}$", ",")

'正則表達式,檢測'0402-60-PH1-4-0445-001-001'中的7個字段,按順序放置,用英文逗號","分割

'----結束--*/>

'----分段排序----

Dim rng As Range, i

Set rng = sh.Range(sh.Cells(2, iCol), sh.Cells(65536, iCol).End(xlUp))

With rng

.EntireColumn.Offset(, 3).Insert Shift:=xlToRight

For i = LBound(iFml) To UBound(iFml)

.Offset(, 3).FormulaR1C1 = "=MID(RC[-1]," & iFml(i) & ")"

.Resize(, 2).Sort .Cells(1, 1), xlAscending, Header:=xlNo

Next

.Offset(, 3).EntireColumn.Delete

End With

'----開始檢測----

Dim iArr, r&, strS, tmp, RegEx As Object, FileNum As Object

With rng

iArr = .Resize(.Rows.Count + 1, 1)

Set RegEx = CreateObject("vbscript.regexp")

RegEx.Global = True

For r = LBound(iArr) To UBound(iArr) - 1

strS = Split(Left(iArr(r, 1), 26), "-")

If UBound(strS) <> 6 Then

.Cells(r, 1).Interior.ColorIndex = 43

Else

For i = LBound(strEx) To UBound(strEx)

……

Hi我,傳給妳文件,詳細解答