chirayu
Well-Known Member
Hi Guys,
Need help with my LOOP code. Also given below is the private sub it is running.
Basically what I want it to do is in column D, there are invoice numbers, It needs to loop through all files in The invoice folder for each invoice in column D. The reason is that the invoice files received have no specific name. Only the contents act as identifiers. so it must go through all invoice files to match against individual cell/ group in column D.
Need help with my LOOP code. Also given below is the private sub it is running.
Basically what I want it to do is in column D, there are invoice numbers, It needs to loop through all files in The invoice folder for each invoice in column D. The reason is that the invoice files received have no specific name. Only the contents act as identifiers. so it must go through all invoice files to match against individual cell/ group in column D.
Code:
'Global Variables
Dim DebsPASPath As String
Dim DebsPASFile As String
Dim DebsPASSheet As String
Dim DebsPASInvoiceFile As String
Dim DebsPASInvoiceSheet As String
Sub DebenhamsPASVerify()
'Please ensure there is an Invoice folder in the same folder as the PAS file.
'Place all the converted excel invoices in that folder before using this macro.
Dim FolderPath As String
Dim FileName As String
Dim OpenInvoice As Workbook
Range("D2").Select
DebsPASPath = ActiveWorkbook.Path
DebsPASFile = ActiveWorkbook.Name
DebsPASSheet = ActiveSheet.Name
FolderPath = DebsPASPath & "\Invoice\"
FileName = Dir(FolderPath & "*.xlsx")
Do While FileName <> ""
Set OpenInvoice = Workbooks.Open(FolderPath & FileName)
DebsPASInvoiceFile = ActiveWorkbook.Name
DebsPASInvoiceSheet = ActiveSheet.Name
'NYK Invoice
Application.Run "Personal.xlsb!DebenhamsPasVerifyNYK"
Workbooks(DebsPASInvoiceFile).Close savechanges:=False
FileName = Dir
Loop
Range("O1").ClearContents
MsgBox "Macro completed. Please check values in Red as they are +/- 10 pence of the Ivoice total", vbInformation, ""
End Sub
Private Sub DebenhamsPasVerifyNYK()
Dim Cont As String 'pas container
Dim ContFound As Variant 'find pas container in invoice file
Dim InvNo As String 'pas invoice number
Dim InvNoFound As Variant 'find pas invoice number in invoice file
Dim Masn As String 'pas masn number
Dim MasnFound As Variant 'find pas masn number in invoice file
Dim InvDT As String 'pas invoice date
Dim InvDTFound As Variant 'find pas invoice date in invoice file
Dim VslName As String 'pas vessel name
Dim VslNameFound As Variant 'find pas vessel name in invoice file
Dim FrstRow As String 'first pas masn row for same masn
Dim FrstVal As String 'first pas masn cell value for same masn
Dim LstRow As String 'last pas masn row for same masn
Dim InvVal As Double 'pas total masn invoice cost
Dim InvValFound As Double 'find pas total masn invoice cost in invoice file
Dim InvValDiff As Double '[InValDiff = InVal - InValFound]
Dim InvValMatch As Double 'TRUE if: [InvValDiff = InvValFound] or [InvValMatch < 0.11 AND InvValMatch > -0.11]
Dim Supplier As Variant 'carrier name
Set Supplier = Cells.Find(What:="Nippon Yusen Kabushiki Kaisha", LookIn:=xlValues)
If Not Supplier Is Nothing Then
Windows(DebsPASFile).Activate
Worksheets(DebsPASSheet).Select
FrstVal = ActiveCell.Value
Do Until FrstVal <> ActiveCell.Value
'Calculate First & Last row for same Masn
'----------------------------------------
Do Until ActiveCell.Value <> ActiveCell.Offset(1, 0).Value
ActiveCell.Offset(1, 0).Select
Loop
LstRow = ActiveCell.Row
Do Until ActiveCell.Value <> ActiveCell.Offset(-1, 0).Value
ActiveCell.Offset(-1, 0).Select
Loop
FrstRow = ActiveCell.Row
FrstVal = ActiveCell.Value
'Sum the costvalue for that Masn
'-------------------------------
Range("O1").Formula = "=Sum(H" & FrstRow & ":H" & LstRow & ")"
InvVal = Range("O1").Value
'Set all the variable to be matched individually against invoice file
'--------------------------------------------------------------------
Cont = Range("F" & ActiveCell.Row).Value
InvNo = Left(Range("D" & ActiveCell.Row).Value, 3) & " " & Right(Range("D" & ActiveCell.Row).Value, 7)
Masn = Range("C" & ActiveCell.Row).Value
InvDT = Format(Range("E" & ActiveCell.Row).Value, "dd mmm yyyy")
VslName = Range("B" & ActiveCell.Row).Value
'Go to Invoice file
'------------------
Windows(DebsPASInvoiceFile).Activate
Worksheets(DebsPASInvoiceSheet).Select
'Remove "Vessel Voyage Bound" from merging with the Vessel Name
'--------------------------------------------------------------
Cells.Replace What:="VESSEL VOYAGE", Replacement:=" ", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
Cells.Replace What:="BOUND", Replacement:=" ", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
'Set the container & vessel variable to be matched automatically against invoice file
'------------------------------------------------------------------------------------
Set ContFound = Cells.Find(What:=Cont, LookIn:=xlFormulas)
Set VslNameFound = Cells.Find(What:=VslName, LookIn:=xlFormulas)
If Not ContFound Is Nothing And Not VslNameFound Is Nothing Then
Cells.Find(What:="AMOUNT DUE", LookIn:=xlFormulas).Activate
Selection.End(xlToRight).Select
InvValFound = ActiveCell.Value
Cells.Find(What:="ISSUE DATE", LookIn:=xlFormulas).Activate
Selection.End(xlToRight).Select
InvDTFound = ActiveCell.Value
Cells.Find(What:="INVOICE NO.", LookIn:=xlFormulas).Activate
Selection.End(xlToRight).Select
InvNoFound = ActiveCell.Value
Cells.Find(What:="REFERENCE", LookIn:=xlFormulas).Activate
Selection.End(xlDown).Select
MasnFound = ActiveCell.Value
'Check if InvVal is +/-10pence of the InvValFound
'------------------------------------------------
InvValDiff = InvVal - InvValFound
If InvVal = InvValFound Then
InvValMatch = True
ElseIf InvValDiff < 0.11 And InvValDiff > -0.11 Then
InvValMatch = True
Else
InvValMatch = False
End If
'Go to PAS file
'--------------
Windows(DebsPASFile).Activate
Worksheets(DebsPASSheet).Select
'Check if manual variables match & then colour
'---------------------------------------------
If InvValMatch = True _
And InvDT = InvDTFound _
And InvNo = InvNoFound _
And Masn = MasnFound Then
Range("H" & FrstRow & ":H" & LstRow).Interior.Color = RGB(146, 208, 80) 'Green is Good
Else
Range("H" & FrstRow & ":H" & LstRow).Interior.Color = RGB(255, 0, 0) 'Red is Bad
End If
Range("C" & LstRow).Select
ActiveCell.Offset(1, 0).Select
Else
Windows(DebsPASFile).Activate
Worksheets(DebsPASSheet).Select
Range("D" & LstRow).Select
ActiveCell.Offset(1, 0).Select
End If
Loop
Else
End If
End Sub