2016-08-26 61 views
0

我無法複製標準範圍內的過濾數據,然後將其粘貼到「受管實體」表中,而沒有將新的過濾數據粘貼到先前過濾的數據上。我如何調整下面的代碼,以確保首先粘貼1226上過濾的數據,然後將1843年過濾的數據粘貼到1226的數據下面?將多個過濾條件粘貼到一張表中而不粘貼原始數據?

Sheets("P&L").Select 
Range("A1").AutoFilter Field:=1, Criteria1:= _ 
    "*1226*" 
ActiveSheet.AutoFilter.Range.Copy 
Sheets("P&L Regulated Entities").Select 
Range("A1").Select 
Selection.PasteSpecial _ 
Paste:=xlPasteFormulas, Operation:=xlNone, SkipBlanks:=False,Transpose:=False 


Sheets("P&L").Select 
Range("A1").AutoFilter Field:=1, Criteria1:= _ 
    "*1843*" 
ActiveSheet.AutoFilter.Range.Copy 
Sheets("P&L Regulated Entities").Select 
ActiveCell.Offset(1).Select 
Range("A1").Select 
Selection.PasteSpecial _ 
Paste:=xlPasteFormulas, Operation:=xlNone, SkipBlanks:=False, Transpose:=False 


Sheets("P&L").Select 
Range("A1").AutoFilter Field:=1, Criteria1:= _ 
    "*865*" 
ActiveSheet.AutoFilter.Range.Copy 
Sheets("P&L Regulated Entities").Select 
ActiveCell.Offset(1).Select 
Range("A1").Select 
Selection.PasteSpecial _ 
Paste:=xlPasteFormulas, Operation:=xlNone, SkipBlanks:=False, Transpose:=False 


Sheets("P&L").Select 
Range("A1").AutoFilter Field:=1, Criteria1:= _ 
    "*1223*" 
ActiveSheet.AutoFilter.Range.Copy 
Sheets("P&L Regulated Entities").Select 
ActiveCell.Offset(1).Select 
Range("A1").Select 
Selection.PasteSpecial _ 
Paste:=xlPasteFormulas, Operation:=xlNone, SkipBlanks:=False, Transpose:=False 
+0

那麼你會想要使範圍(「A1」)。選擇動態通過查找最後一行有數據和下移一個。看到這裏查找最後一行的方法:http://stackoverflow.com/questions/11169445/error-in-finding-last-used-cell-in-vba然後加1到最後一行的返回。 –

+0

@ScottCraner你能告訴我如何將它嵌入上面的代碼中嗎? – JHags

+1

另外,最好[避免使用'.Select' /'.Activate'](https://stackoverflow.com/questions/10714251/how-to-avoid-using-select-in-excel-vba-macros) 。很多代碼可以減少到最多10行。 – BruceWayne

回答

0

我會用這個。 With...End With結構允許您刪除.Select語句。像.Range("A" & .Rows.Count).End(xlUp).Offset(1,0)這樣的行會將複製的數據放在受管實體工作表的第一個空行。

With Sheets("P&L") 
    .Range("A1").AutoFilter Field:=1, Criteria1:= _ 
    "*1226*" 
    .AutoFilter.Range.Copy 
End With 

With Sheets("P&L Regulated Entities") 
    .Range("A1").PasteSpecial _ 
Paste:=xlPasteFormulas, Operation:=xlNone, SkipBlanks:=False,Transpose:=False 
End With 


With Sheets("P&L") 
    .Range("A1").AutoFilter Field:=1, Criteria1:= _ 
    "*1843*" 
    .AutoFilter.Range.Copy 
End With 

With Sheets("P&L Regulated Entities") 
    .Range("A" & .Rows.Count).End(xlUp).Offset(1,0).PasteSpecial _ 
Paste:=xlPasteFormulas, Operation:=xlNone, SkipBlanks:=False, Transpose:=False 
End With 


With Sheets("P&L") 
    .Range("A1").AutoFilter Field:=1, Criteria1:= _ 
    "*865*" 
    .AutoFilter.Range.Copy 
End With 

With Sheets("P&L Regulated Entities") 
    .Range("A" & .Rows.Count).End(xlUp).Offset(1,0).PasteSpecial _ 
Paste:=xlPasteFormulas, Operation:=xlNone, SkipBlanks:=False, Transpose:=False 
End With 


With Sheets("P&L") 
    .Range("A1").AutoFilter Field:=1, Criteria1:= _ 
    "*1223*" 
    .AutoFilter.Range.Copy 
End With 

With Sheets("P&L Regulated Entities") 
    .Range("A" & .Rows.Count).End(xlUp).Offset(1,0).PasteSpecial _ 
Paste:=xlPasteFormulas, Operation:=xlNone, SkipBlanks:=False, Transpose:=False 
End With