Hello
this code will increment numbers continuously when run the userform every time.
what I want when run the userform just show the last number is existed in cell A1 into textbox without increment numbers .
for instance if the number is BGH-02/23-1 in A1 , then will show in TEXTBOX1 BGH-02/23-1 and if I press commandbutton1 then will increment number in A1 ,TEXTBOX1 = BGH-02/23-2 , if I press commandbutton2 without press commandbutton1 then doesn't increment in textbox1, A1 should keep BGH-02/23-1 without increment and if I close form from window button without press commandbutton1 then also should not increment numbe and keep A1 ,TEXTBOX1 = BGH-02/23-1, if I press commandbutton1 and close the userform then will increment number in A1 ,TEXTBOX1 = BGH-02/23-2 , if I press commandbutton1 will increment number in A1 ,TEXTBOX1 = BGH-02/23-2 after that press commandbutton2 then should return to old number A1 ,TEXTBOX1 = BGH-02/23-1
, if I press commandbutton1 more than one time , then will pop up message for two choices . if press ok will continue increming numbers and if I press no ,then will not increment (just keep the last incrementing number)
I hope somebody help .
this code will increment numbers continuously when run the userform every time.
what I want when run the userform just show the last number is existed in cell A1 into textbox without increment numbers .
for instance if the number is BGH-02/23-1 in A1 , then will show in TEXTBOX1 BGH-02/23-1 and if I press commandbutton1 then will increment number in A1 ,TEXTBOX1 = BGH-02/23-2 , if I press commandbutton2 without press commandbutton1 then doesn't increment in textbox1, A1 should keep BGH-02/23-1 without increment and if I close form from window button without press commandbutton1 then also should not increment numbe and keep A1 ,TEXTBOX1 = BGH-02/23-1, if I press commandbutton1 and close the userform then will increment number in A1 ,TEXTBOX1 = BGH-02/23-2 , if I press commandbutton1 will increment number in A1 ,TEXTBOX1 = BGH-02/23-2 after that press commandbutton2 then should return to old number A1 ,TEXTBOX1 = BGH-02/23-1
, if I press commandbutton1 more than one time , then will pop up message for two choices . if press ok will continue increming numbers and if I press no ,then will not increment (just keep the last incrementing number)
VBA Code:
Private Sub UserForm_Activate()
Dim num As String
Dim cell As Range
Dim m As String, y As String
Set cell = Sheets("Sheet1").Range("A1") 'Set the sheet name and any available cell
num = cell.Value
If Left(num, 3) <> "BGH" Or Mid(num, 4, 1) <> "-" Or _
Mid(num, 7, 1) <> "/" Or Mid(num, 10, 1) <> "-" Then
num = "BGH-" & Format(Date, "mm/yy") & "-1"
Else
y = Mid(num, 8, 2)
m = Mid(num, 5, 2)
If y <> Format(Date, "yy") Or m <> Format(Date, "mm") Then
num = "BGH-" & Format(Date, "mm/yy") & "-" & Split(num, "-")(2) + 1
Else
num = Left(num, 10) & Split(num, "-")(2) + 1
End If
End If
cell.Value = num
TextBox1 = num
End Sub