PC Review


Reply
Thread Tools Rate Thread

Avoiding Redundancy (.Find Method)

 
 
Smartin
Guest
Posts: n/a
 
      30th Sep 2007
While somewhat experienced with VBA I am not so experienced working
with Excel objects.

My objective is to loop through multiple worksheets (currently 150+),
find a keyword on each worksheet, and return the value in the cell two
columns to the right. The keyword will exist zero or one times per
sheet.

After studying examples I was able to piece together the following
code, which works. But, I have to think the redundant call to .Find is
unnecessary, as commented in the remark midway.

Any suggestions to improve this? Many thanks.

' ------------ VBA CODE BEGIN ------------
Sub FindInAllSheets()
Const D As String = ";"
Dim sh As Worksheet
Dim r1 As Range

For Each sh In Application.ThisWorkbook.Worksheets
Debug.Print sh.Name & D;
Set r1 = Nothing
sh.Activate
' CAN I AVOID CALLING .Find TWICE HERE?
If Not sh.Cells.Find([parameters]) Is Nothing Then
sh.Cells.Find([parameters]).Activate
Set r1 = Selection
End If
If Not r1 Is Nothing Then
Debug.Print r1.Offset(0, 2).Value
Else
Debug.Print "not found"
End If
Next sh
Worksheets(1).Activate
Set r1 = Nothing
End Sub
' ------------ VBA CODE END ------------

 
Reply With Quote
 
 
 
 
=?Utf-8?B?QW5hbnQgQmFzYW50?=
Guest
Posts: n/a
 
      30th Sep 2007
Hi Smartin,

Hope the following code is what you want.

Sub FindInAllSheets()
Const D As String = ";"
Dim sh As Worksheet
Dim r1 As Range

For Each sh In Application.ThisWorkbook.Worksheets
Debug.Print sh.Name & D;
Set r1 = Nothing
sh.Activate

Set r1 = sh.Cells.Find([Parameters])

' CAN I AVOID CALLING .Find TWICE HERE?
If Not r1 Is Nothing Then
'value found do something
Debug.Print r1.Offset(0, 2).Value
Else
Debug.Print "not found"
End If
Next sh
Worksheets(1).Activate
Set r1 = Nothing
End Sub

If you need some more tweaking, please write back.
--
Anant


"Smartin" wrote:

> While somewhat experienced with VBA I am not so experienced working
> with Excel objects.
>
> My objective is to loop through multiple worksheets (currently 150+),
> find a keyword on each worksheet, and return the value in the cell two
> columns to the right. The keyword will exist zero or one times per
> sheet.
>
> After studying examples I was able to piece together the following
> code, which works. But, I have to think the redundant call to .Find is
> unnecessary, as commented in the remark midway.
>
> Any suggestions to improve this? Many thanks.
>
> ' ------------ VBA CODE BEGIN ------------
> Sub FindInAllSheets()
> Const D As String = ";"
> Dim sh As Worksheet
> Dim r1 As Range
>
> For Each sh In Application.ThisWorkbook.Worksheets
> Debug.Print sh.Name & D;
> Set r1 = Nothing
> sh.Activate
> ' CAN I AVOID CALLING .Find TWICE HERE?
> If Not sh.Cells.Find([parameters]) Is Nothing Then
> sh.Cells.Find([parameters]).Activate
> Set r1 = Selection
> End If
> If Not r1 Is Nothing Then
> Debug.Print r1.Offset(0, 2).Value
> Else
> Debug.Print "not found"
> End If
> Next sh
> Worksheets(1).Activate
> Set r1 = Nothing
> End Sub
> ' ------------ VBA CODE END ------------
>
>

 
Reply With Quote
 
Smartin
Guest
Posts: n/a
 
      30th Sep 2007
Yes, that's perfect. Thanks again, Anant.

On Sep 29, 10:01 pm, Anant Basant
<AnantBas...@discussions.microsoft.com> wrote:
> Hi Smartin,
>
> Hope the following code is what you want.
>
> Sub FindInAllSheets()
> Const D As String = ";"
> Dim sh As Worksheet
> Dim r1 As Range
>
> For Each sh In Application.ThisWorkbook.Worksheets
> Debug.Print sh.Name & D;
> Set r1 = Nothing
> sh.Activate
>
> Set r1 = sh.Cells.Find([Parameters])
>
> ' CAN I AVOID CALLING .Find TWICE HERE?
> If Not r1 Is Nothing Then
> 'value found do something
> Debug.Print r1.Offset(0, 2).Value
> Else
> Debug.Print "not found"
> End If
> Next sh
> Worksheets(1).Activate
> Set r1 = Nothing
> End Sub
>
> If you need some more tweaking, please write back.
> --
> Anant
>
> "Smartin" wrote:
> > While somewhat experienced with VBA I am not so experienced working
> > with Excel objects.

>
> > My objective is to loop through multiple worksheets (currently 150+),
> > find a keyword on each worksheet, and return the value in the cell two
> > columns to the right. The keyword will exist zero or one times per
> > sheet.

>
> > After studying examples I was able to piece together the following
> > code, which works. But, I have to think the redundant call to .Find is
> > unnecessary, as commented in the remark midway.

>
> > Any suggestions to improve this? Many thanks.

>
> > ' ------------ VBA CODE BEGIN ------------
> > Sub FindInAllSheets()
> > Const D As String = ";"
> > Dim sh As Worksheet
> > Dim r1 As Range

>
> > For Each sh In Application.ThisWorkbook.Worksheets
> > Debug.Print sh.Name & D;
> > Set r1 = Nothing
> > sh.Activate
> > ' CAN I AVOID CALLING .Find TWICE HERE?
> > If Not sh.Cells.Find([parameters]) Is Nothing Then
> > sh.Cells.Find([parameters]).Activate
> > Set r1 = Selection
> > End If
> > If Not r1 Is Nothing Then
> > Debug.Print r1.Offset(0, 2).Value
> > Else
> > Debug.Print "not found"
> > End If
> > Next sh
> > Worksheets(1).Activate
> > Set r1 = Nothing
> > End Sub
> > ' ------------ VBA CODE END ------------



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
xls vba find method to find row that contains the current date =?Utf-8?B?UkNyYW5zdG9u?= Microsoft Excel Programming 5 28th Mar 2007 03:59 PM
date find using find method x taol Microsoft Excel Programming 2 22nd Dec 2005 09:51 AM
Using variables to make a date and using find method to find that. =?Utf-8?B?S3lXaWxkZQ==?= Microsoft Excel Programming 2 21st Apr 2005 09:43 PM
Best method for print server redundancy Howard Microsoft Windows 2000 Printing 2 15th Jul 2004 02:50 AM
AD Redundancy - Domain Controller redundancy Steven Microsoft Windows 2000 Active Directory 4 3rd Sep 2003 03:05 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 10:09 PM.