Macro : IF Condition is not met

Siops

Board Regular
Joined
Sep 8, 2011
Messages
76
Good day. I would like to ask help regarding conditional setup. So I have a dates that will be filtered based on the condition and will copy paste it. My problem right now is if the condition dates are not available, it will copy all the information. So if the condition dates are not available, it will move to the next Sheet. Thank you in advance.


Code:
Sub Consolidate()Dim cRow As Integer
Dim sRow As Integer
Dim lngStart As Long, lngEnd As Long
    lngStart = Range("K1").Value
    lngEnd = Range("L1").Value
    


For i = 2 To Sheets.Count


Sheets(i).Activate


Range("A:G").AutoFilter field:=1, _
        Criteria1:=">=" & lngStart, _
        Operator:=xlAnd, _
        Criteria2:="<=" & lngEnd
        
cRow = Application.WorksheetFunction.CountA(Range("A:A"))
Cells(2, 7) = Sheets(i).Name
Sheets(i).Range("G2").Copy
Sheets(i).Range("G2:G" & cRow).PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:=False
Sheets(i).Range("A2:G" & cRow).SpecialCells(xlCellTypeVisible).Copy


Sheets(1).Activate
sRow = Application.WorksheetFunction.CountA(Range("A:A")) + 1
Sheets(1).Range("A" & sRow).PasteSpecial Operation:=xlNone, SkipBlanks:=False






Next i


End Sub
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)

Forum statistics

Threads
1,215,206
Messages
6,123,639
Members
449,111
Latest member
ghennedy

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