Hi all,
First post here, but as I'm not entirely new to Excel and VBA (doing selfstudy for a few weeks in VBA and 10+ years of Excel experience in general), I'm hoping to return the service later on by helping others
So I'm creating an invoicing tool from scratch and I'm stuck with a certain listbox.
I'll try to explain as much as I can what I want this listbox to do.
So first, I generate certain invoice data in a userform (invoice date, subject, client, etc...). This data is written to sheet1 (facturatieoverzicht).
In this sheet1, I created a dynamic range :
Code:
=OFFSET(Facturatieoverzicht!$A$2;0;0;COUNTA(Facturatieoverzicht!$A:$A);COUNTA(Facturatieoverzicht!$10:$10))
Why do I only select 10 columns? Because I want the listbox to show only 5 columns (so the columnwidth of the other 5 is 0).
I have the listbox in sheet3 (factuurtemplate), which has the invoice template on the left and the listbox on the right.
I've linked sheet1 to this listbox with the ListFillRange property. The listbox data is: sequential number, client, invoice number, invoice date, amount.
Now, all invoices are showing up in this list. Based on the user selection, I've written a code to perform a vlookup and fill certain cells in the template based on the selected value.
once the invoice template is filled in with the data selected, the user is able to push the print or email button etc...
Here is the code that I execute as soon as a different item is selected in the list:
Code:
Sub listfacturen_change()
If listFacturen.List(listFacturen.ListIndex, 0) = "" Then This is to make sure no empty row is selected as this will return an runtime error. Should not be required if my dynamic range works properly...
MsgBox "Please select an invoice to display"
Else
Workbooks("Facturatie Latest.xlsm").Worksheets("factuurtemplate").Range("$g$5") = Application.VLookup(listFacturen.List(listFacturen.ListIndex, 0), Worksheets("facturatieoverzicht").Range("$a$2:$z$4000"), 5, 0)
Workbooks("Facturatie Latest.xlsm").Worksheets("factuurtemplate").Range("$g$6") = Application.VLookup(listFacturen.List(listFacturen.ListIndex, 0), Worksheets("facturatieoverzicht").Range("$a$2:$z$4000"), 4, 0)
Workbooks("Facturatie Latest.xlsm").Worksheets("factuurtemplate").Range("$g$7") = Application.VLookup(listFacturen.List(listFacturen.ListIndex, 0), Worksheets("facturatieoverzicht").Range("$a$2:$z$4000"), 6, 0)
Workbooks("Facturatie Latest.xlsm").Worksheets("factuurtemplate").Range("$D$12") = Application.VLookup(listFacturen.List(listFacturen.ListIndex, 0), Worksheets("facturatieoverzicht").Range("$a$2:$z$4000"), 3, 0)
Workbooks("Facturatie Latest.xlsm").Worksheets("factuurtemplate").Range("$D$20") = Application.VLookup(listFacturen.List(listFacturen.ListIndex, 0), Worksheets("facturatieoverzicht").Range("$a$2:$z$4000"), 14, 0)
Workbooks("Facturatie Latest.xlsm").Worksheets("factuurtemplate").Range("$G$20") = Application.VLookup(listFacturen.List(listFacturen.ListIndex, 0), Worksheets("facturatieoverzicht").Range("$a$2:$z$4000"), 15, 0)
Workbooks("Facturatie Latest.xlsm").Worksheets("factuurtemplate").Range("$D$21") = Application.VLookup(listFacturen.List(listFacturen.ListIndex, 0), Worksheets("facturatieoverzicht").Range("$a$2:$z$4000"), 16, 0)
Workbooks("Facturatie Latest.xlsm").Worksheets("factuurtemplate").Range("$G$21") = Application.VLookup(listFacturen.List(listFacturen.ListIndex, 0), Worksheets("facturatieoverzicht").Range("$a$2:$z$4000"), 17, 0)
Workbooks("Facturatie Latest.xlsm").Worksheets("factuurtemplate").Range("$D$22") = Application.VLookup(listFacturen.List(listFacturen.ListIndex, 0), Worksheets("facturatieoverzicht").Range("$a$2:$z$4000"), 18, 0)
Workbooks("Facturatie Latest.xlsm").Worksheets("factuurtemplate").Range("$G$22") = Application.VLookup(listFacturen.List(listFacturen.ListIndex, 0), Worksheets("facturatieoverzicht").Range("$a$2:$z$4000"), 19, 0)
Workbooks("Facturatie Latest.xlsm").Worksheets("factuurtemplate").Range("$D$23") = Application.VLookup(listFacturen.List(listFacturen.ListIndex, 0), Worksheets("facturatieoverzicht").Range("$a$2:$z$4000"), 20, 0)
Workbooks("Facturatie Latest.xlsm").Worksheets("factuurtemplate").Range("$G$23") = Application.VLookup(listFacturen.List(listFacturen.ListIndex, 0), Worksheets("facturatieoverzicht").Range("$a$2:$z$4000"), 21, 0)
Workbooks("Facturatie Latest.xlsm").Worksheets("factuurtemplate").Range("$D$24") = Application.VLookup(listFacturen.List(listFacturen.ListIndex, 0), Worksheets("facturatieoverzicht").Range("$a$2:$z$4000"), 22, 0)
Workbooks("Facturatie Latest.xlsm").Worksheets("factuurtemplate").Range("$G$24") = Application.VLookup(listFacturen.List(listFacturen.ListIndex, 0), Worksheets("facturatieoverzicht").Range("$a$2:$z$4000"), 23, 0)
Workbooks("Facturatie Latest.xlsm").Worksheets("factuurtemplate").Range("$G$33") = Application.VLookup(listFacturen.List(listFacturen.ListIndex, 0), Worksheets("facturatieoverzicht").Range("$a$2:$z$4000"), 7, 0)
Workbooks("Facturatie Latest.xlsm").Worksheets("factuurtemplate").Range("$G$34") = -Application.VLookup(listFacturen.List(listFacturen.ListIndex, 0), Worksheets("facturatieoverzicht").Range("$a$2:$z$4000"), 8, 0)
Workbooks("Facturatie Latest.xlsm").Worksheets("factuurtemplate").Range("$G$35") = Application.VLookup(listFacturen.List(listFacturen.ListIndex, 0), Worksheets("facturatieoverzicht").Range("$a$2:$z$4000"), 9, 0)
Workbooks("Facturatie Latest.xlsm").Worksheets("factuurtemplate").Range("$G$36") = Application.VLookup(listFacturen.List(listFacturen.ListIndex, 0), Worksheets("facturatieoverzicht").Range("$a$2:$z$4000"), 10, 0)
Workbooks("Facturatie Latest.xlsm").Worksheets("factuurtemplate").Range("$G$37") = Application.VLookup(listFacturen.List(listFacturen.ListIndex, 0), Worksheets("facturatieoverzicht").Range("$a$2:$z$4000"), 11, 0)
Workbooks("Facturatie Latest.xlsm").Worksheets("factuurtemplate").Range("$G$38") = Application.VLookup(listFacturen.List(listFacturen.ListIndex, 0), Worksheets("facturatieoverzicht").Range("$a$2:$z$4000"), 12, 0)
Workbooks("Facturatie Latest.xlsm").Worksheets("factuurtemplate").Range("$G$39") = Application.VLookup(listFacturen.List(listFacturen.ListIndex, 0), Worksheets("facturatieoverzicht").Range("$a$2:$z$4000"), 13, 0)
Workbooks("Facturatie Latest.xlsm").Worksheets("factuurtemplate").Range("$D$13") = Application.VLookup(listFacturen.List(listFacturen.ListIndex, 2), Worksheets("klantendatabase").Range("$a$2:$z$4000"), 5, 0) & " " & Application.VLookup(listFacturen.List(listFacturen.ListIndex, 2), Worksheets("klantendatabase").Range("$a$2:$z$4000"), 6, 0)
Workbooks("Facturatie Latest.xlsm").Worksheets("factuurtemplate").Range("$D$14") = Application.VLookup(listFacturen.List(listFacturen.ListIndex, 2), Worksheets("klantendatabase").Range("$a$2:$z$4000"), 7, 0) & " " & Application.VLookup(listFacturen.List(listFacturen.ListIndex, 2), Worksheets("klantendatabase").Range("$a$2:$z$4000"), 8, 0)
Workbooks("Facturatie Latest.xlsm").Worksheets("factuurtemplate").Range("$D$13") = Application.VLookup(listFacturen.List(listFacturen.ListIndex, 2), Worksheets("klantendatabase").Range("$a$2:$z$4000"), 5, 0) & " " & Application.VLookup(listFacturen.List(listFacturen.ListIndex, 2), Worksheets("klantendatabase").Range("$a$2:$z$4000"), 6, 0)
Workbooks("Facturatie Latest.xlsm").Worksheets("factuurtemplate").Range("$D$15") = Application.VLookup(listFacturen.List(listFacturen.ListIndex, 2), Worksheets("klantendatabase").Range("$a$2:$z$4000"), 12, 0)
Workbooks("Facturatie Latest.xlsm").Worksheets("factuurtemplate").Range("$D$16") = Application.VLookup(listFacturen.List(listFacturen.ListIndex, 2), Worksheets("klantendatabase").Range("$a$2:$z$4000"), 11, 0)
Workbooks("Facturatie Latest.xlsm").Worksheets("factuurtemplate").Range("$D$17") = Application.VLookup(listFacturen.List(listFacturen.ListIndex, 2), Worksheets("klantendatabase").Range("$a$2:$z$4000"), 10, 0)
End If
End Sub
So just to be clear: this code works as I expected it to work, but I have some additional questions to finetune it:
1) If I open another workbook with macro's in it, it will give me a runtime error on this code instantly. I was able to fix this by adding a code to activate the workbook 'facturatie latest' first. However, as soon as I added a tab to this workbook (the invoice workbook), it would show another error in this code.
So any advice on how to debug this code would be great... is there something like 'if the code returns an error, don't do anything and don't pop up with the error message'?
2) I would like to add a searchbox (textbox) above the listbox. If I type any value that exists in one of the 5 columns, it should filter those rows out.
I know that there are many examples on the internet (and here), but none of them seem to work in my case. I'm guessing it's related to the fact that I have code that executes as soon as the listbox changes? Is there a workaround, without losing the 'click item => template is filled' functionality?
3) So there's also a problem with my dynamic range. It shows all rows (1-4000) that I filled with formula's. Column A is the sequential numbering of this code:
Code:
=IF(C5="";"";A4+1)
(column C is the client field, which is automatically filled when the user creates an invoice in my userform). So column A returns an empty cell when there is no client filled in.
Of course, I just want it to show the filled in rows instead of all 4000 (I just chose 4000 to limit the file size). I've created dynamic ranges before and those worked perfectly. However this one doesn't seem to be working... Should I base the code on a column without formulas instead, for example C?
4) Currently, my listbox is set up as multiselectSingle. However, I want it to improve by adding the following functionality:
If the user selects more than 1 invoice (let's say the last 10 invoices), a msgbox should pop up and ask to confirm if they want to save them all as pdf or save and send each invoice seperately to the emailadress in the invoice template (cell D16).
I suppose I should work with a loop function and execute code if the item is selected.
I'm just not sure how I should implement this in my code without adding more chance of errors...
I really have to dig into listboxes further, so I'm not 100% sure that the above is possible to be combined in 1 listbox.
If there is anyone able to help me out, that would be awesome!
Thanks a lot in advance!!!