Solved

Have Visual Basic Assign a Purchase Order No.

Posted on 2002-03-19
5
178 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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
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…

757 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now