Conditions Check for a Macro

magnusver

New Member
Joined
Jul 12, 2006
Messages
11
Is there a way to build into a macro a check to see if preset conditions are met and produce an error message if those conditions are not met?

Here is the condition as if it were a formula in Excel:
=IF(COUNTIF(Output!E2:E101,5)<19,"do macro","post an error message with text I can edit")

Here is the existing code for the macro:

Sheets("Output").Select
Range("D2").Select
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Workbooks.Add
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
'ActiveWorkbook.SaveAs Filename:= _
"C:\Documents and Settings\nmagnuso\Desktop\Book4.csv", FileFormat:=xlCSV, _
CreateBackup:=False

fileSaveName = Application.GetSaveAsFilename( _
fileFilter:="CSV Files(*.csv), *.csv")
If fileSaveName <> False Then
MsgBox "Save as " & fileSaveName
End If

ActiveWorkbook.SaveAs Filename:=fileSaveName, FileFormat:=xlCSV
End Sub



Thanks for your help.

magnusver
 

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.

acw

MrExcel MVP
Joined
Feb 13, 2004
Messages
4,814
Hi

Does this approach get you started?


Code:
Sub ccc()
  Dim GetMessageText As String
  Sheets("output").Select
  
 If WorksheetFunction.CountIf(Range("B5:F11"), 5) < 19 Then
   'do macro
 Else
   GetMessageText = InputBox("Enter the message you want to send.")
   MsgBox GetMessageText
End If

End Sub


Tony
 

Forum statistics

Threads
1,176,454
Messages
5,903,205
Members
435,014
Latest member
chriswind

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
Top