Consulting

Results 1 to 8 of 8

Thread: Userforms - Textbox Autofill

  1. #1
    VBAX Regular
    Joined
    May 2017
    Posts
    19
    Location

    Userforms - Textbox Autofill

    Hello!

    Probably a super easy noob question however.. I've created a Userform which works without any issue, but I'm trying to make it a little bit faster by adding various autofills, which i've been able to do most of them but im having a bit of trouble when it comes to a reference number.

    I require it to increase by 1 every time the userform is used, the reference isn't purely numbers its a mixture of letters hyphens and numbers (last few being numbers), would the code be something like

    ... Range("A:A")+1 ...

    Or would it be something more complex?

    Thanks in advance.

  2. #2
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,728
    Location
    Probably something like this.

    Not too complex

    Option Explicit
    Sub demo()
        Dim s As String
        s = "ABD-DEF-00123"
        MsgBox s
        
        s = AddOne(s)
        MsgBox s
        
        s = AddOne(s)
        MsgBox s
        
        s = AddOne(s)
        MsgBox s
        
        s = AddOne(s)
        MsgBox s
        
    End Sub
    
    'assumes something like AAA-BBB-CCC-123 with numbers only in last position and seperated by hyphen
    Function AddOne(s As String) As String
        Dim v As Variant
        Dim n As Long
        Dim i As String
        
        v = Split(s, "-")
        
        n = Len(v(UBound(v)))
        
        i = CLng(v(UBound(v)) + 1)
            
        v(UBound(v)) = Format(i, Left("000000000000", n))
        
        AddOne = Join(v, "-")
    
    End Function
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  3. #3
    VBAX Regular
    Joined
    May 2017
    Posts
    19
    Location
    Quote Originally Posted by Paul_Hossler View Post
    Probably something like this.

    Not too complex

    Option Explicit
    Sub demo()
        Dim s As String
        s = "ABD-DEF-00123"
        MsgBox s
        
        s = AddOne(s)
        MsgBox s
        
        s = AddOne(s)
        MsgBox s
        
        s = AddOne(s)
        MsgBox s
        
        s = AddOne(s)
        MsgBox s
        
    End Sub
    
    'assumes something like AAA-BBB-CCC-123 with numbers only in last position and seperated by hyphen
    Function AddOne(s As String) As String
        Dim v As Variant
        Dim n As Long
        Dim i As String
        
        v = Split(s, "-")
        
        n = Len(v(UBound(v)))
        
        i = CLng(v(UBound(v)) + 1)
            
        v(UBound(v)) = Format(i, Left("000000000000", n))
        
        AddOne = Join(v, "-")
    
    End Function
    Don't think I understand this,

    My reference will be following the below creitra,

    ABC-DEF-00-GH-IJ-L-0001 (0001 is what i want to increase by 1)

    Do i add the code into the body or in the UserForm_Initialize?

  4. #4
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Private Sub UserForm_Initialize()
    Dim LR As Long
    LR = Sheets(1).Cells(Rows.Count, 1).End(xlUp).Row + 1
    TextBox1.Text = "ABC-DEF-00-GH-IJ-L-" & Format(LR, "0000")
    End Sub
    or
    Private Sub UserForm_Initialize()
    Dim LR As Long
    LR = Sheets(1).[A1] = Sheets(1).[A1] + 1
    TextBox1.Text = "ABC-DEF-00-GH-IJ-L-" & Format(LR, "0000")
    End Sub
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  5. #5
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,728
    Location
    Quote Originally Posted by Auxie View Post
    Don't think I understand this,

    My reference will be following the below creitra,

    ABC-DEF-00-GH-IJ-L-0001 (0001 is what i want to increase by 1)

    Do i add the code into the body or in the UserForm_Initialize?
    Most likely in the UserForm_Initialize

    It would probably be something like ...


    Private Sub UserForm_Initialize() 
    Worksheets("Sheet1").Range("A1").Value = AddOne(Worksheets("Sheet1").Range("A1").Value)
    End Sub
    So if A1 = "ABC-DEF-00-GH-IJ-L-0001" the AddOne will make it "ABC-DEF-00-GH-IJ-L-0002" each time the Initialize is called

    Without having a sample workbook, it's hard to be specific.

    For example, I'm not sure what you meant by "... Range("A:A")+1 ..."
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  6. #6
    VBAX Regular
    Joined
    May 2017
    Posts
    19
    Location
    Hey,

    I've added a copy of the excel file to this post. Currently working on the TQ -out page - very basic code and I'm probably missing out on loads of things, but I am a novice after all!


    Thanks in advance
    Last edited by mdmackillop; 06-20-2017 at 06:30 AM. Reason: Attachment deleted at OP's request

  7. #7
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    So much clearer with a sample. After clearing your test data.
    Private Sub UserForm_Initialize()
            Dim x
            x = Format(Cells(Rows.Count, 1).End(xlUp).Row - 2, "0000")
            TransmittalNo.SetFocus
            TransmittalNo.Value = "WOO-CAP-00-XX-RE-Z-" & x 'This is what i use atm and manually add the last 4 digits
    A couple of points.
    If this entry is not to be changed, consider
    1) using a label instead of a textbox
    2) disabling the textbox if you keep it
    3) moving the focus to the next control to be entered
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  8. #8
    VBAX Regular
    Joined
    May 2017
    Posts
    19
    Location
    Quote Originally Posted by mdmackillop View Post
    So much clearer with a sample. After clearing your test data.
    Private Sub UserForm_Initialize()
            Dim x
            x = Format(Cells(Rows.Count, 1).End(xlUp).Row - 2, "0000")
            TransmittalNo.SetFocus
            TransmittalNo.Value = "WOO-CAP-00-XX-RE-Z-" & x 'This is what i use atm and manually add the last 4 digits
    A couple of points.
    If this entry is not to be changed, consider
    1) using a label instead of a textbox
    2) disabling the textbox if you keep it
    3) moving the focus to the next control to be entered
    Excellent thank you - I had the focus at that position because that's where i had to originally input data, but thanks to you expertise I can now move it to another field! Thanks again!

    If possible could a mod please remove my attachment in the above post? (#6)

Posting Permissions

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