Find value of active cell in other visible worksheets
If you're using a commandbutton from the control toolbox toolbar, then the code
will be behind the worksheet with that commandbutton. (You don't need to call a
separate routine in a General module.) But you are tied into the name of the
procedure. It'll be the commandbutton's name_Click:
Option Explicit
Private Sub CommandButton1_Click()
Dim myVal As Variant 'long, string, double???
Dim wks As Worksheet
Dim FoundCell As Range
myVal = ActiveCell.Value
For Each wks In Me.Parent.Worksheets
With wks
If Me.Name = wks.Name Then
'skip this sheet
Else
If .Visible = xlSheetVisible Then
Set FoundCell = .Cells.Find(What:=myVal, _
After:=.Cells(.Cells.Count), _
LookIn:=xlFormulas, _
LookAt:=xlPart, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False, _
SearchFormat:=False)
If FoundCell Is Nothing Then
MsgBox myVal & " was NOT found on " & .Name
Else
MsgBox myVal & " was found on " & .Name _
& " in " & FoundCell.Address(0, 0)
'go there and stop looking?
'Application.Goto FoundCell, Scroll:=True
'Exit For
End If
End If
End If
End With
Next wks
End Sub
By using a variable (FoundCell), you can avoid the .activate error. Then just
check to see if the value was found (with "if foundcell is nothing").
RiverGully wrote:
Starting in the first worksheet (Sheet00.Name), I wish to search all visible
worksheets for a value (value of the active cell).
(The user of the spreadsheet will click a command button for this VBA to run)
[I have turned off the On Error instruction, while testing this out...]
Private Sub FindCellContent()
'On Error GoTo addError1
Dim CellLocation As Range
Dim CellContent As String
Dim Sh As Worksheet
' Capture cell information
Set CellLocation = ActiveCell
CellContent = ActiveCell
' Start at the first Worksheet
Sheets(Sheet31.Name).Select
Range("A1").Select
' Search for CellContent in all Visible Worksheets
For Each Sh In ThisWorkbook.Worksheets
' If Sh.Visible = xlSheetVisible Then
If Cells.Find(What:=CellContent, After:=ActiveCell,
LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Activate Then
Exit For
End If
' End If
Next
'addError1:
' Application.GoTo Reference:=CellLocation
' MsgBox "Cell content not found in other worksheets"
' Exit Sub
End Sub
Thank you... Clive
--
Dave Peterson
|