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
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().

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,884
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
 

Forum statistics

Threads
1,141,227
Messages
5,705,137
Members
421,380
Latest member
Nuwan Sanjeewa Aponso

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Top