Solved Macro to check for column before executing - EXCEL

October 8, 2012 at 00:50:16
Specs: Windows XP
I am looking to write a Macro that will search the first row of a table of data (the row containing the collumn headings) for 5 specificly named headings (call them Heading1, heading2 etc...), and only IF all of those headings are found,continue to run the rest of the macro. Otherwise give an error or exit the macro.

The Macro itself runs a series of calculations and looks up information from the table to compile it into more managable data, but if all of the required collumns are not their then it will return an error and this causes me problems. I want to pre-empt the error and prevent the macro from executing.

Thank you in advance. Finley

See More: Macro to check for column before executing - EXCEL

Report •

October 8, 2012 at 07:11:52
✔ Best Answer
You can certainly check for the Column Heading within the macro or you can use a Error Handling routine with On Error. With On Error you can force the Macro to stop execution or present a message to the user about the mssing column or basically do whatever you want it to do when an error occurs. You can even test the error value and do different things based on what error occurred.

Look up the On Error statement in the VBA Help files or via Google. Using error handling routines is very powerful.

If all you want to do is check for the Column heading and exit the macro if the heading isn't found, this is one of the many ways it can be done:

Sub FindColumnHeading()
  With Range("B5:N5")
    Set c = .Find("MyColumnHeading")
      If c Is Nothing Then Exit Sub
  End With
End Sub

Click Here Before Posting Data or VBA Code ---> How To Post Data or Code.

Report •
Related Solutions

Ask Question