Solved

Have Visual Basic Assign a Purchase Order No.

Posted on 2002-03-19
5
182 Views
Last Modified: 2013-11-25
I have a spreadsheet for ordering accessories. Right now I have a macro button that when clicked copies a number from worksheet 2, Cell A1 and pastes it on the order worksheet, it then goes back to sheet 2 and deletes row 1 so the purchase order number isnt used twice. The next time someone clicks the macro button labeled "Enter P.O." it will grab the next number in succession. On worksheet 2 I have numbers filled in from A1 down to A200.
Is there a way Visual Basic will assign a random number and make sure this number isnt repeated again?
Here is the macro I wrote below....
Sub Enter_Po_Number()
    Sheets("P.O. Nos").Visible = True
    Sheets("P.O. Nos").Select
    Range("A1").Select
    Selection.Copy
    Sheets("Access & Couplers").Select
    Range("I9").Select
    ActiveSheet.Paste
    Sheets("P.O. Nos").Select
    Rows("1:1").Select
    Application.CutCopyMode = False
    Selection.Delete Shift:=xlUp
    ActiveWindow.SelectedSheets.Visible = False
    Sheets("Access & Couplers").Select
End Sub

I guess Im looking for a way to eliminate this extra worksheet named "P.O. Nos" where I have Cells A1:A200 filled in with numbers and going to this worksheet later on when it reaches the end number.
Example....A1 has a number that reads Y6001 and A200 has the number Y6201.
Im not sure if Visual Bacic CAN assign random numbers that will NOT repeat themselves as you know its very scary to end up with two orders with the same Purchase Order.

Please Help
He4Giv@Aol.Com
0
Comment
Question by:He4Giv
5 Comments
 
LVL 6

Expert Comment

by:xSinbad
ID: 6881310
You could use the date and time functions like this.


Dim PoNumber
PoNumber = day(date)& month(date)& year(date)& minute(date) & second(date)

this produce a unique number baed on the date/time/secinds so that it will never be repeated again.

Cheers
Marcus

0
 
LVL 1

Expert Comment

by:superchook
ID: 6881644
I personally would use "yymmdd_HHnn" so that they sort easily e.g. "020320_1413.xxx"
0
 
LVL 43

Accepted Solution

by:
TimCottee earned 50 total points
ID: 6882087
If you really want random numbers then use this combination:

Private Sub CommandButton1_Click()
    Dim intColumn As Integer
    Dim strNumber As String
    intColumn = ActiveCell.Column
    strNumber = PONumber
    Do While Not (ActiveSheet.Columns(intColumn).Find(strNumber) Is Nothing)
        strNumber = PONumber
    Loop
    ActiveCell.Value = strNumber
End Sub

Public Function PONumber() As String
    Dim sngNumber As Single
    sngNumber = Rnd(Now) * 1000000
    PONumber = Format(sngNumber, "0000000")
End Function

All this does is generate a random number and test whether it exists in the column of the activecell. If it does then it gets another number and uses that.
0
 
LVL 1

Expert Comment

by:superchook
ID: 6884025
Or - of course use a GUID variable
32 digits (plus hyphens)
0
 

Author Comment

by:He4Giv
ID: 6884139
Im lost...Your answer looks well done and I pasted it into my Excel worksheet but I dont know how to get it running.
I never used Functions via Visual Basic.
I did add the CommandButton1 to a inserted blank worksheet but dont understand how to get it wroking?

He4Giv
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

808 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question