我不断收到运行时错误 91,我搜索了一下,似乎问题出在我是否正确设置/声明了变量/对象。我很确定我正确引用了所有内容,并且相同的宏之前运行正常……但突然间,vba 宏坏了。有什么线索可以解释为什么会这样吗?
注意:有 2 个独立的工作簿。1 个工作簿充当客户的数据库,另一个工作簿是模板表,我可以在其中搜索客户,它会自动填充模板表。
在给变量 LastRow 赋值时发现错误,我尝试在前面使用 Set,但似乎仍然不起作用。
Sub AddNewClient()
Application.ScreenUpdating = False
Dim i As Long
Dim LastRow As Long
Dim strClient As String
Dim strEmail As String
Dim strPhone As String
Dim ClientDBWB As Excel.Workbook
Set CurrWkbk = ActiveWorkbook
ClientDBWB = Workbooks.Open("[SomePath][File Name.xlsx]")
***The Debugger shows the error here*** when assigning LastRow a value.
LastRow = ClientDBWB.Sheets("ClientDB").Cells(Rows.Count, "A").End(xlUp).Row + 1
strClient = CurrWkbk.Sheets("House").Range("C11:J11").Cells(1, 1).Value
strEmail = CurrWkbk.Sheets("House").Range("C12:J12").Cells(1, 1).Value
strPhone = CurrWkbk.Sheets("House").Range("C13:J13").Cells(1, 1).Value
With ClientDBWB.Sheets("ClientDB")
.Cells(LastRow, 1).Value = strClient
.Cells(LastRow, 2).Value = strPhone
.Cells(LastRow, 4).Value = strEmail
End With
MsgBox "Client is Added to the Database!"
Application.ScreenUpdating = True
答案1
在给变量 LastRow 赋值时发现错误,我尝试在前面使用 Set,但似乎仍然不起作用。
如果您在 LastRow 前面使用了 SET,那么您就错过了 VBA 的语法。由于您将 LastRow 声明为 Long,因此只能使用“=”运算符进行赋值。
另一方面,ClientDBWB 是一个必须使用 SET 命令的对象。使用调试器查看使用 SET 和未使用 SET 时的不同结果。
使用“选项显式”来强制您思考您正在处理的对象类型,因为您必须声明所有内容。
我看到的另一个问题是:
strClient = CurrWkbk.Sheets("房屋").Range("C11:J11").Cells(1, 1).值
有一种更复杂的写法:
strClient = CurrWkbk.Sheets("房屋").Range("C11").value
甚至
strClient = CurrWkbk.Sheets("房屋").Range("C11")
答案2
按任务复制
Option Explicit
Sub AddNewClient()
' Source
Dim swb As Workbook: Set swb = ThisWorkbook ' workbook containing this code
Dim sws As Worksheet: Set sws = swb.Worksheets("House")
Application.ScreenUpdating = False
' Destination
Dim dwb As Workbook: Set dwb = Workbooks.Open("C:\Test\Test.xlsx")
Dim dws As Worksheet: Set dws = dwb.Worksheets("ClientDB")
Dim dRow As Long: dRow = dws.Cells(dws.Rows.Count, "A").End(xlUp).Row + 1
' ' Or...
' Dim dRow As Long: dRow = dws.Cells(dws.Rows.Count, 1).End(xlUp).Row + 1
' Dim dRow As Long: dRow = dws.Range("A" & dws.Rows.Count).End(xlUp).Row + 1
' Copy
dws.Cells(dRow, 1).Value = sws.Range("C11").Value ' Client
dws.Cells(dRow, 2).Value = sws.Range("C12").Value ' Phone
dws.Cells(dRow, 4).Value = sws.Range("C13").Value ' Email
' Or opting for only 'Range' or only 'Cells':
' ' Or...
' dws.Range("A" & dRow).Value = sws.Range("C11").Value ' Client
' dws.Range("B" & dRow).Value = sws.Range("C12").Value ' Phone
' dws.Range("D" & dRow).Value = sws.Range("C13").Value ' Email
' ' Or...
' dws.Cells(dRow, "A").Value = sws.Cells(11, "C").Value ' Client
' dws.Cells(dRow, "B").Value = sws.Cells(12, "C").Value ' Phone
' dws.Cells(dRow, "D").Value = sws.Range(13, "C").Value ' Email
' ' Or...
' dws.Cells(dRow, 1).Value = sws.Cells(11, 3).Value ' Client
' dws.Cells(dRow, 2).Value = sws.Cells(12, 3).Value ' Phone
' dws.Cells(dRow, 4).Value = sws.Range(13, 3).Value ' Email
' Save and Close
'dwb.Close SaveChanges:=True
Application.ScreenUpdating = True
' Inform
MsgBox "Client is Added to the Database!"
End Sub