Increment Userform texbox by 1

atmospheric

Well-known Member
Joined
Jul 16, 2002
Messages
565
Ok, back to this first Userform of mine:

I am converting a data entry spreadsheet into a Userform for ease of entry. On the spreadsheet I have to following formula, (courtesy of Yogi Anand):

=IF(A5>0,LEFT(B4,4)&RIGHT(B4,4)+1,"")

This looks at the cell above and adds 1 to the numeric part, if the cell to the left contains something (a date in this case). The cell above contains an Order No in the form of ASC/6789.

On clicking "Add New Order" how can I get Textbox4 to look at the last Order No and add 1, i.e. automatically generate the next sequential Order No?

Your help much appreciated
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
Hi atmospheric,
What I did was put your formula (the one from Yogi) in cell D1.
Then in D2 I used this: =LEFT(D1, 4)&RIGHT(D1, 4) + 1

Then in the code of the userform button I used:
Code:
Private Sub CommandButton1_Click()
TextBox4.Value = [D2].Value
End Sub

There's probably a better way, but hey, I don't see anyone else's ideas, it's late here and this works. :rolleyes:

Dan
 
Upvote 0
Looks like you are trying to populate the next order number in your entry screen. This assumes that column b has no entries after the last order number!

That being the case, this code pulls first instance of new order number:

Code:
Private Sub UserForm_Initialize()
    Dim order
    
    order = ActiveSheet.Range("b65536").End(xlUp).Value
    TextBox4.Value = Mid(order, 1, 4) & Mid(order, 5, 4) + 1
End Sub


And add this to your OK button. Assumes you are doing multiple entries

Code:
Private Sub CommandButton1_Click()
    Dim rw, rng As Range
    '// these lines populate the order number and repopulate textbox4 for next order
    ' // unless there is another reason, you don't need Yogi's formula if you are
    '// doing all entry via the userform!
    Set rng = ActiveSheet.Range("b65536").End(xlUp).Offset(1, 0)
    rng.Value = TextBox4.Value
    TextBox4.Value = Mid(rng.Value, 1, 4) & Mid(rng.Value, 5, 4) + 1
    TextBox1.SetFocus
End Sub


HTH
 
Upvote 0
HalfAce, thanks, but i'm trying to avoid formula on the spreadsheet, too many novices inputting.

TTL.......a little confused here, could you talk me through both parts of the code, and why the necessity for both parts? I have it partially working just using the first part, but if I use both I get all sorts of problems.

Thanks
 
Upvote 0
Probably easier if you post the code to your OK button and we can work in the rest of the coding.

The code for initialization populates textbox4 (herein after refered to as the party of the tb4!) with the next order number.

This code is attached to your OK button:

Private Sub CommandButton1_Click()
Dim rw, rng As Range
' your exising code less anything for textbox4 or column b goes here
' then:
' find the first unused cell in column B (1 cell past last invoice number)
Set rng = ActiveSheet.Range("b65536").End(xlUp).Offset(1, 0)
' set this newly found blank cell = new invoice number as shown in tb4
rng.Value = TextBox4.Value
' assumes this is the last thing you're doing and lets go back
' and enter the next invoice so:
' increment the invoice number in tb4
TextBox4.Value = Mid(rng.Value, 1, 4) & Mid(rng.Value, 5, 4) + 1
' go back to tb1 to begin your entries
TextBox1.SetFocus
End Sub


Most importantly, column b should not have any formulas in it, just the last invoice number!

Does this help clarify it?
 
Upvote 0

Forum statistics

Threads
1,221,631
Messages
6,160,942
Members
451,679
Latest member
BlueH1

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top