is there way in Excel(VBA) to copy/paste formulas from filtered column in 1 statement ? This works :
Sheets(1).Range("A2:C" & LastRow).Copy
Sheets(2).Range("A2:C" & Range("D" & Rows.Count).End(xlUp).Row).PasteSpecial xlPasteFormulas
But this returns messed up rows (probably because column is filtered) :
Sheets(2).Range("A2:C" & Range("D" & Rows.Count).End(xlUp).Row).Formula = Sheets(1).Range("A2:C" & LastRow).Formula
any ideas if it's possible to do it without using clipboard, in 1 statement ?
EDIT
In Sheet1, I add formulas to columns A,B and C:
With Sheets(1)
LastRow = .Range("D" & Rows.Count).End(xlUp).Row
.Range("A5:A" & LastRow).Value = "=D5/$A$3*100"
.Range("A:AG").AutoFilter Field:=22, Criteria1:=">=1/1/2014", Operator:=xlAnd, Criteria2:="<=12/31/2014"
.Range("B5:B" & LastRow).SpecialCells(xlCellTypeVisible).Value = "=D" & .UsedRange.Offset(5, 0).SpecialCells(xlCellTypeVisible).Row & "/$B$3*100"
.Range("A:AG").AutoFilter Field:=22, Criteria1:=">=1/1/2015"
.Range("C5:C" & LastRow).SpecialCells(xlCellTypeVisible).Value = "=D" & .UsedRange.Offset(5, 0).SpecialCells(xlCellTypeVisible).Row & "/$C$3*100"
.ShowAllData
End With
Therefore column A has formula "=Dn/$A$3*100, where n is row number. B and C formulas have division by B3 and C3 cell value. Then I filter Sheet1, copy filtered rows and paste them to Sheet2
Sheets(1).Range("A4:AG" & LastRow).AutoFilter Field:=7, Criteria1:=name
Sheets(1).Range("A5:C" & LastRow).Copy
Sheets(2).Range("A5:C" & Range("D" & Rows.Count).End(xlUp).Row).PasteSpecial xlPasteFormulas
.FormulaLocal
rather than.Formula
? There is also.FormulaHidden
and.FormulaArray
that might be helpful. – R3uKEnum XlCellType
, containing :Const xlCellTypeFormulas = -4123 (&HFFFFEFE5)
andConst xlCellTypeVisible = 12
– R3uK.End
will skip hidden cells which becomes relevant when you have filtered data. Can you also show whereLastRow
comes from? I bumped your rep past 10, so you can post a picture. – Byron Wall