There are lots of different cases. if there is a problem see if you can
narrrow down the problem. I made soem improvemements . Make sure you
include the Global Variables which get placed outside any of the
functions so the code remembers the settings each time it is run.
'GLOBAL Variables
'need a variable outside a sub so code will continue
'after first bar code is found and give you athe ability
'to search multiple pages.
Dim StartCell As Range
'the first Address on each page
Dim StartPage As Range
Dim FirstPage As Boolean
Sub FindFirstBarCode()
'end code is cell is blank
If ActiveCell = "" Then
Exit Sub
End If
'code to start search like using Find rather than FindNext
'this is required if you have multiple bar codes with
'the same number on the same page
Set StartCell = ActiveCell
Set StartPage = StartCell
FirstPage = True
First = True
Call FindBarCode(First)
End Sub
Sub FindNextBarCode()
'end code is cell is blank
If ActiveCell = "" Then
Exit Sub
End If
'code to start search like using Find rather than FindNext
'this is required if you have multiple bar codes with
'the same number on the same page
First = False
Call FindBarCode(First)
End Sub
Sub FindBarCode(ByVal First As Boolean)
'end code is cell is blank
If ActiveCell = "" Then
Exit Sub
End If
If StartCell Is Nothing Then
'need this if you didn't run Findfirst after opening workbook
Set StartCell = ActiveCell
Set StartPage = ActiveCell
FirstPage = True
Else
Finish = False
If StartCell.Address(external:=True) = _
ActiveCell.Address(external:=True) Then
If Sheets.Count = 1 Then
Finish = True
Else
If FirstPage = False Then
Finish = True
End If
End If
End If
If Finish = True Then
response = MsgBox(prompt:="Found All Cells. Do you want to start
Again", _
Buttons:=vbYesNo)
If response = vbNo Then
Exit Sub
Else
Set StartCell = ActiveCell
Set StartPage = ActiveCell
FirstPage = True
End If
End If
End If
BarCode = StartCell.Value
Do
'startpage.parent is current sheet
If First = True Then
Set c = StartPage.Parent.Cells.Find(what:=BarCode, _
after:=ActiveCell, LookIn:=xlValues, lookat:=xlWhole)
Else
Set c = StartPage.Parent.Cells.FindNext(after:=ActiveCell)
End If
NewPage = False
If c Is Nothing Then
NewPage = True
Else
If c.Address(external:=True) = _
StartPage.Address(external:=True) Or _
c.Address(external:=True) = ActiveCell.Address(external:=True)
Then
NewPage = True
End If
End If
If NewPage = True Then
'Move to next page
If Sheets.Count > 1 Then
'only do if there is more that one page in workbook
'index is page count
PageNumber = StartPage.Parent.Index
If PageNumber = Sheets.Count Then
'Move to page 1
Set StartPage = Sheets(1).Range("A1")
Else
'Move to next Page
Set StartPage = Sheets(PageNumber + 1).Range("A1")
End If
FirstPage = False
End If
If StartPage.Value = BarCode Then
Set c = StartPage
Exit Do
End If
Else
Exit Do
End If
'loop to next page
Loop While StartPage.Address(external:=True) <>
StartCell.Address(external:=True)
If Not c Is Nothing Then
c.Parent.Activate
Application.Goto Reference:=c
End If
End Sub
--
joel
------------------------------------------------------------------------
joel's Profile: 229
View this thread:
http://www.thecodecage.com/forumz/showthread.php?t=162180
Microsoft Office Help
.