PDA

View Full Version : [SOLVED:] How can I fix my autofilter?



roxnoxsox
12-22-2015, 04:01 AM
I think my error might be with the selected range to copy maybe..? I'm trying to check if cell in column D is empty and if so, then copy the contents of cell in column C and paste into D. I'm doing this for a very large amount of data. Currently I'm trying to do this through an autofilter (open to other suggestions if anyone knows of a better way!) so that the visible contents of column C are pasted into D. But it's coming up with an error message when I run the macro: (the line beginning 'Range("D2:D" & LR.....' is the line highlighted in Debug)



LR = Cells(Rows.Count, 1).End(xlUp).Row
Rows("1:1").Select
Selection.AutoFilter
ActiveSheet.Range("D2:D" & LR).AutoFilter Field:=4, Criteria1:="="
Range("C2", Range("C2").End(xlDown)).Select
Selection.Copy
Range("D2:D" & LR).SpecialCells(xlCellTypeVisible).PasteSpecial xlValues
Rows("1:1").Select
Application.CutCopyMode = False
ActiveSheet.ShowAllData
Selection.AutoFilter
Range("A1").Select

Is this an obvious error? ;_; My range of data will change a lot so I need this to be able to run for however many rows there are which is where the LR comes in.. Thanks in advance!

Bob Phillips
12-22-2015, 04:25 AM
This works


Dim LR As Long
Dim rngArea As Range
LR = Cells(Rows.Count, 1).End(xlUp).Row
Rows("1:1").AutoFilter
ActiveSheet.Range("D2:D" & LR).AutoFilter Field:=4, Criteria1:="="
For Each rngArea In Range("C2:C" & LR).SpecialCells(xlCellTypeVisible).Areas

rngArea.Copy
rngArea.Offset(0, 1).PasteSpecial xlValues
Next rngArea
ActiveSheet.ShowAllData
Rows("1:1").AutoFilter
Range("A1").Select

roxnoxsox
12-22-2015, 04:43 AM
Perfect, this worked great! Many thanks :)

snb
12-22-2015, 06:27 AM
wouldn't this ?


Sub M_snb()
[D1:D2000] = [if(D1:D2000="",C1:C2000,D1:D2000)]
End Sub

Aussiebear
12-22-2015, 06:43 AM
True snb, but what if the range was bigger ( given the the OP suggested that the range had to be for how ever many rows) in post #1

snb
12-22-2015, 08:38 AM
What about changing 2000 ? no big deal I gather.

but alternatively:


Sub M_snb()
usedrange.columns(4).name="snb"
usedrange.columns(3).name="snb1"
[snb] = [if(snb="",snb1,snb)]
End Sub

mancubus
12-22-2015, 08:52 AM
@snb
since 'SNB1' is a cell address just change it to, eg, snbxx.

snb
12-22-2015, 04:19 PM
Good idea; snb0 would do as well.