我通过使用以下代码生成发票的唯一键,并且我获得运行时错误1004应用程序定义或对象定义错误在行
cell.Value = CStr(d & Mx)
为什么我得到它?
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
答案 0 :(得分:0)
将Option Explicit
写在最前面。
然后声明所有变量。
EG。 Dim d as String
。它会起作用。
或快速而肮脏:
cell.Value = CStr(d) & CStr(Mx)
答案 1 :(得分:0)
这对我有用。
尝试将cell As Range
的名称更改为myCell As Range
之类的内容。因为cell
是VBA中的对象,所以您无法使用该名称。