This seems like a simple task but I keep running into various errors. I need to filter worksheet B and then copy a column of data. I then need to filter worksheet A and then paste the copied data into a column.
Worksheets("SheetB").Select
lastRowOne = Range("B" & Rows.Count).End(xlUp).Row
Range("DL2:DL" & lastRowOne).AutoFilter Field:=116, Criteria1:="<>Apples"
lastRowTwo = Range("B" & Rows.Count).End(xlUp).Row
Range("DG2:DG" & lastRowTwo).AutoFilter Field:=111, Criteria1:=Target
'Target is already defined earlier in the Macro and functions fine
lastRowThree = Range("B" & Rows.Count).End(xlUp).Row
Range("DX2:DX" & lastRowThree).Copy
Worksheets("SheetA").Activate
lastRowFour = Range("B" & Rows.Count).End(xlUp).Row
Range("A2:A" & lastRowFour).AutoFilter Field:=1, Criteria1:=Target
lastRowFive = Range("B" & Rows.Count).End(xlUp).Row
Range("Z2:Z" & lastRowFive).SpecialCells(xlCellTypeVisible).Select
Selection.PasteSpecial Paste:=xlPasteRange, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
In place of the last line I have also tried:
ActiveSheet.Paste
The first returns a "Run-time error '1004': PasteSpecial method of range class failed
the ActiveSheet.Paste returns a "Run-time error '1004': Paste method of Worksheet class failed
Although this code is not the cleanest, it all functions with the exception of the "pasting" onto 'sheetA' in Column Z. I also need the data pasted into AA if that can be included in a fix.
Thanks !
.Select
/.Activate
. Using these is not best practice - you want to avoid using.Select
/.Activate
, as you can see it can throw some errors. I highly suggest reading through that page, and apply it. If you still get errors, let us know. Especially when switching in between sheets, using.Activate
will likely fail. – BruceWaynelastRow = Range("B" & Rows.Count).End(xlUp).Row
Dim Adjustment As Range
'Set Adjustment = Worksheets("Sheet").Range("DX2:DX" & lastRow)' Then Activated and filtered Sheet A then this code:Range("Z2:Z" & lastRowTwelve).SpecialCells(xlCellTypeVisible).Value = Adjustment.Value
This "pasted" in the correct column BUT the data that was "copied" was incorrect. It ignored the filters on SheetB (in my original code it was copying correctly. Any Suggestions? I am continuing to try and clean out the .activate and .selection! – RugsKidSet Adjustment = Worksheets("YTD Promo Review").Range("DX2:DX" & lastRowTen).SpecialCells(xlCellTypeVisible)
This resulted in four correct lines bring "pasted" to SheetA in Column Z. But the rest of the rows below the top four are not correct. They say #N/A – RugsKidSet copyRng = .Range("DX2:DX" & lastRow)
to:Set copyRng = .Range("DX2:DX" & lastRowLast).SpecialCells(xlCellTypeVisible)
This "copied" and "pasted" the first four rows correctly but the rest of the rows now contain #N/A - just as a value - no formula in them. Any ideas? – RugsKidcopyRng
and see if there's any reason a cell might be/become#N/A
. Are these formulas I assume that you're pasting the values to the other sheet? If you can, I suggest doing this part of the steps manually, with the macro recorder on. So, copy your range, paste as values to the other sheet. Then look at the code generated, and also check to see if there are any#N/A
cells. (PS: When replying regarding my answer, comment under that one) – BruceWayne