VBA – 运行此代码时出现运行时错误“1004”

我是VBA新手,我试图将两个不同的列合并到另一个工作表中的一列中。

我的问题是,我得到了“运行时错误”1004“应用程序或对象定义的错误”。

从我所读到的错误最有可能与Sheets("Tabelle1")但我不知道如何解决它。

这里是代码:

 Sub test2() Dim name1 As Range, size As Integer size = WorksheetFunction.CountA(Columns(3)) With Sheets("Tabelle1") For Each name1 In Sheets("advprsrv").Range("D2:D" & size) If Not (Trim(name1.Value & vbNullString) = vbNullString) Then .Cells(name1.Row, 1).Value = LCase(name1.Value & " " & Range(Cells(name1.Row, name1.Column))) End If Next name1 End With End Sub 

编辑:我通过与F8的代码,似乎在这一行popup的错误ws.Cells(name1.Row, 1).Value = LCase(name1.Value & " " & Range(Cells(name1.Row, name1.Column).Value))

试试下面的代码,解释里面的代码注释:

 Option Explicit Sub test2() Dim name1 As Range, size As Long Dim TblSht As Worksheet Dim advpSht As Worksheet ' set the worksheet object and trap errors in case it doesn't exist On Error Resume Next Set TblSht = ThisWorkbook.Worksheets("Tabelle1") On Error GoTo 0 If TblSht Is Nothing Then ' in case someone renamed the "Expense" Sheet MsgBox "Tabelle1 sheet has been renamed", vbCritical Exit Sub End If ' set the worksheet object and trap errors in case it doesn't exist On Error Resume Next Set advpSht = ThisWorkbook.Worksheets("advprsrv") On Error GoTo 0 If advpSht Is Nothing Then ' in case someone renamed the "Expense" Sheet MsgBox "advprsrv sheet has been renamed", vbCritical Exit Sub End If With advpSht ' safer way to get the last row from column "D" (since later on you use it in a Range in Column "D") size = .Cells(.Rows.Count, "D").End(xlUp).Row For Each name1 In .Range("D2:D" & size) If Trim(name1.Value2) <> "" Then ' ***** Not sure about the connection between the 2 sheets ***** TblSht.Cells(name1.Row, 1).Value = LCase(name1.Value2 & " " & TblSht.Cells(name1.Row, name1.Column)) End If Next name1 End With End Sub