End macro if no filter results, but continue macro if results exist

jl1066

New Member
Joined
Mar 22, 2021
Messages
4
Office Version
  1. 2016
Platform
  1. Windows
I'm looking for some help in the following code. I am filtering a set of data and depending on the results looking for either two things to happen: if there are results then copy and paste them into another workbook, but if there are no results I'm looking to have a message box appear, close the workbook and then end the Macro. But what's happening is if there are no results then the macro will continue with the code to copy and paste which I do not want.

Any help would be appreciated.

This is the code I'm using:
Rich (BB code):
ws.Range("A14:AG14").AutoFilter field:=2, Criteria1:=""
Dim j As Integer
Dim r As Range
Set r = Range(Range("G15"), Range("G15").End(xlDown))

j = WorksheetFunction.Count(r.Cells.SpecialCells(xlCellTypeVisible))
If j = 0 Then
result = MsgBox("No New Requisitions")
Workbooks("Filter Report.xlsx").Close savechanges:=False
End If
'End macro here if no results

'If there are filtered results copy the results and paste in the second workbook
Dim LR As Long
LR = Range("A" & Rows.Count).End(xlUp).Row
Range("D15:AE" & LR).SpecialCells(xlCellTypeVisible).Copy
Workbooks("Requisition Macro.xlsm").Activate
Cells(Rows.Count, 1).End(xlUp).Offset(1, 1).Select
Selection.PasteSpecial Paste:=xlPasteValues
Application.CutCopyMode = False
Application.DisplayAlerts = False
 
Last edited by a moderator:

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
Try this:
if you turn off Application.EnableEvents or ScreenUpdating, Turn on Them Also Before Exit Sub
VBA Code:
ws.Range("A14:AG14").AutoFilter field:=2, Criteria1:=""
Dim j As Integer
Dim r As Range
Set r = Range(Range("G15"), Range("G15").End(xlDown))

j = WorksheetFunction.Count(r.Cells.SpecialCells(xlCellTypeVisible))
If j = 0 Then
result = MsgBox("No New Requisitions")
Workbooks("Filter Report.xlsx").Close savechanges:=False
Exit Sub
End If
'End macro here if no results

'If there are filtered results copy the results and paste in the second workbook[/B]
Dim LR As Long
LR = Range("A" & Rows.Count).End(xlUp).Row
Range("D15:AE" & LR).SpecialCells(xlCellTypeVisible).Copy
Workbooks("Requisition Macro.xlsm").Activate
Cells(Rows.Count, 1).End(xlUp).Offset(1, 1).Select
Selection.PasteSpecial Paste:=xlPasteValues
Application.CutCopyMode = False
Application.DisplayAlerts = False
 
Upvote 0

Forum statistics

Threads
1,214,785
Messages
6,121,543
Members
449,038
Latest member
Guest1337

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