我在写一个VBA宏,它复制一组数据,应用一些过滤,然后将过滤的数据复制到下一个可用行。我的问题是最后一行“copyRange.SpecialCells(xlCellTypeVisible).Copy GAR070 ....”,它有时会起作用,有时不会引发错误:运行时错误'1004':方法'Range'object'_Worksheet'失败。将可见单元格仅复制到变量行
如果我然后将GAR070图纸对象的引用更改为表格(“GAR070”),我最终得到运行时错误“1004”:应用程序定义或对象定义的错误。
由于这有时适用于我,有时候我不知道它是否与我已打开的其他工作簿有关?或者当我声明对象时发生了什么?
我的代码还有很多,但我没有把它包含在这里,所以你不必一一阅读,但是如果你怀疑可能会发生某些事情,我很乐意对它进行评论。
我以前在本网站和其他网站上看过,关于如何做到这一点,我最初是如何找到这种方法的。
请让,我知道如果我错过了提前:)
Sub Check_for_specials()
Dim GAR070 As Worksheet
Dim LRowred As Long
Dim filterRange As Range
Dim copyRange As Range
Dim newrow As Long
Dim wb as Workbook
Application.ScreenUpdating = False
Set wb = ActiveWorkbook
Set GAR070 = wb.Sheets("GAR070")
'filter for "Y" in last column, copy this data and paste into the next available row
With GAR070
LRowred = .Cells(Rows.Count, "A").End(xlUp).Row
' turn off any autofilters that are already set
.AutoFilterMode = False
' the range that we are auto-filtering (all columns)
Set filterRange = .Range("A1:O" & LRowred)
' the range we want to copy (only columns we want to copy)
Set copyRange = .Range("A2:N" & LRowred)
' filter range based on column O
filterRange.AutoFilter field:=15, Criteria1:="Y"
' copy the visible cells to our target range
newrow = LRowred + 1
copyRange.SpecialCells(xlCellTypeVisible).Copy GAR070.Range(Cells(newrow, 1), Cells(newrow + 1, 14)) ' seems very volatile...
Application.ScreenUpdating = True
End sub
谢谢谢谢谢谢谢谢!它确实有效。并且完全合理。为了延迟回复,我必须重新开始测试。非常感谢:) – KateNZ
我很乐意提供帮助。考虑接受我的回答,以便将问题标记为已回答。 – dendarii