Special Cells on blank failure!!!

rfletcher35

Active Member
Joined
Jul 20, 2011
Messages
300
Office Version
  1. 365
Platform
  1. Windows
I have the following code

Sheets("MPR Data").Select
ActiveSheet.Range("$T$4:$AD$4000").AutoFilter Field:=9, Criteria1:="x"
ActiveSheet.Range("$T$4:$AD$4000").AutoFilter Field:=1, Criteria1:="365"
If Range("T5:AB4000").SpecialCells(xlCellTypeVisible).Value > "" Then
Range("T5:AB4000").SpecialCells(xlCellTypeVisible).Select
Selection.Copy
Sheets("Results Sheet").Select
Range("P5").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Else
End If

My code is failing at the line If Range("T5:AB4000").SpecialCells(xlCellTypeVisible).Value > "" Then
I have the above filters in place and sometimes the filters return nothing and when it does it fails as it tries to copy nothing. I'm trying to put a step in to avoid that, so if filters produce nothing it goes on to the next part of my coding.

Can anyone assist with this please?


Thanks

Fletch
 

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
Perhaps.
Code:
Dim rng As Range

    With Sheets("MPR Data")
        .Range("$T$4:$AD$4000").AutoFilter Field:=9, Criteria1:="x"
        .Range("$T$4:$AD$4000").AutoFilter Field:=1, Criteria1:="365"
        On Error Resume Next
        Set rng = .Range("T5:AB4000").SpecialCells(xlCellTypeVisible).Value
        On Error GoTo 0
    End With

    If Not rng Is Nothing Then
        rng.Copy
        Sheets("Results Sheet").Range("P5").PasteSpecial Paste:=xlPasteValues
    End If
 
Upvote 0
Hi I implemented the above code as below and whilst it doesn't crash it also doesn't seem to copy and paste the filtered data either?

Dim rng As Range
With Sheets("MPR Data")
'.Range("$T$4:$AD$4000").AutoFilter Field:=9, Criteria1:="x"
.Range("$T$4:$AD$4000").AutoFilter Field:=1, Criteria1:=">=358", Operator:=xlFilterValues, Criteria2:="<364", Operator:=xlFilterValues
On Error Resume Next
Set rng = .Range("T5:AB4000").SpecialCells(xlCellTypeVisible).Value
On Error GoTo 0
End With
If Not rng Is Nothing Then
rng.Copy
Sheets("Results Sheet").Range("P5").PasteSpecial Paste:=xlPasteValues
End If
 
Upvote 0
Hi,

don't use "select" and "visible cells"

without testing:

with Sheets("MPR Data").Range("$T$4:$AD$4000")
.AutoFilter Field:=9, Criteria1:="x"
.ActiveSheet.Range("$T$4:$AD$4000").AutoFilter Field:=1, Criteria1:="365"
.Copy Sheets("Results Sheet").Range("P5").paste
end with

If you realy need "paste special" then in two lines

regards
 
Upvote 0
Hi,

That fails on .ActiveSheet.Range("$T$4:$AD$4000").AutoFilter Field:=1, Criteria1:="365"
If I take out .ActiveSheet and start it from .Range it goes past this line but then fails on .Copy Sheets("Results Sheet").Range("P5").paste

Any ideas. I just want to be able to copy what ever filtered data is there and paste it. Because it's filtered it won't always be the same row which is why specialcells came into it. But I also need something in there so it ignores when there is no data to copy once filtered.

Fletch
 
Upvote 0
Hi I implemented the above code as below and whilst it doesn't crash it also doesn't seem to copy and paste the filtered data either?

Dim rng As Range
With Sheets("MPR Data")
'.Range("$T$4:$AD$4000").AutoFilter Field:=9, Criteria1:="x"
.Range("$T$4:$AD$4000").AutoFilter Field:=1, Criteria1:=">=358", Operator:=xlFilterValues, Criteria2:="<364", Operator:=xlFilterValues
On Error Resume Next
Set rng = .Range("T5:AB4000").SpecialCells(xlCellTypeVisible).Value
On Error GoTo 0
End With
If Not rng Is Nothing Then
rng.Copy
Sheets("Results Sheet").Range("P5").PasteSpecial Paste:=xlPasteValues
End If

Maybe...

Code:
Dim rng As Range

With Sheets("MPR Data")
    '.Range("$T$4:$AD$4000").AutoFilter Field:=9, Criteria1:="x"
    .Range("$T$4:$AD$4000").AutoFilter Field:=1, Criteria1:=">=358", [COLOR=#0000ff]Operator:=xlAnd[/COLOR], Criteria2:="<364", Operator:=xlFilterValues
    On Error Resume Next
    [COLOR=#0000ff]Set rng = .Range("T5:AB4000").SpecialCells(xlCellTypeVisible)[/COLOR]
    On Error GoTo 0
End With

If Not rng Is Nothing Then
    rng.Copy
    Sheets("Results Sheet").Range("P5").PasteSpecial Paste:=xlPasteValues
End If

Hope this helps

M.
 
Upvote 0

Forum statistics

Threads
1,214,394
Messages
6,119,263
Members
448,881
Latest member
Faxgirl

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top