No Visible Cells

Carly

Active Member
Joined
Aug 21, 2002
Messages
370
I have got a macro which filters a spreadsheet and then copies the visible cells in one column into another spreadsheet.

If there is no data in the filter it errors. If there any way I can say:

If no data selected then Do Nothing otherwise Do My Code
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
Perhaps like this

Code:
On Error Resume Next
'
'code to copy and paste
'
On Error GoTo 0
 
Upvote 0
I had put this in, but wouldn't this go to next whatever the error? I only want it to happen on this specific error.
 
Upvote 0
Or use logic like this:
Code:
[COLOR="Blue"]Sub[/COLOR] Visi()
    
    [COLOR="Blue"]Dim[/COLOR] rng [COLOR="Blue"]As[/COLOR] Range, lastRow [COLOR="Blue"]As[/COLOR] [COLOR="Blue"]Long[/COLOR]
    
    [COLOR="Blue"]With[/COLOR] Sheets("Sheet1")
                
[COLOR="SeaGreen"]        ' Define last row in data. I have chosen column A.[/COLOR]
        lastRow = .Range("A6").End(xlDown).Row
        
[COLOR="seagreen"]        ' Turn off autofilter, if any.[/COLOR]
        [COLOR="Blue"]If[/COLOR] .AutoFilterMode [COLOR="Blue"]Then[/COLOR] .AutoFilter.Range.AutoFilter
         
[COLOR="seagreen"]        ' Filter data by room.[/COLOR]
        .Range("A6:M6").AutoFilter Field:=2, Criteria1:=12
        
[COLOR="seagreen"]        ' Copy cell we're interested in.[/COLOR]
        [COLOR="Blue"]Set[/COLOR] rng = .Range("C7:C" & lastRow).SpecialCells(xlCellTypeVisible)
    
        [COLOR="Blue"]If[/COLOR] [COLOR="Blue"]Not[/COLOR] rng [COLOR="Blue"]Is Nothing Then[/COLOR] 
            rng.Copy
        [COLOR="Blue"]End[/COLOR] [COLOR="Blue"]If[/COLOR]
        
[COLOR="seagreen"]        ' Turn off autofilter, if any.[/COLOR]
        [COLOR="Blue"]If[/COLOR] .AutoFilterMode [COLOR="Blue"]Then[/COLOR] .AutoFilter.Range.AutoFilter
        
    [COLOR="Blue"]End[/COLOR] [COLOR="Blue"]With[/COLOR]
    
[COLOR="Blue"]End[/COLOR] [COLOR="Blue"]Sub[/COLOR]
 
Upvote 0
Yes, but if you only have the copy & paste line between the On Error Resume Next and On Error Goto 0 there probably isn't much else that is going to go wrong.
 
Upvote 0
And any other errors that happen elsewhere in my code will just be handled as normal?
 
Upvote 0
Thank you very much...I will use this!

Very much appreciated, never managed to get my head around Error Handling :)
 
Upvote 0

Forum statistics

Threads
1,224,566
Messages
6,179,555
Members
452,928
Latest member
101blockchains

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