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

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)

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,141,629
Messages
5,707,506
Members
421,511
Latest member
mgroah1

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