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

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
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
 
Upvote 0
Thank you , have tried the code and get "Compile error Variable not defined with the sh highlighted in: For Each sh In ActiveWorkbook.Sheets
 
Upvote 0
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
 
Upvote 0
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
 
Upvote 0
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
 
Upvote 0

Forum statistics

Threads
1,214,950
Messages
6,122,438
Members
449,083
Latest member
Ava19

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
Back
Top