這應該做你想要什麼,如果我的理解如下:
- 複製從授權表錯配的標題行
- 通過並比較授權表中的「Cust Bill TO ID」和「SAP CMF#」列之間的數據
- 複製的行數據低於
見代碼不匹配表:
Option Explicit
Sub Mismatch()
Dim AuthIssuedsheet As Worksheet
Set AuthIssuedsheet = ThisWorkbook.Sheets("Authorizations Issued")
Dim MismatchSheet As Worksheet
Set MismatchSheet = ThisWorkbook.Sheets("Mismatch")
'Copying the header of AuthIssuedsheet to MismatchSheet
AuthIssuedsheet.Range("A1:BI1").Copy MismatchSheet.Range("A1")
Dim AuthShtLastRow As Long
'Finding last row of used data in Authorizations Issued sheet
With AuthIssuedsheet
AuthShtLastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
End With
Dim AuthShtLastCol As Long
'Finding last row of used data in Authorizations Issued sheet
With AuthIssuedsheet
AuthShtLastCol = .Cells(1, .Columns.Count).End(xlToLeft).Column
End With
Dim CustBillCol As Range
Dim SAPCMF As Range
Dim CustBillArray As Variant
Dim SAPCMFArray As Variant
Dim CustBill_SAPCMF_Array As Variant
With AuthIssuedsheet
'Finds the header column with text Cust Bill To IF
Set CustBillCol = .Range(.Cells(1, 1), .Cells(1, .Columns.Count)).Find(_
What:="Cust Bill To ID", After:=AuthIssuedsheet.Range("A1"), _
LookIn:=xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, _
SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False)
'Finds the header column with text SAP CMF#
Set SAPCMF = .Range(.Cells(1, 1), .Cells(1, .Columns.Count)).Find(_
What:="SAP CMF#", After:=AuthIssuedsheet.Range("A1"), _
LookIn:=xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, _
SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False)
CustBillArray = .Range(.Cells(2, CustBillCol.Column), .Cells(AuthShtLastRow, CustBillCol.Column))
SAPCMFArray = .Range(.Cells(2, SAPCMF.Column), .Cells(AuthShtLastRow, SAPCMF.Column))
End With
Dim ArrayPos As Long
Dim LastRowMismatch As Long
'deletes old data from Mismatch sheet before copying the mismatch data over
MismatchSheet.Range(MismatchSheet.Rows(2), MismatchSheet.Rows(Rows.Count)).ClearContents
'Looping through every value in the two arrays to check for mismatchs
For ArrayPos = 1 To UBound(CustBillArray)
If CustBillArray(ArrayPos, 1) <> SAPCMFArray(ArrayPos, 1) Then
'Finding last row of used data in Mismatch sheet
With MismatchSheet
LastRowMismatch = .Cells(.Rows.Count, "A").End(xlUp).Row
End With
'Copy the information acorss to mismatch sheet
AuthIssuedsheet.Range(AuthIssuedsheet.Cells(ArrayPos + 1, 1), _
AuthIssuedsheet.Cells(ArrayPos + 1, AuthShtLastCol)).Copy _
MismatchSheet.Range("" & "A" & LastRowMismatch + 1)
End If
'becasue you might be running through a large data set i just put a msgbox every 20% to
'let user know it will be a while
'Ideally you would have a Userform with progress for large data sets
If ((ArrayPos/UBound(CustBillArray)) * 100) = 20 Or _
((ArrayPos/UBound(CustBillArray)) * 100) = 40 Or _
((ArrayPos/UBound(CustBillArray)) * 100) = 60 Or _
((ArrayPos/UBound(CustBillArray)) * 100) = 80 Then
MsgBox "Currently on " & Fix((ArrayPos/UBound(CustBillArray)) * 100) & "%"
End If
Next ArrayPos
MismatchSheet.UsedRange.EntireColumn.AutoFit
MsgBox "Complete!"
End Sub
你必須聲明你的數組的大小,或者在你暗淡的時候聲明它的大小,或者,如果你事先不知道,在使用'Redim'的代碼中。 – SJR
@SJR'Redim BTID(0)'已添加,但結果保持不變。 :( – lcc
這隻會保存一個條目,因爲數組是基於零的(默認情況下)。您還需要詳細閱讀如何避免使用'Select'。 – SJR