View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
Patrick Molloy Patrick Molloy is offline
external usenet poster
 
Posts: 1,049
Default Modification of Recorded Macro

here's a code example:

the main procedure is teh FindDemo procedure. This simply calls the main
routine, passing the value to look for.
the error you have occurs when there's no cell containing the value sought.
Excel raises an error. In this code, the object 'cell' is set to each find,
thus initially, if there's at least on cell with the sought after value,
then there will be a valid object. However, if no cells are found, the
object isn't created , ie it IS NULL

Option Explicit
Sub FindDemo()
FindCells "B"
End Sub
Sub FindCells(FindText As String)
Dim cell As Range
Dim startAddress As String
Set cell = Sheet1.Cells.Find(FindText)
If Not cell Is Nothing Then
startAddress = cell.Address
Do
'do something
cell.Select
Set cell = Sheet1.Cells.FindNext(cell)
Loop While cell.Address < startAddress
Else
msgbox "No cells match " & FindText
End If

'
End Sub


"Greg" wrote in message
...
If I am in the wrong newsgroup I apologize. Maybe someone can direct me to
the correct group.

I have an excel workbook with one column of data on sheet 1. I have
recorded
a Macro that moves certain data from sheet 1 to specified columns on sheet
2
of the workbook .

In some cases I needed to record several "find" i.e. EDIT-Find to locate
on
sheet 1 the information I need to move to sheet 2.

My problem occurs certain months when the "find" does not locate the data
I
am searching for. If I was doing this manually, I would set a default
value
in the appropriate column of sheet 2 and continue.

However when my macro runs and encounters this situation I receive a
runtime
error message and of course the Macro halts.

My question is can I :

1. Detect the runtime error before stopping execution of the Macro by
adding code to my Macro
2. Put a default value in the required column of sheet 2
3. Continue with the Macro at the point I received the runtime error

I am new at Macros and VBA.

Thank you in advance for your assistance.

Greg