Macro help

AussieVic

Active Member
Joined
Jan 9, 2009
Messages
364
Hi would like to add a condition to the below code, when the code filtersinplace but there is no data to filter then do no proceed in copying to destination.



ShtTemp.Range("A5").CurrentRegion.AdvancedFilter Action:=xlFilterInPlace, _
Criteriarange:=Range("rngAsset"), Unique:=False


ShtTemp.Range("A5").CurrentRegion.Offset(1).Resize(, 14).Copy Destination:=shtAsset.Range("A6")
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
How about wrapping your copy line in this If statement?

Code:
ShtTemp.Range("A5").CurrentRegion.AdvancedFilter Action:=xlFilterInPlace, _
Criteriarange:=Range("rngAsset"), Unique:=False
If ShtTemp.Range("A5").CurrentRegion.SpecialCells(xlCellTypeVisible).Areas.count > 1 OR _
ShtTemp.Range("A5").CurrentRegion.SpecialCells(xlCellTypeVisible).Rows.count > 1 then _
ShtTemp.Range("A5").CurrentRegion.Offset(1).Resize(, 14).Copy Destination:=shtAsset.Range("A6")

Let us know if this works.

Gary
 
Last edited:
Upvote 0
Hi Gary is it possible to exit sub if there is no data when filetered, so the code does not carry on



How about wrapping your copy line in this If statement?

Code:
ShtTemp.Range("A5").CurrentRegion.AdvancedFilter Action:=xlFilterInPlace, _
Criteriarange:=Range("rngAsset"), Unique:=False
If ShtTemp.Range("A5").CurrentRegion.SpecialCells(xlCellTypeVisible).Areas.count > 1 OR _
ShtTemp.Range("A5").CurrentRegion.SpecialCells(xlCellTypeVisible).Rows.count > 1 then _
ShtTemp.Range("A5").CurrentRegion.Offset(1).Resize(, 14).Copy Destination:=shtAsset.Range("A6")

Let us know if this works.

Gary
 
Upvote 0
Code:
[COLOR="Blue"]Sub[/COLOR] SomeCode()

    [COLOR="Blue"]Dim[/COLOR] rng [COLOR="Blue"]As[/COLOR] Range
    
    [COLOR="Blue"]With[/COLOR] ShtTemp.Range("A5").CurrentRegion
        .AdvancedFilter Action:=xlFilterInPlace, Criteriarange:=Range("rngAsset"), Unique:=[COLOR="Blue"]False[/COLOR]
        [COLOR="Blue"]Set[/COLOR] rng = .SpecialCells(xlCellTypeVisible)
        [COLOR="Blue"]If[/COLOR] [COLOR="Blue"]Not[/COLOR] rng [COLOR="Blue"]Is[/COLOR] [COLOR="Blue"]Nothing[/COLOR] [COLOR="Blue"]Then[/COLOR]
            .Offset(1).Resize(, 14).Copy Destination:=shtAsset.Range("A6")
        [COLOR="Blue"]End[/COLOR] [COLOR="Blue"]If[/COLOR]
    [COLOR="Blue"]End[/COLOR] [COLOR="Blue"]With[/COLOR]
    
[COLOR="Blue"]End[/COLOR] [COLOR="Blue"]Sub[/COLOR]
 
Upvote 0
Sektor will this exit sub if there is no data so the code does not keep running?

Code:
Sub SomeCode()

    Dim rng As Range
    
    With ShtTemp.Range("A5").CurrentRegion
        .AdvancedFilter Action:=xlFilterInPlace, Criteriarange:=Range("rngAsset"), Unique:=False
        Set rng = .SpecialCells(xlCellTypeVisible)
        If Not rng Is Nothing Then
            .Offset(1).Resize(, 14).Copy Destination:=shtAsset.Range("A6")
        End If
 
        'more code
 

    End With
    
End Sub
 
Upvote 0
With exit:
Code:
[COLOR="Blue"]Sub[/COLOR] SomeCode()

    [COLOR="Blue"]Dim[/COLOR] rng [COLOR="Blue"]As[/COLOR] Range
    
    [COLOR="Blue"]With[/COLOR] ShtTemp.Range("A5").CurrentRegion
        .AdvancedFilter Action:=xlFilterInPlace, Criteriarange:=Range("rngAsset"), Unique:=[COLOR="Blue"]False[/COLOR]
        [COLOR="Blue"]Set[/COLOR] rng = .SpecialCells(xlCellTypeVisible)
        [COLOR="Blue"]If[/COLOR] [COLOR="Blue"]Not[/COLOR] rng [COLOR="Blue"]Is[/COLOR] [COLOR="Blue"]Nothing[/COLOR] [COLOR="Blue"]Then[/COLOR]
            .Offset(1).Resize(, 14).Copy Destination:=shtAsset.Range("A6")
        [COLOR="Blue"]Else[/COLOR]
            [B][COLOR="Blue"]Exit[/COLOR] [COLOR="Blue"]Sub[/COLOR][/B]
        [COLOR="Blue"]End[/COLOR] [COLOR="Blue"]If[/COLOR]
    [COLOR="Blue"]End[/COLOR] [COLOR="Blue"]With[/COLOR]
    
[COLOR="Blue"]End[/COLOR] [COLOR="Blue"]Sub[/COLOR]
 
Upvote 0
That did not worked, when the data was filtered there was nothing there except for header row so i expect the code to Exit, but when i stepped through, it went to .offset etc and then End if

With exit:
Code:
[COLOR=blue]Sub[/COLOR] SomeCode()
 
    [COLOR=blue]Dim[/COLOR] rng [COLOR=blue]As[/COLOR] Range
 
    [COLOR=blue]With[/COLOR] ShtTemp.Range("A5").CurrentRegion
        .AdvancedFilter Action:=xlFilterInPlace, Criteriarange:=Range("rngAsset"), Unique:=[COLOR=blue]False[/COLOR]
        [COLOR=blue]Set[/COLOR] rng = .SpecialCells(xlCellTypeVisible)
        [COLOR=blue]If[/COLOR] [COLOR=blue]Not[/COLOR] rng [COLOR=blue]Is[/COLOR] [COLOR=blue]Nothing[/COLOR] [COLOR=blue]Then[/COLOR]
            .Offset(1).Resize(, 14).Copy Destination:=shtAsset.Range("A6")
        [COLOR=blue]Else[/COLOR]
            [B][COLOR=blue]Exit[/COLOR] [COLOR=blue]Sub[/COLOR][/B]
        [COLOR=blue]End[/COLOR] [COLOR=blue]If[/COLOR]
    [COLOR=blue]End[/COLOR] [COLOR=blue]With[/COLOR]
 
[COLOR=blue]End[/COLOR] [COLOR=blue]Sub[/COLOR]
 
Upvote 0
Your original question: "Sektor will this exit sub if there is no data so the code does not keep running?"
I wrote code which makes Sub terminate when there's no data. When do you want to exit? Please, be more specific.
 
Upvote 0

Forum statistics

Threads
1,224,590
Messages
6,179,756
Members
452,940
Latest member
rootytrip

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