+ Reply to Thread
Results 1 to 13 of 13

adding consecutive number to userform textbox

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    01-28-2007
    MS-Off Ver
    2019
    Posts
    204

    adding consecutive number to userform textbox

    i've got a sample database (attached) with a userform for inputting of data. What i want it to do is automatically generate the next number and add it to textbox "our ref" on the userform. the number is in Col C. This is the code i've tried using the code highlighted in red which doesnt work..... can you point me in the right direction ?

    i've attached said file...

    Private Sub UserForm_Initialize()
    Application.ScreenUpdating = False
    Worksheets("DATA").Select
    Range("A2").Select
    Do Until ActiveCell.Value = ""
        UserForm1.ComboBox1.AddItem ActiveCell.Value
        ActiveCell.Offset(1, 0).Select
    Loop
    
    Worksheets("DATA").Select
    Range("B2").Select
    Do Until ActiveCell.Value = ""
        UserForm1.ComboBox2.AddItem ActiveCell.Value
        ActiveCell.Offset(1, 0).Select
    Loop
    
    Worksheets("DATA").Select
    Range("c2").Select
    Do Until ActiveCell.Value = ""
        UserForm1.ComboBox3.AddItem ActiveCell.Value
        ActiveCell.Offset(1, 0).Select
    Loop
    Worksheets("RESULTS").Select
    Range("C1").Select
    nextNumber = Sheets("RESULTS").Range("C1").Value + 1
    
        UserForm1.TextBox3.AddItem nextNumber.Value
        
      
        Loop
        
    Worksheets("Results").Select
    End Sub
    Attached Files Attached Files
    Last edited by royUK; 04-10-2008 at 08:53 AM.

  2. #2
    Forum Expert
    Join Date
    01-15-2007
    Location
    Brisbane, Australia
    MS-Off Ver
    2007
    Posts
    6,591
    Hi

    Try

    UserForm1.TextBox3.Value = nextNumber

    rylo

  3. #3
    Forum Contributor
    Join Date
    01-28-2007
    MS-Off Ver
    2019
    Posts
    204
    that gives me a number but it doesnt give me the next consecutive number

  4. #4
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200
    You don't need all those Loops nor Selections. Change the initialize code to

    Private Sub UserForm_Initialize()
        Application.ScreenUpdating = False
        With Worksheets("DATA")
            UserForm1.ComboBox1.List = .Range(.Cells(2, 1), .Cells(.Rows.Count, 1).End(xlUp)).Value
            UserForm1.ComboBox2.List = .Range(.Cells(2, 2), .Cells(.Rows.Count, 2).End(xlUp)).Value
            UserForm1.ComboBox3.List = .Range(.Cells(2, 3), .Cells(.Rows.Count, 3).End(xlUp)).Value
        End With
        nextnumber = Sheets("RESULTS").Range("C1").Value + 1
        UserForm1.TextBox3.Value = nextnumber    'ActiveCell.Value + 1
    End Sub
    Please don't use colours and large font in your code as above it is not helping us read it!

    A TextBox does not have an additem method.

    Search the Forum for some examples of userforms or look at the databaseform example here

    http://www.excel-it.com/vba_examples.htm
    Last edited by royUK; 04-10-2008 at 06:36 AM.
    Hope that helps.

    RoyUK
    --------
    For Excel Tips & Solutions, free examples and tutorials why not check out my web site

    Free DataBaseForm example

  5. #5
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200
    I think this is what you are trying to do

    Private Sub UserForm_Initialize()
    Dim nextnumber As Long
    Dim rNumbers As Range
    
    Set rNumbers = Sheets("RESULTS").Range(Cells(3, 3), Cells(Rows.Count, 3).End(xlUp))
        Application.ScreenUpdating = False
        With Worksheets("DATA")
            UserForm1.ComboBox1.List = .Range(.Cells(2, 1), .Cells(.Rows.Count, 1).End(xlUp)).Value
            UserForm1.ComboBox2.List = .Range(.Cells(2, 2), .Cells(.Rows.Count, 2).End(xlUp)).Value
            UserForm1.ComboBox3.List = .Range(.Cells(2, 3), .Cells(.Rows.Count, 3).End(xlUp)).Value
        End With
        nextnumber = Application.WorksheetFunction.Max(rNumbers) + 1
        MsgBox nextnumber
        UserForm1.TextBox3.Value = nextnumber    'ActiveCell.Value + 1
    End Sub
    Just my opinion but I would definitely lose the greenn background of the userform

  6. #6
    Forum Contributor
    Join Date
    01-28-2007
    MS-Off Ver
    2019
    Posts
    204
    This is only giving me 1 where as the last number in col c is 8 so i would want it to give me the next number which would be 9 and the next time a sample is entered it would be 10 and so on.



    point taken on the green........ now you mention it it does look in your face.

  7. #7
    Forum Contributor
    Join Date
    01-28-2007
    MS-Off Ver
    2019
    Posts
    204
    i download the version you edited and that works but when i copy the same code onto my version it doesnt work ? The actual file is 500kb.

  8. #8
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200
    Your original numbers in Column C do not appear to be recognised as numbers, I had to type them in again.

  9. #9
    Forum Contributor
    Join Date
    01-28-2007
    MS-Off Ver
    2019
    Posts
    204
    bingo !Obvious really i suppose. yet again this forum saves the day. thanks

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1