I have created a new project Userform in a new workbook and copied the code below from another project, which is operating perfectly with this code. I copied the code into this new workbook project, but I keep getting the Runtime error, even after I modified the textbox name.
The purpose of the code is to automatically generate a Work Order number in the textbox txt_Work_Order_No prefixed by "SWC" and then by year, month and then the number format. The final output should look like this SWC201706-00001 (incrementing each time the form is opened).
Private Sub UserForm_Initialize()
'** WORK ORDER NUMBER IN TEXTBOX
Me.txt_Work_Order_No.Enabled = True
Dim irow As Long
Dim ws As cnServiceLog
Set ws = cnServiceLog
'find last data row from database'
irow = ws.Cells(Rows.Count, 1).End(xlUp).Row
Me.txt_Work_Order_No = "SWC" & Year(Date) & Format(Month(Date), "00") & "-" & Format(Split(ws.Cells(irow, 1).Value, "-")(1) + 1, "00000")
I stepped through the code, and broke down to the code line that was giving me grief and I commented the rest of it out and everything was fine, but obviously it won't do want I would like it to do.
The area which causes the Runtime error is right after the double zero(Month(Date,"00")
Me.txt_Work_Order_No = "SWC" & Year(Date) & Format(Month(Date), "00") & "-" & Format(Split(ws.Cells(irow, 1).Value, "-")(1) + 1, "00000")
I hope someone would be able to help me out .... I have searched the internet for days and still haven't been able to resolve whatever problem it seems to have. With gratitude, TheShyButterfly
Me.txt_Work_Order_No = "SWC" & Year(Date) & Format(Month(Date), "00") & "-" & Format(Split(ws.Cells(irow, 1).Value, "-")(0) + 1, "00000")
– Vityata(1)
to(0)
in your code. Use this to see the differences textdiff.com – Vityataws.Cells(irow,1)
. Probably it is a string. Thus, change it toMe.txt_Work_Order_No = "SWC" & Year(Date) & Format(Month(Date), "00") & "-" & Split(ws.Cells(irow, 1).Value, "-")(0)
– Vityata