Search ALL sheets

isildad

Active Member
Joined
Sep 22, 2004
Messages
325
Here is the code I try to adapt to search in ALL sheets and not just Sheet1 (Code is in a userform)

How do you change Sheet.("Sheet1") to "All Sheets" ?
Thank you for your help

Option Explicit

Private Sub CommandButton1_Click()
Dim rngfind As Range
With Sheets("sheet1")
Set rngfind = .Range("B:B").Find(Me.TextBox1.Value, lookat:=xlWhole, MatchCase:=True)
If rngfind Is Nothing Then
MsgBox "Not Found"
Else
Me.TextBox2.Value = rngfind.Offset(0, 1).Value
Me.TextBox3.Value = rngfind.Offset(0, 2).Value
End If
End With
End Sub
 

Some videos you may like

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.

Doug.T

Board Regular
Joined
Sep 19, 2006
Messages
80
Private Sub CommandButton1_Click()
Dim rngfind As Range

For each sh in activeworkbook.sheets

With Sheets(sh.name)
Set rngfind = .Range("B:B").Find(Me.TextBox1.Value, lookat:=xlWhole, MatchCase:=True)
If rngfind Is Nothing Then
MsgBox "Not Found"
Else
Me.TextBox2.Value = rngfind.Offset(0, 1).Value
Me.TextBox3.Value = rngfind.Offset(0, 2).Value
End If
End With

next sh

End Sub
 

isildad

Active Member
Joined
Sep 22, 2004
Messages
325
Thank you , have tried the code and get "Compile error Variable not defined with the sh highlighted in: For Each sh In ActiveWorkbook.Sheets
 

pgc01

MrExcel MVP
Joined
Apr 25, 2006
Messages
19,834
Hi isildad

Variable not defined with the sh highlighted

If you know sh is not defined why don't you define it?

Since sh is a worksheet, add it to the declarations

Dim rngfind As Range, sh as Worksheet

HTH
PGC
 

isildad

Active Member
Joined
Sep 22, 2004
Messages
325

ADVERTISEMENT

Thank you for the replies
Here is the code I now have.
It works in finding in either sheets but in spite of it the warning "not found" comes up every time.

Option Explicit

Private Sub CommandButton1_Click()

Dim rngfind As Range, sh As Worksheet
For Each sh In ActiveWorkbook.Sheets

With Sheets(sh.Name)
Set rngfind = .Range("B:B").Find(Me.TextBox1.Value, lookat:=xlWhole, MatchCase:=True)
If rngfind Is Nothing Then
MsgBox "Not Found"
Else
Me.TextBox2.Value = rngfind.Offset(0, 1).Value
Me.TextBox3.Value = rngfind.Offset(0, 2).Value
End If
End With

Next sh

End Sub
 

jindon

MrExcel MVP
Joined
Aug 21, 2004
Messages
16,995
try
Code:
Option Explicit

Private Sub CommandButton1_Click()

Dim rngfind As Range, sh As Worksheet
For Each sh In ActiveWorkbook.Sheets

With Sheets(sh.Name)
Set rngfind = .Range("B:B").Find(Me.TextBox1.Value, lookat:=xlWhole, MatchCase:=True)
If Not rngfind Is Nothing Then
Me.TextBox2.Value = rngfind.Offset(0, 1).Value
Me.TextBox3.Value = rngfind.Offset(0, 2).Value
Exit Sub
End If
End With

Next sh
MsgBox "Not Found"
End Sub
 

Watch MrExcel Video

Forum statistics

Threads
1,108,911
Messages
5,525,591
Members
409,652
Latest member
strangelyangely

This Week's Hot Topics

Top