当我尝试运行下面的代码时,我收到“运行时错误'1004'应用程序定义或对象定义”错误。一直试图修复它几个小时,但似乎无法正确。
我正在尝试做的事情:两个工作表,一个包含Projects,另一个包含与每个Project相关的所有Perks。因此,在Perks工作表上可以有多个具有相同project_id的行,而在Project工作表上,project_id都是唯一的。我希望获得一系列关于行的数据,在Perks工作表上具有相同的project_id,以便在Projects工作表的同一project_id的一行上彼此相邻放置。希望你仍然遵循我的意思;)。我在下面使用的代码在具有类似情况的其他工作簿上工作正常,因此不确定这里的问题是什么。 Perks工作表上有大约3000个条目,所以这不应该是我认为的问题。有什么想法吗?
Sub Perks_and_Projects()
Dim r As Long, lr As Long
Dim src As Object
Application.ScreenUpdating = False
With Sheets("Perks")
lr = .Cells(Rows.Count, 1).End(xlUp).Row
For r = 2 To lr
Set src = Sheets("Projects").Columns(1).Find(.Cells(r, 1).Value, LookAt:=xlWhole)
If Not src Is Nothing Then
*'error occurs on the next line:*
Sheets("Projects").Range("AA" & src.Row).End(xlToRight).Offset(0, 1).Value = .Cells(r, 19).Value
Sheets("Projects").Range("AA" & src.Row).End(xlToRight).Offset(0, 1).Value = .Cells(r, 20).Value
Sheets("Projects").Range("AA" & src.Row).End(xlToRight).Offset(0, 1).Value = .Cells(r, 21).Value
Sheets("Projects").Range("AA" & src.Row).End(xlToRight).Offset(0, 1).Value = .Cells(r, 22).Value
Sheets("Projects").Range("AA" & src.Row).End(xlToRight).Offset(0, 1).Value = .Cells(r, 23).Value
Sheets("Projects").Range("AA" & src.Row).End(xlToRight).Offset(0, 1).Value = .Cells(r, 24).Value
End If
Next r
End With
Application.ScreenUpdating = True
End Sub
答案 0 :(得分:0)
我终于设法创建了一种解决方法。如果有人遇到类似的情况,这是代码:
Sub Perks_and_Projects()
Dim b As Variant, r As Long, lr As Long, n As Long
Dim src As Object, d As Integer
Application.ScreenUpdating = False
With Sheets("Perks")
lr = .Cells(Rows.Count, 1).End(xlUp).Row
b = .Range("A1:B" & lr)
For r = 1 To lr
Set src = Sheets("Projects").Columns(1).Find(.Cells(r, 1).Value, LookAt:=xlWhole)
d = Sheets("Projects").Cells(src.Row, .Columns.Count).End(xlToLeft).Column
If Not src Is Nothing Then
Sheets("Projects").Cells(src.Row, d).Offset(0, 1).Value = .Cells(r, 19).Value
Sheets("Projects").Cells(src.Row, d).Offset(0, 2).Value = .Cells(r, 20).Value
Sheets("Projects").Cells(src.Row, d).Offset(0, 3).Value = .Cells(r, 21).Value
Sheets("Projects").Cells(src.Row, d).Offset(0, 4).Value = .Cells(r, 22).Value
Sheets("Projects").Cells(src.Row, d).Offset(0, 5).Value = .Cells(r, 23).Value
Sheets("Projects").Cells(src.Row, d).Offset(0, 6).Value = .Cells(r, 23).Value
End If
Next r
End With
Sheets("Perks").Range("A1").Resize(UBound(b, 1), UBound(b, 2)) = b
Application.ScreenUpdating = True
End Sub