下面的代码部分大部分时间都能工作,但偶尔也会引发错误
loc.PasteSpecial xlValues
使用
类的运行时1004‘PasteSpecial方法失败
我也不能让它有规律的错误,所以妨碍我找出原因。在执行代码时不会发生错误。
这种复制和粘贴特殊的方法是在宏中使用几次。
这个问题的其他答案似乎是不同的用例,所以我很难在代码中找到问题。
'extracts the cfc data out to its own table if its there.
If Not IsError(Application.Match("CFC", rng1, 0)) Then
wb2.Sheets("Import").Activate
'reset filter to show all data
If (ActiveSheet.AutoFilterMode And ActiveSheet.FilterMode) Or ActiveSheet.FilterMode Then
ActiveSheet.ShowAllData
End If
If ActiveSheet.AutoFilterMode = False Then ActiveSheet.Range("A1:BK1").AutoFilter
ActiveSheet.Range("A1:BK1").AutoFilter Field:=1,
'filters data based on criteria
Criteria1:=Array("*criteria1*"), Operator:=xlFilterValues
ActiveSheet.Range("A1:BK1").AutoFilter Field:=5, Criteria1:=Array("*CFC*"), Operator:=xlFilterValues
'Copys the rows that are visable.
Range("A2:BK" & ActiveSheet.UsedRange.Rows.Count + 1) _
.Cells.SpecialCells(xlCellTypeVisible).Rows.Copy
wb2.Sheets("CFC").Select
'reset filter to show all data
If (ActiveSheet.AutoFilterMode And ActiveSheet.FilterMode) Or ActiveSheet.FilterMode Then
ActiveSheet.ShowAllData
End If
With Range("a:a") 'find the next available row on sheet data using column A
Set loc = .Find(What:="", LookIn:=xlFormulas, _
LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False)
End With
outputrow = loc.Row 'sets the output row
loc.PasteSpecial xlValues
loc.PasteSpecial xlFormats
End If
基本功能是。
我已经尝试了一段时间了,所以任何帮助都会非常感谢。
发布于 2015-07-27 12:04:49
仅在粘贴范围之前立即复制范围(使用变量存储要复制的范围)。-罗里
正如罗里所说的。它没有将副本放在引起问题的粘贴之前。
复制代码被更改为使用工作表引用变量,并将其放置在粘贴之前,现在宏100%按预期工作。:)
outputrow = loc.Row 'sets the output row
sh1.Range("A2:BK" & sh1.UsedRange.Rows.Count + 1) _
.Cells.SpecialCells(xlCellTypeVisible).Rows.Copy
loc.PasteSpecial xlValues
loc.PasteSpecial xlFormats
https://stackoverflow.com/questions/31649661
复制相似问题