Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 13
Default Can someone help me make this code more robust?

As part of a data manipulation / data prep macro I need to locate certain
fields and copy their values to a new location. The way I try to do this is
to search for a caption cell to that field and then to select the data
relative to the caption field, then copy that data and past it in a new
location.

Here is a code sample:
=========================
'Locate the field that contains the main process name
Sheets(Sheetname).Select
Range("A4").Select
Cells.Find(What:="Significant Process:", After:=ActiveCell,
LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Activate
ActiveCell.Offset(0, 1).Select

Application.CutCopyMode = False
Selection.Copy
Sheets("Import This").Select
ActiveSheet.Paste

=========================

Unfortunately some of the worksheets I need to convert are inconsistent in
their format. Some workbooks don't even have the field I am looking for. So
let's say I am looking for a cell with the value :="Significant Process:"
and it just doesn't exist. Right now the macro just bombs out and I have to
go to debug and figure out what to do next. Generally the process is the
same, I skip the procedure for manipulating that particular piece of data
and go onto the next set of data that I am looking for.

What I don't know is how to trap a "Data Not Found" error using Excel's
search capability. This is one thing I need help with.


In addition, I have noted that in some cases there may actually be more than
one cell with the same data string in it. I have sort of bypassed this
problem by starting my search at a specified location. I know that the
search can be ordered by specified directions, so if I start at a cell that
would be just before the potential location of the string I am looking for I
can be pretty sure I am hitting the one I want. Are there other strategies
I could use here that might say alert me to the existence of multiple cells
with the same data?

--
Henry Stock, Network Administrator
onProject.com
3 Wing Drive
Cedar Knolls, NJ 07927-1006


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default Can someone help me make this code more robust?

Dim rng as Range
set rng = Nothing
'Locate the field that contains the main process name
Sheets(Sheetname).Select
Range("A4").Select
set rng = Cells.Find(What:="Significant Process:", _
After:=ActiveCell, _
LookIn:=xlFormulas, _
LookAt:=xlPart, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False, _
SearchFormat:=False)
if not rng is nothing then
set rng1 = rng.offset(0,1)
rng.copy
Sheets("Import This").Activate
ActiveSheet.Paste
else
Sheets("Import This").Activate
End if
' move to next entry location

--
Regards,
Tom Ogilvy

"Henry Stock" wrote in message
...
As part of a data manipulation / data prep macro I need to locate certain
fields and copy their values to a new location. The way I try to do this

is
to search for a caption cell to that field and then to select the data
relative to the caption field, then copy that data and past it in a new
location.

Here is a code sample:
=========================
'Locate the field that contains the main process name
Sheets(Sheetname).Select
Range("A4").Select
Cells.Find(What:="Significant Process:", After:=ActiveCell,
LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Activate
ActiveCell.Offset(0, 1).Select

Application.CutCopyMode = False
Selection.Copy
Sheets("Import This").Select
ActiveSheet.Paste

=========================

Unfortunately some of the worksheets I need to convert are inconsistent

in
their format. Some workbooks don't even have the field I am looking for.

So
let's say I am looking for a cell with the value :="Significant Process:"
and it just doesn't exist. Right now the macro just bombs out and I have

to
go to debug and figure out what to do next. Generally the process is the
same, I skip the procedure for manipulating that particular piece of data
and go onto the next set of data that I am looking for.

What I don't know is how to trap a "Data Not Found" error using Excel's
search capability. This is one thing I need help with.


In addition, I have noted that in some cases there may actually be more

than
one cell with the same data string in it. I have sort of bypassed this
problem by starting my search at a specified location. I know that the
search can be ordered by specified directions, so if I start at a cell

that
would be just before the potential location of the string I am looking for

I
can be pretty sure I am hitting the one I want. Are there other

strategies
I could use here that might say alert me to the existence of multiple

cells
with the same data?

--
Henry Stock, Network Administrator
onProject.com
3 Wing Drive
Cedar Knolls, NJ 07927-1006




Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
How do I make a Bar code ??? Wadewild Excel Discussion (Misc queries) 2 June 9th 08 09:08 PM
Are there functions that perform robust statistics in Excel? froot_broot Excel Worksheet Functions 0 August 30th 05 10:18 PM
How to make VBA code to self-destroy itself? count Excel Programming 6 November 10th 04 05:11 PM
Make code more economical Mark C[_5_] Excel Programming 4 October 5th 04 04:06 PM
Make a Change to Code Steved[_3_] Excel Programming 2 September 29th 04 11:31 PM


All times are GMT +1. The time now is 01:59 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"