excel - 试图避免 .Select 或 .Activate 但失败并出现错误 1004
问题描述
我正在编写一个带有一些循环的小宏,效果很好,但现在尝试清理并避免使用 .Select 和 .Activate,但是这样做时,我遇到了“错误 1004”
使用 .Select 让工作表完美运行以查找数据,然后 .Select 回到应该粘贴的工作表。
这里有一些代码:
Sub Sæt_ind_i_eksporten()
Dim dagensliste As Worksheet
Dim trailerliste As Worksheet
Dim trailernummer As String
Dim finalrow As Integer
Dim i As Integer
Dim t As Integer
Dim targetcount As Integer
Set trailerliste = ThisWorkbook.Sheets("Data ud (2)")
Set dagensliste = ThisWorkbook.ActiveSheet
Application.ScreenUpdating = False
targetcount = trailerliste.Cells(30, 1).End(xlDown).Row
finalrow = Cells(Rows.Count, 1).End(xlUp).Row
For t = 30 To targetcount
trailernummer = trailerliste.Cells(t, 3).Value
For i = 4 To finalrow
If Cells(i, 3) = trailernummer Then
' Here is where it fails - If I select the sheet manually, it moves on
trailerliste.Range(Cells(t, 1), Cells(t, 13)).Copy
' And here it fails again (logic i know!) select manually again and it moves on.
dagensliste.Range(Cells(i, 1), Cells(i, 13)).PasteSpecial xlPasteValues
End If
Next i
Next t
Application.GoTo reportsheet.Range(A1)
MsgBox ("Søgning gennemført")
Application.ScreenUpdating = True
End Sub
如果我在单独的行上trailerliste.Range(Cells(t, 1), Cells(t, 13)).Copy
用trailerliste.select
和替换Range(Cells(t, 1), Cells(t, 13)).Copy
,并且相同,dagensliste.Select
那么代码就完美了。
我知道这是一件简单的事情,但我尝试阅读我在论坛上可以找到的所有内容,但没有运气。
我希望有人能帮助我:)
亲切的问候
解决方案
Range(Cells(x,y)) 很棘手,因为您必须指定单元格所在的工作表。
trailerliste.Range(Cells(t, 1), Cells(t, 13)).Copy
必须
trailerliste.Range(trailerliste.Cells(t, 1), trailerliste.Cells(t, 13)).Copy
推荐阅读
- android - 在自定义 BroadcastReceiver 中接收地理围栏事件
- c# - 将 DataGridView 的行高度设置为自定义值很慢
- javascript - Html 从给定数组中选择选项未显示
- python - python2.7线程池比逐个启动线程快很多
- react-native - React Native - 广告请求成功,但由于广告库存不足,没有返回广告
- javascript - 停止触发事件
- c++ - 抛出 C++20 协程上的 AddressSanitizer 错误
- javascript - 事件发射器相对于异步函数的优势
- python - Python课程和书籍柜台
- amazon-web-services - 发生错误:服务器在处理 Lambda 响应时遇到错误