Hi all,
I want to know if there is more efficient way to get what this macro does. So I need macro to generate next number. But the number is part of string. This how numbers look like for example:
1X0001223
1K0000244
1Q0000098
They always starts with 1 and some letter. Then there is 7 digits number. Currently I have specific command button for each number, so 1X if I want to generate 1X* number, button with 1K if I want generate 1K* number etc.
There is a workbook which stores these numbers. When I click command button, 1X for example, it opens this workbook, goes to "1K" sheet, finds last row and creates new number in a row below, then it copies it and pastes in selected cell in original workbook.
But the problem is that it ignores 0 between 1X and last number, so I had to add line to check last 4 characters in string and then add 1 to them. When I skipped it it resulted 1X1224 as next number, instead of 1X0001224. But also when I will go up to 1X0009999 number, it will create 1X00010000 instead of replacing 0 to the left with 1 (all materials are 9 digits total long). Any ideas how to make it more efficient, so I don't have to change macro each time?
I want to know if there is more efficient way to get what this macro does. So I need macro to generate next number. But the number is part of string. This how numbers look like for example:
1X0001223
1K0000244
1Q0000098
They always starts with 1 and some letter. Then there is 7 digits number. Currently I have specific command button for each number, so 1X if I want to generate 1X* number, button with 1K if I want generate 1K* number etc.
There is a workbook which stores these numbers. When I click command button, 1X for example, it opens this workbook, goes to "1K" sheet, finds last row and creates new number in a row below, then it copies it and pastes in selected cell in original workbook.
But the problem is that it ignores 0 between 1X and last number, so I had to add line to check last 4 characters in string and then add 1 to them. When I skipped it it resulted 1X1224 as next number, instead of 1X0001224. But also when I will go up to 1X0009999 number, it will create 1X00010000 instead of replacing 0 to the left with 1 (all materials are 9 digits total long). Any ideas how to make it more efficient, so I don't have to change macro each time?
VBA Code:
Private Sub CommandButton7_Click()
Dim LastRow As Long
Dim x As Integer
Dim rng As range
Dim FPath As String
Dim wb As Workbook
Dim wb1 As Workbook: Set wb1 = ActiveWorkbook
Application.ScreenUpdating = False
FPath = "C:\Local\KR\Numbers.xlsm"
Set wb = Workbooks.Open(FPath)
Dim ws As Worksheet: Set ws = wb.Sheets("1X")
wb.Activate
ws.Activate
LastRow = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Row
x = LastRow
Set rng = ActiveSheet.Cells(x, 1)
rng.Offset(1, 0).Value = "1X000" & Right(rng, 4) + 1
rng.Offset(1, 1).Value = Date
rng.Offset(1, 2).Value = Format(Now, "HH:MM")
rng.Offset(1, 3).Value = Application.UserName
rng.Offset(1, 0).Copy
wb1.Activate
ActiveCell.PasteSpecial Paste:=xlPasteValues
wb.Close True
Application.ScreenUpdating = True
End Sub