Select and view invoice [VBA]
This post demonstrates how to view saved invoices based on the invoice number using a userform. The userform appears when the user clicks on button "View Invoice", see image above.
The userform contains:
- Listbox populated with unique distinct invoice numbers.
- A "Cancel" button that unloads the userform.
- A button "View Invoice data" that will start the macro. The macro will extract the invoice data from another sheet based on the selected invoice number in the listbox.
Cancel Command button
- Press Alt-F11 to open the visual basic editor.
- Right-click on Userform1.
- Click "View Code" and the code module will appear.
- Copy and paste "Sub Button1_Click" and "CommandButton2_Click" code below to code module.
'The following macro will remove the userform if visible to the user Private Sub CommandButton1_Click() Unload UserForm1 End Sub
"View Invoice data" Command button
Copy and paste "Sub Button2_Click" code below to code module
Private Sub CommandButton2_Click() 'Declare variables Dim RowStart As Long Dim RowEnd As Long 'Find invoice range RowStart = Sheets("Invoice data").Columns("A").Find(ListBox1.Value, _ SearchOrder:=xlRows, LookAt:=xlWhole, SearchDirection:=xlNext, _ LookIn:=xlValues).Row RowEnd = Sheets("Invoice data").Columns("A").Find(ListBox1.Value, _ SearchOrder:=xlRows, LookAt:=xlWhole, SearchDirection:=xlPrevious, _ LookIn:=xlValues).Row + 1 'Clear range Sheets("Invoice").Range("B16:E38").ClearContents 'Copy values from sheet Invoice data Sheets("Invoice").Range("B16").Resize(RowEnd - RowStart, 4).Value = _ Sheets("Invoice data").Range("D" & RowStart & ":G" & RowEnd).Value Sheets("Invoice").Range("F3").Value = Sheets("Invoice data").Range("B" _ & RowStart).ValueSheets("Invoice").Range("D13").Value = _ Sheets("Invoice data").Range("A" & RowStart) Sheets("Invoice").Range("B8").Value = Sheets("Invoice data").Range("C" _ & RowStart) 'Hide the userform Unload UserForm1 End Sub
Invoice template with dependent drop down lists
This article demonstrates a basic invoice template I created. It lets you use dropdown lists to quickly select products on […]
Rattan asks: In my workbook I have three worksheets; "Customer", "Vendor" and "Payment". In the Customer sheet I have a […]
This article demonstrates a macro that copies values between sheets. I am using the invoice template workbook. This macro copies […]
Open Excel files in a folder [VBA]
This tutorial shows you how to list excel files in a specific folder and create adjacent checkboxes, using VBA. The […]
Split data across multiple sheets [VBA]
In this post I am going to show how to create a new sheet for each airplane using vba. The […]
Identify missing numbers in a column
The image above shows an array formula in cell D6 that extracts missing numbers i cell range B3:B7, the lower […]
Working with COMBO BOXES [Form Controls]
This blog post demonstrates how to create, populate and change comboboxes (form control) programmatically. Form controls are not as flexible […]
4 Responses to “Select and view invoice [VBA]”
Leave a Reply
How to comment
How to add a formula to your comment
<code>Insert your formula here.</code>
Convert less than and larger than signs
Use html character entities instead of less than and larger than signs.
< becomes < and > becomes >
How to add VBA code to your comment
[vb 1="vbnet" language=","]
Put your VBA code here.
[/vb]
How to add a picture to your comment:
Upload picture to postimage.org or imgur
Paste image link to your comment.
Thanks for this useful file, is there a file with statement of account, show invoice and payment.
Excel 2016
Run-time error '457'
How Fixing code
Test.Add Value, CStr(Value)
Excel 2016
Run-time error '457'
How Fixing code
Test.Add Value, CStr(Value)
hi. in select and view invoice with listbox,when i clear content range D7:I16 but do not remove formulas from rang D7:I16, i use this code:
'Clear range
Set rConstants = Sheets("Invoice Form").Range("D7:I16").SpecialCells(xlCellTypeConstants)
rConstants.ClearContents
but this code remove formulas
pls help me