If, Then, Else Test for Invoice number before adding records to list.

JohnZ1156

Board Regular
Joined
Apr 10, 2021
Messages
160
Office Version
  1. 2021
Platform
  1. Windows
I have a simple invoice sheet. On it there is a pregenerated invoice nuumber. After filling in the invoice data , I have a macro that will copy the details to an "Invoice List" sheet, however, I don't want it to copy if it has already copied the data. Best way I can think of is to check for the invoice number.

For example:
If the current invoice number (InvNo) is equal to the maxumum invoice number on the "Invoice List" sheet, (Column A)

then message, "Invoice already exists", and do nothing.

Else copy data to "List sheet" and clear invoice sheet ready for next entry.

Here are my variables:
Current Invoice number = InvNo
Invoice number column in list sheet is column A
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
I have a simple invoice sheet. On it there is a pregenerated invoice nuumber. After filling in the invoice data , I have a macro that will copy the details to an "Invoice List" sheet, however, I don't want it to copy if it has already copied the data. Best way I can think of is to check for the invoice number.

For example:
If the current invoice number (InvNo) is equal to the maxumum invoice number on the "Invoice List" sheet, (Column A)

then message, "Invoice already exists", and do nothing.

Else copy data to "List sheet" and clear invoice sheet ready for next entry.

Here are my variables:
Current Invoice number = InvNo
Invoice number column in list sheet is column A
Use (iserror (vlookup in the macro to run only if the result is true. it means that if the value is not present in list then run macro only.
 
Upvote 0
Something like this:-

VBA Code:
Sub TestLookForInvNo()

    Dim InvNo As Long
    Dim wsInvList As Worksheet
    
    InvNo = 11        ' Feed in your Invoice Number
    
    Set wsInvList = Worksheets("Invoice List")
    
    If IsError(Application.Match(InvNo, wsInvList.Columns("A"), 0)) = False Then
        MsgBox "Invoice already exists"
        Exit Sub
    End If
    
    ' Do something

End Sub
 
Upvote 0

Forum statistics

Threads
1,215,741
Messages
6,126,610
Members
449,321
Latest member
syzer

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top