Change character when show in textbox on userform based on optionbutton

MKLAQ

Active Member
Joined
Jan 30, 2021
Messages
397
Office Version
  1. 2016
Platform
  1. Windows
Hello
I want changing the character when autonumbering in textbox based on optionbutton selection
the current code will show word "BGH-" & date based on this line
VBA Code:
.Value = "BGH-" & Format$(Date, "mm-dd-1")
but now I want changing to
AUT1.JPG

it depends on optionbutton name selection and the optionbutton name will match with sheet name where auto numbering for each sheet alone
when autonumbering in textbox will write the optionbutton name selection for instance SALES & NO 0004300 to become SALES NO 0004300 as in the picture above
and copy to sheet name is matched with optionbutton name selection in A1 cell and when click command button1 will increase the number like this
AUT2.JPG


and if I click commandbutton2 then will decrease number like this
AUT3.JPG

and if select PURCHASE will be like SALES for instance PURCHASE NO 0004300 but the the only different is OPTIONBUTTON NAME and should copy to sheet name into cell A1 is matched with optionbutton name
here is the whole codes
VBA Code:
Option Explicit
Private flg As Boolean



Private Sub UserForm_Initialize()
    Dim x
    With Sheets("SALES").[a1]
        flg = .Value = ""
        If flg Then
            .Value = "BGH-" & Format$(Date, "mm-dd-1")
            Me.TextBox1 = .Value
        Else
            x = Split(Sheets("SALES").[a1], "-")
            x(UBound(x)) = x(UBound(x))
            .Value = Join(x, "-")
            Me.TextBox1 = .Value
        End If
        Me.Tag = .Value
    End With
    Me.CommandButton1.Enabled = Not flg
End Sub

Private Sub CommandButton1_Click()
    Dim x
    With Sheets("SALES").[a1]
        If Not flg Then
            x = Split(Sheets("SALES").[a1], "-")
            x(UBound(x)) = x(UBound(x)) + 1
            .Value = Join(x, "-")
            Me.TextBox1 = .Value
        End If
    End With
End Sub

Private Sub CommandButton2_Click()
    If Me.TextBox1 = Me.Tag Then Exit Sub
    Dim x
    With Sheets("SALES").[a1]
        If Not flg Then
            x = Split(Sheets("SALES").[a1], "-")
            x(UBound(x)) = x(UBound(x)) - 1
            .Value = Join(x, "-")
            Me.TextBox1 = .Value
        End If
    End With
End Sub

I hope from the experts give me some help.
 

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
Cross-posting (posting the same question in more than one forum) is not against our rules, but the method of doing so is covered by #13 of the Forum Rules.

Be sure to follow & read the link at the end of the rule too!

Cross posted at: Modyfying code to change showing way number in textbox on userform
There is no need to repeat the link(s) provided above but if you have posted the question at other places, please provide links to those as well.

If you do cross-post in the future and also provide links, then there shouldn’t be a problem.
 
Upvote 0
If you do cross-post in the future and also provide links, then there shouldn’t be a problem.
no it's just here and attached link in your post , sorry!
 
Upvote 0
ok after many tries and tests and no answers from here or other forum !!!:mad::mad::mad: .I got the solution .:)
no need this
Rich (BB code):
Private Sub UserForm_Initialize()
    Dim x
    With Sheets("SALES").[a1]
        flg = .Value = ""
        If flg Then
            .Value = "BGH-" & Format$(Date, "mm-dd-1")
            Me.TextBox1 = .Value
        Else
            x = Split(Sheets("SALES").[a1], "-")
            x(UBound(x)) = x(UBound(x))
            .Value = Join(x, "-")
            Me.TextBox1 = .Value
        End If
        Me.Tag = .Value
    End With
    Me.CommandButton1.Enabled = Not flg
End Sub
should be like this
VBA Code:
Private Sub CommandButton1_Click()
  Dim x
  Dim i           As Long
    Dim sheetname   As String

    For i = 1 To 3
        With Me.Controls("OptionButton" & i)
            If .Value Then sheetname = .Caption: Exit For
        End With
    Next i

    With Worksheets(sheetname).[a1]
    'With Sheets("SALES").[a1]
        flg = .Value = ""
        If flg Then
            .Value = "NO 0004300"
            Me.TextBox1 = .Value
        Else
            x = Split(Worksheets(sheetname).[a1], "000")
            x(UBound(x)) = x(UBound(x)) + 1
            .Value = Join(x, "000")
            Me.TextBox1 = sheetname & " " & .Value
           
        End If
        Me.Tag = .Value
    End With
   ' Me.CommandButton1.Enabled = Not flg
End Sub

Private Sub CommandButton2_Click()
    If Me.TextBox1 = Me.Tag Then Exit Sub
    Dim x
    Dim i           As Long
    Dim sheetname   As String

    For i = 1 To 3
        With Me.Controls("OptionButton" & i)
            If .Value Then sheetname = .Caption: Exit For
        End With
    Next i

    With Worksheets(sheetname).[a1]
        If Not flg Then
            x = Split(Worksheets(sheetname).[a1], "000")
            x(UBound(x)) = x(UBound(x)) - 1
            .Value = Join(x, "000")
            Me.TextBox1 = sheetname & " " & .Value
        End If
    End With
End Sub
 
Upvote 0
here is the right code
VBA Code:
Private Sub CommandButton1_Click()
  Dim x
  Dim i           As Long
    Dim sheetname   As String

    For i = 1 To 3
        With Me.Controls("OptionButton" & i)
            If .Value Then sheetname = .Caption: Exit For
        End With
    Next i

    With Worksheets(sheetname).[a1]
    'With Sheets("SALES").[a1]
        flg = .Value = ""
        If flg Then
            .Value = sheetname & " " & "NO 0004300"
            Me.TextBox1 = .Value
        Else
            x = Split(Worksheets(sheetname).[a1], "000")
            x(UBound(x)) = x(UBound(x)) + 1
            .Value = Join(x, "000")
            Me.TextBox1 = sheetname & " " & .Value
           
        End If
        Me.Tag = .Value
    End With
   ' Me.CommandButton1.Enabled = Not flg
End Sub

Private Sub CommandButton2_Click()
    If Me.TextBox1 = Me.Tag Then Exit Sub
    Dim x
    Dim i           As Long
    Dim sheetname   As String

    For i = 1 To 3
        With Me.Controls("OptionButton" & i)
            If .Value Then sheetname = .Caption: Exit For
        End With
    Next i

    With Worksheets(sheetname).[a1]
        If Not flg Then
            x = Split(Worksheets(sheetname).[a1], "000")
            x(UBound(x)) = x(UBound(x)) - 1
            .Value = Join(x, "000")
            Me.TextBox1 = sheetname & " " & .Value
        End If
    End With
End Sub
 
Upvote 0
just there is one problem when click commandbutton1 when increment the number will repeat writing option name twice
for instance when click commandbutton1 from the first time will be SALES NO 0004300 , but when click commandbutton1 again will be SALESSALES NO 0004301
the right way should be SALES NO 0004301, HOW I can fix it ?
I hope some body help .
 
Upvote 0

Forum statistics

Threads
1,215,084
Messages
6,123,021
Members
449,092
Latest member
ikke

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