Run macro only if condition is met.

eurano

New Member
Joined
Sep 13, 2015
Messages
25
Hi,
I have following macro wich run 2 macros but I want to check if condition is met before this macro even start. In spreadsheet named "Arkusz1" i have pivot table named "Tabela przestawna 2" wich when its empty macro shouldnt start and show msgbox "there is nothing to send". In addition this pivot table is allways filtered.

Code:
Sub RunThemAll()

    Application.Run "vbaAutoRefreshPivot2"
    
    Application.Run "Notes_Email_Excel_Cells"
    
   
End Sub

Picture of pivot below:
URL]
 
Last edited:

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
Well first I need to know the range of data that we are checking to see if its empty. So in this macro, I'll assume that the range of data that might be empty is B2:C25. Because I'm going to assume column A is never empty. You would run the Sub checkIfRangeHasData().
Code:
Sub checkIfRangeHasData()
     dataRange = "B2:C25"
     If emptyCheck(dataRange) = "Empty" Then
          MsgBox "There is nothing to send"
     Else
          Call RunThemAll()
     End If
End Sub

Function emptyCheck(dataRange)
     If WorksheetFunction.CountA(Range(dataRange)) = 0 Then
          emptyCheck = "Empty"
     Else
         emptyCheck = "Not Empty"
     End If
End Function

Sub RunThemAll()
     Application.Run "vbaAutoRefreshPivot2"
     Application.Run "Notes_Email_Excel_Cells"
End Sub
 
Upvote 0
The pivot table range is named "Report" (i defined range as dynamic value so it resize whenever there will be more rows in pivot table).
 
Upvote 0
You didn't understand the problem so just post an example table so I can figure it out myself.
 
Upvote 0
The range to check is pivot table in cell N1. If there is no data there like in the picture from first post then macro shouldnt start,
 
Last edited:
Upvote 0
Code:
Sub checkIfRangeHasData()
     '65536 rows in Excel 1997
     '1048576 rows in Excel 2007, 2010,and  2013
     dataRange = "N2:S1048576"
     If emptyCheck(dataRange) = "Empty" Then
          MsgBox "There is nothing to send"
     Else
          Call RunThemAll()
     End If
End Sub

Function emptyCheck(dataRange)
     If WorksheetFunction.CountA(Range(dataRange)) = 0 Then
          emptyCheck = "Empty"
     Else
         emptyCheck = "Not Empty"
     End If
End Function

Sub RunThemAll()
     Application.Run "vbaAutoRefreshPivot2"
     Application.Run "Notes_Email_Excel_Cells"
End Sub
 
Upvote 0
And just to clarify, N1 is not a range so your last post didn't make sense and I had to assume you meant the range started at N1. The range is N1:S1048576. In the code I started with N2 because of the headers that will never be blank.
 
Upvote 0

Forum statistics

Threads
1,214,583
Messages
6,120,380
Members
448,955
Latest member
BatCoder

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