Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 175
Default ForEach statement problem

This macro will search for records based on a PO# given by the user.
Often, there are more than one record with that PO#. The code, up to
the End If statement works. I wanted to add code so that I can show in
UserForm13 which of the found records is showing (1 of 4, 2 of 4, etc).
I already have a textbox showing the total records found. I wanted the
ForEach part to "assign" a distinct number to that record, so I can
show it in another textbox.
Obviously, I'm not setting this up correctly. In the ForEach
statement, I'm getting an error when it reads rndFound.
Can someone help me with ideas on what I need to change here?
Thanks
Much appreciated
J.O.

'These are the Standard module declarations
Public rngToSearch As Range
Public rngFound As Range
Public strFirst As String
Public FindPOVal As String

Sub TestFind_POCurrent()
Worksheets("Official List").Activate

Set rngToSearch = Sheets("Official List").Columns("J")
Set rngFound = rngToSearch.Find(What:=FindPOVal, _
LookIn:=xlValues, _
LookAt:=xlWhole)
If rngFound Is Nothing Then
MsgBox "This record was not found. Please try again."
Unload UserForm12
UserForm12.Show
Else

strFirst = rngFound.Address
rngFound.Selec
End If

Dim RecordsFound As Integer

'******** this is the part showing the error, in the rngFound part.
*******
'******** I thought I could use rngFound to identify each record in the
range. ******
For Each rngFound In rngToSearch .
RecordsFound = RecordsFound + 1

Next rngFound

Unload UserForm12
UserForm13.Show

End Sub

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 595
Default ForEach statement problem

excelnut1954 wrote:
'******** this is the part showing the error, in the rngFound part.
*******
'******** I thought I could use rngFound to identify each record in
the range. ******
For Each rngFound In rngToSearch .
RecordsFound = RecordsFound + 1

Next rngFound


This part isn't doing what you think. rngFound pointed to the cell returned
by the Find method, but using it in the For Each destory's that pointer and
points it to J1 (on the first loop).

Since you don't use the After argument in the Find method, Find is always
going to return the first one it finds. If you display "Record x of y", x
will always be 1. To find y, you need to use FindNext until it loops back
to the first one and count them along the way. All this should be in the
Else part of your If rngFound Is Nothing block.

If you want the user to be able to loop through all the POs, you'll need
another variable to hold all the cells that are found.

Else
strFirst = rngFound.Address
Set rngAllFound = rngFound
Do
Set rngFound = rngToSearch.FindNext(rngFound)
Set rngAllFound = Union(rngAllFound, rngFound)
Loop Until rngFound.Address = strFirst

RecordsFound = rngAllFound.Cells.Count

Now rngAllFound will be a range of cells with that PO number and rngFound
will be pointing to the first one it finds.

If you can clarify what you're doing, I may be able to give you more
specific help. You can see an example of the Find method here

http://www.dailydoseofexcel.com/arch...e-find-method/

--
Dick Kusleika
MS MVP - Excel
www.dailydoseofexcel.com


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
If statement problem Scott@CW Excel Discussion (Misc queries) 2 October 5th 07 03:22 AM
Problem with IF statement Tanya Excel Worksheet Functions 9 July 1st 07 02:10 PM
problem with IF statement wolfpack95 Excel Discussion (Misc queries) 1 August 21st 06 08:36 PM
Problem with IF statement Jonibenj Excel Discussion (Misc queries) 5 August 18th 05 12:06 AM
ForEach procedure adding values Mats Westin Excel Programming 4 February 12th 04 07:30 AM


All times are GMT +1. The time now is 10:20 AM.

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

About Us

"It's about Microsoft Excel"