Solved

Have Visual Basic Assign a Purchase Order No.

Posted on 2002-03-19
5
186 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
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…
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…
Suggested Courses
Course of the Month7 days, 14 hours left to enroll

617 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