You are here:

Excel/VBA Error Handling

Advertisement


Question
Hi Tom,
Using Excel 2007 VBA
Here's my code so far:

Sub findPrevMonth()
   
   Dim CurMo As Date
   Dim LstMo As Date
   Dim LstMoNm As String
   Dim LstMoRow
   CurMo = Now
   LstMo = WorksheetFunction.EoMonth(CurMo, -1)
   LstMoNm = Format(LstMo, "mmmm")
   
   Cells.Find(What:=LstMoNm, After:=ActiveCell, LookIn:=xlFormulas, _
       LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
       MatchCase:=False, SearchFormat:=False).Activate

   LstMoRow = ActiveCell.Row
   Range("e" & LstMoRow).Select
       

End Sub

This finds the cell in ColA that contains the name of the previous month. There is only one instance of each month name. I'll be copying data from various cells in that row. So far, so good. (My code is probably pretty lame, but it works).
My question is this: If the month name is missing in ColA, or misspelled, I'd like a box to pop up telling the user to fix it before the macro will continue.
Thanks!

Answer
Doug,

Well there is no pausing a macro to allow the user to edit the sheet.

You can have your code warn and quit.  Also, you use cells, but you said you wanted to look in column A.


Sub findPrevMonth()
  
  Dim CurMo As Date
  Dim LstMo As Date
  Dim LstMoNm As String
  Dim LstMoRow
  CurMo = Now
  LstMo = WorksheetFunction.EoMonth(CurMo, -1)
  LstMoNm = Format(LstMo, "mmmm")
  if application.Countif(columns(1), LstMoNm) = 0 then
      msgbox "Month " & LstMoNm & " not found in column A; fix it!!!"
      exit sub
  End if
  Columns(1).Find(What:=LstMoNm, After:=ActiveCell, LookIn:=xlFormulas, _
      LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
      MatchCase:=False, SearchFormat:=False).Activate

  LstMoRow = ActiveCell.Row
  Range("e" & LstMoRow).Select
      

End Sub
--
Regards,
Tom Ogilvy

About Excel
This topic answers questions related to Microsoft Excel spreadsheet (or workbook) stand-alone or Mircrosoft Office Excel including Excel 2003, Excel 2007, Office 2000, and Office XP. You can get Excel help on Excel formulas(or functions), Excell macros, charting in Excel, advanced features, and the general use of Excel. This does not provide a general Excel tutorial nor the basics of using a spreadsheet. It provides specific answers to using Microsoft Excel only. If you do not see your Excel question answered in this area then please ask an Excel question here

Excel

All Answers


Answers by Expert:


Ask Experts

Volunteer


Tom Ogilvy

Expertise

Selected as an Excel MVP by Microsoft since 1999. Answering Excel questions in Allexperts since its inception in 2001. Able to answer questions on almost all aspects of Excel's internal capabilities. If seeking a VBA solution, please specify that in your question itself so I give you the answer you want. [Excel has weak protection - if you are distributing an application, I don't answer questions on how to protect your project from your users.]

Experience

Extensive experience.

Education/Credentials
Master of Science (MS) degree Operations Research (ORSA)

Awards and Honors
Microsoft MVP in Excel.

©2016 About.com. All rights reserved.