I'm generating unique keys for invoice by using the below code and im getting Run time Error 1004 Application defined or object defined error in line
cell.Value = CStr(d & Mx)
Why I'm getting it?
Private Sub Key_Gen()
Dim WIP_rng As Range
Dim d As String, c As Long, r As Long, s As Integer, p As Integer
Set WIP_rng = Range("WIP[Key]")
Mx = WorksheetFunction.Max(keynum)
c = 1
p = 0
For Each col In WIP_rng
r = col.Row
If col.Value = "" And Cells(r, 3).Value <> "" And Cells(r, 4).Value <> "" And Cells(r, 5).Value <> "" And Cells(r, 6).Value <> "" And Cells(r, 7).Value <> "" Then
s = Val(Len(Trim(Mx)))
Debug.Print s
t = 5
p = t - s
Debug.Print p
If p = 1 Then
p = "0"
ElseIf p = 2 Then
p = "00"
ElseIf p = 3 Then
p = "000"
ElseIf p = 4 Then
p = "0000"
ElseIf p = 0 Then
p = ""
End If
Mx = CStr(Mx + c)
d = CStr("MG" & p)
d = d & Mx
col.Value = d
c = c + 1
End If
Next
Application.ScreenUpdating = True
End Sub
"WIP[Key]"
before - is that valid? What is the value ofNum_Key
?c
,r
,l
ands
should beLong
I take it? – Darren Bartrup-Cook