Question:
Need a specific formula for Excel?
Malgorzata Z
2007-08-15 06:28:40 UTC
I'm tryin' to automate procedures at my work by the use of excel spreadsheets in place of Purchase Order books. Soooo, i've designed a sheet, but as every purchase order has its own number, I need a formula that will generate a random number and also... this is very important... one that will update itself every time the Excel template is opened (that way each purchase order will have a DIFFERENT number and the user enterting the information will not have to modify the formula).

I've started working with the RAND function in excel, which is FINE once i format it so that it gives me a 3 or 4 digit number, but I haven't figured out a way to make the sheet update itself every time it's opened without altering it.

Any help is greatly appreciated.
Thanks!
:)
Four answers:
2007-08-15 07:13:06 UTC
It seems that the RAND number has the risk of creating duplicate numbers sooner or later.



I would have thought you would want a sequential number.



If you could maintain a list of the used numbers in a separate file, you could have the template find the MAX number used so far, and add one.



The trick would be maintaining that master list of numbers up to date.
TreyJ
2007-08-15 07:21:15 UTC
Are you not using a database to store anything at all? This seems very limiting... it is going to get pretty ugly when there are hundreds or even thousands of Purchase Orders. You won't be able to search through them or aggregate any of the information.



But regardless, if that's what you want...



Your problem is that each PO doesn't know about any other PO. If it did, it could just add 1 to the previous one. But since it doesn't, you have to create a unique identifier. I see two ways to accomplish that. You could create a GUID, or just use the date/time.



A GUID is a Globally Unique IDentifier. It is a little ugly to generate one from within Excel, but it is guaranteed to be unique. The other problem with GUIDs is that they are pretty long (32 characters). You'll have to call a COM object or a .NET assembly to get it. This will have to be done from VBA.



This function will generate a GUID with the curly braces and dashes removed:



Function GenGuid() As String

Dim TypeLib As Object

Dim Guid As String



Set TypeLib = CreateObject("Scriptlet.TypeLib")

Guid = TypeLib.Guid



' format is {24DD18D4-C902-497F-A64B-28B2FA741661}



Guid = Replace(Guid, "{", "")

Guid = Replace(Guid, "}", "")

Guid = Replace(Guid, "-", "")



GenGuid = Guid

End Function



The other way is to just use the date/time. This will only be useful if these are created one at a time on a single machine. If you're passing the program around to a bunch of people, you might have conflicts. But it could be pretty small (14 characters). Such as 20070815094815. You could even eliminate the century and possibly the seconds to get it down to 10 characters. Or you could just have the value of a field be =now() and then set the format to "number". You will get something like 39309.41. This is a textual version:



=TEXT(YEAR(NOW()),"0000")&TEXT(MONTH(NOW()),"00"&TEXT(DAY(NOW()),"00")) & TEXT(HOUR(NOW()),"00") & TEXT(MINUTE(NOW()),"00") & TEXT(SECOND(NOW()),"00")



The big trick will be that you only want to generate this number once, not every time the sheet recalculates.



You can have a macro run at the load of a file using the Workbook_Open() event. I suggest keeping another (hidden) field with a flag to indicate if you've generated the data for this spreadsheet yet or not. Save the spreadsheet with a 0 in it, and when someone loads it and the macro runs for the first time, set it to 1 and create the unique ID. Then everytime it is loaded and the macro runs, it will see a 1 in the field and know not to overwrite what was generated originally. Good luck!
2007-08-15 06:57:18 UTC
I do not think this is possible because Excel ahs no way of running a piece of code on Open. The only option within Excels VBA is on Activate which I think will not work for your purposes. The only option would be to set the formula in a cell and then have the use enter something in a different cell where at that point the formula could then generate the Purchase Order number.
?
2016-12-15 20:49:58 UTC
The values interior the fields (which you do not define right here) are what are inflicting the formatting themes. you % to exhibit the output as a decimal cost fairly than the completed selection which you're turning out to be. If this have been my spreadsheet, i could upload "/10000000" on the top of the formulation. this could provide you a value of ".3651001". Then format the cellular to exhibit 3 decimal places or ".365".


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...