Solved

Have Visual Basic Assign a Purchase Order No.

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

867 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

21 Experts available now in Live!

Get 1:1 Help Now