Solved

Have Visual Basic Assign a Purchase Order No.

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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
MsgBox 2 57
Question to Pivot table 1 53
VIDEO DATASOURCE Control Resize issue 2 20
C# LINQ ForEach() question 6 28
The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
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…

831 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