Option Explicit
Private Const nFirstDataROW = 2
Private Const sAutoFilterRANGE = "A1:BG1"
Sub MacroForColumnBF()
'This puts 'OK' or 'FAILED in Column 'BF' based on the following:
'
'Pass 1:
'Examine the contents of:
'Column H (Vendor Name)
'Column AG (Acct No)
'Column AF (Bank Key)
'
'If there is a duplicate in any row for a specific Vendor in columns 'AF' and 'AG'
'ALL rows for that vendor FAIL
'
'
'
'Pass2:
'Examine the contents of:
'Column H (Vendor Name)
'Column D (Account Group)
'
'Duplicate Vendor Names and Account Groups indicate FAIL
Dim i As Long
Dim iRow As Long
Dim iRowCountForThisVendor As Long
Dim iLastRow As Long
Dim bHaveFailure As Boolean
Dim sAcccountKeyFromColumnD As String
Dim sAcccountKeyPrevious As String
Dim sAccountNumberFromColumnAG As String
Dim sPreviousConcatenation As String
Dim sBankKeyFromColumnAF As String
Dim sConcatenation As String
Dim sPassFailValue As String
Dim sRange As String
Dim sSortRange As String
Dim sVendorNameFromColumnH As String
Dim sVendorNamePrevious As String
''''''''''''''''''''''''''''''''''''''''''''''''
'Initialization
''''''''''''''''''''''''''''''''''''''''''''''''
'Make Sheet 'Load Data' the Active Sheet
Sheets("Load Data").Select
'Disable Events
'Inhibit automatic calculation on the Active Sheet
Application.EnableEvents = False
ActiveSheet.EnableCalculation = False
'Turn off AutoFilter
ActiveSheet.AutoFilterMode = False
'Get the Last Row
iLastRow = Cells.Find(What:="*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
'Remove all data from the failure Column 'BF'
sRange = "BF2:BF" & iLastRow
ActiveSheet.Range(sRange).ClearContents
'Create the range to be sorted e.g. 'A1:BG1436'
sSortRange = Left(sAutoFilterRANGE, Len(sAutoFilterRANGE) - 1) & iLastRow
''''''''''''''''''''''''''''''''''''''''''''''''
'Pass 1
''''''''''''''''''''''''''''''''''''''''''''''''
'Sort by Column H (Vendor Name) - Primary Key
'Sort by Column AF(Bank Key) - Secondary Key
'Sort by Column AG (Acct No) - Tertiary Key
Range(sSortRange).Sort _
Key1:=Range("H1"), Order1:=xlAscending, _
Key2:=Range("AF1"), Order2:=xlAscending, _
Key3:=Range("AG1"), Order3:=xlAscending, _
Header:=xlYes, _
OrderCustom:=1, _
MatchCase:=False, _
Orientation:=xlTopToBottom, _
DataOption1:=xlSortTextAsNumbers, _
DataOption2:=xlSortTextAsNumbers, _
DataOption3:=xlSortTextAsNumbers
sVendorNamePrevious = ""
'Loop Through the data
For iRow = nFirstDataROW To iLastRow
sVendorNameFromColumnH = Trim(Cells(iRow, "H"))
If sVendorNameFromColumnH <> sVendorNamePrevious Then
'Output Results for the previous Vendor if the old Vendor is different from the current vendor
If bHaveFailure = True Then
sPassFailValue = "FAILED"
Else
sPassFailValue = "OK"
End If
'Put the same Pass/Fail value in all rows for a specific Vendor Name
For i = 1 To iRowCountForThisVendor
Cells(iRow, "BF").Offset(-i, 0) = sPassFailValue
Next i
'Prepare for the NEXT Vendor Name
bHaveFailure = False
iRowCountForThisVendor = 0
End If
'Process only if the Vendor Name is NOT BLANK
If Len(sVendorNameFromColumnH) > 0 Then
'Increment the Count for this Vendor
iRowCountForThisVendor = iRowCountForThisVendor + 1
'Get the values in 'AF' and 'AG'
sBankKeyFromColumnAF = Trim(Cells(iRow, "AF"))
sAccountNumberFromColumnAG = Trim(Cells(iRow, "AG"))
'Get the concatenated value of'AF' and 'AG' for this row
sConcatenation = sBankKeyFromColumnAF & sAccountNumberFromColumnAG
'Set the failure flag if 'AF' and 'AG' are the same as the 'Previous values' of 'AF' and 'AG'
If iRowCountForThisVendor > 1 Then
If sConcatenation = sPreviousConcatenation Then
bHaveFailure = True
End If
End If
'Save the current value as the Previous value
sPreviousConcatenation = sConcatenation
End If
'Save the current value for comparison purposes later
sVendorNamePrevious = sVendorNameFromColumnH
Next iRow
'Output Results for the LAST Vendor Key
If bHaveFailure = True Then
sPassFailValue = "FAILED"
Else
sPassFailValue = "OK"
End If
'Put the same Pass/Fail value in all rows for a specific Vendor Name
'if the Vendor Name is NOT BLANK (Adjust the row number because 'for loop' increments past the last row)
iRow = iRow - 1
For i = 1 To iRowCountForThisVendor
Cells(iRow, "BF").Offset(-i, 0) = sPassFailValue
Next i
''''''''''''''''''''''''''''''''''''''''''''''''
'Pass 2
''''''''''''''''''''''''''''''''''''''''''''''''
'Sort by Column H (Vendor Name) - Primary Key
'Sort by Column D (Account Group) - Secondary Key
Range(sSortRange).Sort _
Key1:=Range("H1"), Order1:=xlAscending, _
Key2:=Range("D1"), Order2:=xlAscending, _
Header:=xlYes, _
OrderCustom:=1, _
MatchCase:=False, _
Orientation:=xlTopToBottom, _
DataOption1:=xlSortNormal, _
DataOption2:=xlSortNormal
'Initialize variables
sVendorNamePrevious = Trim(Cells(nFirstDataROW, "H"))
sAcccountKeyPrevious = Trim(Cells(nFirstDataROW, "D"))
'Loop Through the data
For iRow = nFirstDataROW + 1 To iLastRow
'Get the input data
sVendorNameFromColumnH = Trim(Cells(iRow, "H"))
sAcccountKeyFromColumnD = Trim(Cells(iRow, "D"))
'Output 'FAILED' for this row and the previous row if the values match
'otherwise output 'OK', unless the previous value was 'FAILED' (then do nothing)
If sVendorNameFromColumnH = sVendorNamePrevious And sAcccountKeyFromColumnD = sAcccountKeyPrevious Then
Cells(iRow, "BF") = "FAILED"
Cells(iRow, "BF").Offset(-1, 0) = "FAILED"
Else
sPassFailValue = Cells(iRow, "BF").Offset(-1, 0)
If sPassFailValue <> "FAILED" Then
Cells(iRow, "BF").Offset(-1, 0) = "OK"
End If
End If
'Save the current values as the Previous values
If iRow <> iLastRow Then
sVendorNamePrevious = sVendorNameFromColumnH
sAcccountKeyPrevious = sAcccountKeyFromColumnD
End If
Next iRow
'Output Results for the LAST Vendor Vendor Name
'Row Number is ONE ROW after the last row (because 'for loop' increments past the last row)
If sVendorNameFromColumnH = sVendorNamePrevious And sAcccountKeyFromColumnD = sAcccountKeyPrevious Then
Cells(iRow, "BF").Offset(-1, 0) = "FAILED"
Else
sPassFailValue = Cells(iRow, "BF").Offset(-1, 0)
If sPassFailValue <> "FAILED" Then
Cells(iRow, "BF").Offset(-1, 0) = "OK"
End If
End If
''''''''''''''''''''''''''''''''''''''''''''''''
'Termination
''''''''''''''''''''''''''''''''''''''''''''''''
'Turn On AutoFilter
ActiveSheet.Range(sAutoFilterRANGE).AutoFilter
'Enable Events
'Enabled automatic calculation on the Active worksheet (and calculate now)
Application.EnableEvents = True
ActiveSheet.EnableCalculation = True
End Sub
Sub MacroForColumnBG()
'This puts 'OK' or 'FAILED in Column 'BG' based on the following:
'
'Examine the contents of:
'Column H (Vendor Name)
'Column D (Account Group)
'
'If there is a duplicate Vendor Name and the Account Group is either either ZM05 or ZM014,
'then the country (column P) associated to it should be the same as the the country (column P) associated to ZM01.
'There can be multiple 'ZM01' codes - to PASS a line must match one of them.
'If fail, then mark all the lines failed, otherwise OK
Dim i As Long
Dim iCountryCodesForZM01 As Long
Dim iRow As Long
Dim iRowCountForThisVendor As Long
Dim iLastRow As Long
Dim bHaveCountryCodeMatch As Boolean
Dim bHaveFailure As Boolean
Dim sAcccountKeyFromColumnD As String
Dim sAcccountKeyPrevious As String
Dim sCountryCodeFromColumnP As String
Dim sCountryCodeForZM01() As String
Dim sPassFailValue As String
Dim sRange As String
Dim sSortRange As String
Dim sVendorNameFromColumnH As String
Dim sVendorNamePrevious As String
''''''''''''''''''''''''''''''''''''''''''''''''
'Initialization
''''''''''''''''''''''''''''''''''''''''''''''''
'Make Sheet 'Load Data' the Active Sheet
Sheets("Load Data").Select
'Disable Events
'Inhibit automatic calculation on the Active Sheet
Application.EnableEvents = False
ActiveSheet.EnableCalculation = False
'Turn off AutoFilter
ActiveSheet.AutoFilterMode = False
'Get the Last Row
iLastRow = Cells.Find(What:="*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
'Remove all data from the failure Column 'BG'
sRange = "BG2:BG" & iLastRow
ActiveSheet.Range(sRange).ClearContents
'Create the range to be sorted e.g. 'A1:BG1436'
sSortRange = Left(sAutoFilterRANGE, Len(sAutoFilterRANGE) - 1) & iLastRow
''''''''''''''''''''''''''''''''''''''''''''''''
'Calculate Results
''''''''''''''''''''''''''''''''''''''''''''''''
'Sort by Column H (Vendor Name) - Primary Key
'Sort by Column D (Account Group) - Secondary Key
Range(sSortRange).Sort _
Key1:=Range("H1"), Order1:=xlAscending, _
Key2:=Range("D1"), Order2:=xlAscending, _
Header:=xlYes, _
OrderCustom:=1, _
MatchCase:=False, _
Orientation:=xlTopToBottom, _
DataOption1:=xlSortNormal, _
DataOption2:=xlSortNormal
'Initialize variables
sVendorNamePrevious = ""
sAcccountKeyPrevious = ""
iRowCountForThisVendor = 0
iCountryCodesForZM01 = 0
ReDim sCountryCodeForZM01(1 To 1)
'Loop Through the data
For iRow = nFirstDataROW To iLastRow
'Get the input data
sVendorNameFromColumnH = Trim(Cells(iRow, "H"))
sAcccountKeyFromColumnD = Trim(Cells(iRow, "D"))
'Output Results for the previous Vendor if the old Vendor is different from the current vendor
If sVendorNameFromColumnH <> sVendorNamePrevious Then
If bHaveFailure = True Then
sPassFailValue = "FAILED"
Else
sPassFailValue = "OK"
End If
'Put the same Pass/Fail value in all rows for a specific Vendor Key
For i = 1 To iRowCountForThisVendor
Cells(iRow, "BG").Offset(-i, 0) = sPassFailValue
Next i
'Prepare for the NEXT Vendor Key
bHaveFailure = False
iRowCountForThisVendor = 0
iCountryCodesForZM01 = 0
ReDim sCountryCodeForZM01(1 To 1)
End If
'Increment the Count for this Vendor Key
iRowCountForThisVendor = iRowCountForThisVendor + 1
'Get the 'Country Code'
sCountryCodeFromColumnP = Trim(Cells(iRow, "P"))
'Make the Pass/Fail Test
If iCountryCodesForZM01 > 0 Then
If sAcccountKeyFromColumnD = "ZM05" Or sAcccountKeyFromColumnD = "ZM14" Then
'Check the country code against all 'ZM01' country codes
bHaveCountryCodeMatch = False
For i = 1 To iCountryCodesForZM01
If sCountryCodeFromColumnP = sCountryCodeForZM01(i) Then
bHaveCountryCodeMatch = True
Exit For
End If
Next i
bHaveFailure = Not bHaveCountryCodeMatch
End If
End If
'If the country code is 'ZM01' add the country code to the list for this vendor
If sAcccountKeyFromColumnD = "ZM01" Then
iCountryCodesForZM01 = iCountryCodesForZM01 + 1
ReDim Preserve sCountryCodeForZM01(1 To iCountryCodesForZM01)
sCountryCodeForZM01(iCountryCodesForZM01) = sCountryCodeFromColumnP
End If
'Save the current values as the Previous value
sVendorNamePrevious = sVendorNameFromColumnH
Next iRow
'Output results for the items associated with the last row
'NOTE" Row number is one greater than the last row because 'for loop' increments past the last row
If bHaveFailure = True Then
sPassFailValue = "FAILED"
Else
sPassFailValue = "OK"
End If
'Put the same Pass/Fail value in all rows for a specific Vendor Key
For i = 1 To iRowCountForThisVendor
Cells(iRow, "BG").Offset(-i, 0) = sPassFailValue
Next i
''''''''''''''''''''''''''''''''''''''''''''''''
'Termination
''''''''''''''''''''''''''''''''''''''''''''''''
'Turn On AutoFilter
ActiveSheet.Range(sAutoFilterRANGE).AutoFilter
'Enable Events
'Enabled automatic calculation on the Active worksheet (and calculate now)
Application.EnableEvents = True
ActiveSheet.EnableCalculation = True
End Sub
Bookmarks