简体   繁体   中英

Excel Vba Function for a counter

I need to keep memorized a progressive number in a function.

I have a file with some invoices to be repeated in a new sheet, the invoices have a progressive number. This number is relevant to avoid double registration in the new sheet. How can I keep this number in a function and start from it for updating.

At the moment I am using a cell in a worksheet (but I 'd like to use a function)

protocol_number = activecell.value (I get the prot nr in a sub)
range("a1000")=protocol_number

different sub
if Range("a1000") = activecell.value then 

Msgbox "the protocol number has been registered yet"
else
update code

Thanks for helping me

I think I was not so clear. I'm sorry about that. I'll try to clarify it.

2 workbooks First one is a summary of a huge detailed (2nd) workbook for a big Financial Company, with several invoices entered by an employee. Invoice numbers are different and alphanumeric (we are in Italy and it's possible). But it is mandatory in Italian accounting to set all the invoice entering by a progressive number called protocol number. This protocol number marks the line of input with invoice number, amount, company, vat, and date of issue.

Transferring the data to the summary workbook I always let the code start from the beginnig (as the pick up of invoices is always retrieved from a different Financial operating software, which creates a new excel file with all invoices, even the ones already reported on the summary workbook), keep looking to the protocol nr, match with the last one entered and from the matched one keep updating the summary workbook. (remember the protocol number in the only data not transferred in the summary workbook, if it was I could match it) Now I used to put the last protocol number used in a cell which can be cancelled or overwritten or other. So I was asking if there is a function (and you clerly said is not possible) that keeps the last protocol number entered and on the next updating match it with the invoice workbook and go further with updating if needed. I do not post the code as I am still using the cell as protocol number keeper.

Hope this is clear

You need a so-called static variable :

Sometimes you want to retain the value of a variable in Excel VBA when a procedure ends. You can achieve this by using the Static keyword.

Static protocol_number As Integer

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM