Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4
Default using vba so search multiple Sheets

i'm trying to search across multiple sheets based on data submitted via an
input box. So essentially, you click the button, a box appears, you type what
your looking for and if it finds it, it'll select it otherwise a error
message appeats.

i found the below code, but it only works if the cell with the value in it
is active (i.e. i've clicked on it).

Code:
Dim datatoFind
Dim sheetCount As Integer
Dim counter As Integer
Dim currentSheet As Integer

On Error Resume Next
currentSheet = ActiveSheet.Index
datatoFind = InputBox("Please enter the value to search for")
If datatoFind = "" Then Exit Sub
sheetCount = ActiveWorkbook.Sheets.Count
If IsError(CDbl(datatoFind)) = False Then datatoFind = CDbl(datatoFind)
For counter = 1 To sheetCount
Sheets(counter).Activate
Cells.Find(What:=datatoFind, After:=ActiveCell, LookIn:=xlFormulas, LookAt _
:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
False).Activate
If ActiveCell.Value = datatoFind Then Exit For
Next counter
If ActiveCell.Value < datatoFind Then
MsgBox ("Value not found")
Sheets(currentSheet).Activate
End If


thanks in advance :)
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 489
Default using vba so search multiple Sheets

This code will scan each worksheet in the activeworkbook looking for the
first instance of the text entered into the Input Box. If the text isn't
found in the entire workbook a message book appears telling the user it
wasn't found. Hope this helps! If so, let me know, click "YES" below.

Option Explicit

Sub FindData()

Dim MyData As String
Dim wks As Worksheet
Dim rngFoundData As Range

' get users data
MyData = InputBox("Please enter the value to search for.")
If MyData = "" Then Exit Sub

' search all sheets in workbook
For Each wks In Worksheets

' find data in current worksheet
Set rngFoundData = wks.Cells.Find(What:=MyData, _
LookIn:=xlFormulas, _
LookAt:=xlWhole, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False)

' select found data and exit sub
If Not rngFoundData Is Nothing Then
wks.Activate
rngFoundData.Select
Exit Sub
End If

Next wks

' tell user data wasn't found
If rngFoundData Is Nothing Then
MsgBox MyData & " was not found in " & ActiveWorkbook.Name,
vbInformation
End If

End Sub
--
Cheers,
Ryan


"Lorcan Dene" wrote:

i'm trying to search across multiple sheets based on data submitted via an
input box. So essentially, you click the button, a box appears, you type what
your looking for and if it finds it, it'll select it otherwise a error
message appeats.

i found the below code, but it only works if the cell with the value in it
is active (i.e. i've clicked on it).

Code:
Dim datatoFind
Dim sheetCount As Integer
Dim counter As Integer
Dim currentSheet As Integer

On Error Resume Next
currentSheet = ActiveSheet.Index
datatoFind = InputBox("Please enter the value to search for")
If datatoFind = "" Then Exit Sub
sheetCount = ActiveWorkbook.Sheets.Count
If IsError(CDbl(datatoFind)) = False Then datatoFind = CDbl(datatoFind)
For counter = 1 To sheetCount
Sheets(counter).Activate
Cells.Find(What:=datatoFind, After:=ActiveCell, LookIn:=xlFormulas, LookAt _
:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
False).Activate
If ActiveCell.Value = datatoFind Then Exit For
Next counter
If ActiveCell.Value < datatoFind Then
MsgBox ("Value not found")
Sheets(currentSheet).Activate
End If


thanks in advance :)

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4
Default using vba so search multiple Sheets

very much appreciated.

thanks!!


"Ryan H" wrote:

This code will scan each worksheet in the activeworkbook looking for the
first instance of the text entered into the Input Box. If the text isn't
found in the entire workbook a message book appears telling the user it
wasn't found. Hope this helps! If so, let me know, click "YES" below.

Option Explicit

Sub FindData()

Dim MyData As String
Dim wks As Worksheet
Dim rngFoundData As Range

' get users data
MyData = InputBox("Please enter the value to search for.")
If MyData = "" Then Exit Sub

' search all sheets in workbook
For Each wks In Worksheets

' find data in current worksheet
Set rngFoundData = wks.Cells.Find(What:=MyData, _
LookIn:=xlFormulas, _
LookAt:=xlWhole, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False)

' select found data and exit sub
If Not rngFoundData Is Nothing Then
wks.Activate
rngFoundData.Select
Exit Sub
End If

Next wks

' tell user data wasn't found
If rngFoundData Is Nothing Then
MsgBox MyData & " was not found in " & ActiveWorkbook.Name,
vbInformation
End If

End Sub
--
Cheers,
Ryan


"Lorcan Dene" wrote:

i'm trying to search across multiple sheets based on data submitted via an
input box. So essentially, you click the button, a box appears, you type what
your looking for and if it finds it, it'll select it otherwise a error
message appeats.

i found the below code, but it only works if the cell with the value in it
is active (i.e. i've clicked on it).

Code:
Dim datatoFind
Dim sheetCount As Integer
Dim counter As Integer
Dim currentSheet As Integer

On Error Resume Next
currentSheet = ActiveSheet.Index
datatoFind = InputBox("Please enter the value to search for")
If datatoFind = "" Then Exit Sub
sheetCount = ActiveWorkbook.Sheets.Count
If IsError(CDbl(datatoFind)) = False Then datatoFind = CDbl(datatoFind)
For counter = 1 To sheetCount
Sheets(counter).Activate
Cells.Find(What:=datatoFind, After:=ActiveCell, LookIn:=xlFormulas, LookAt _
:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
False).Activate
If ActiveCell.Value = datatoFind Then Exit For
Next counter
If ActiveCell.Value < datatoFind Then
MsgBox ("Value not found")
Sheets(currentSheet).Activate
End If


thanks in advance :)

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
search, copy and paste through multiple sheets Jeff S.[_2_] Excel Discussion (Misc queries) 1 August 16th 09 12:08 AM
Search Multiple Sheets Then Add Txt1.Txt to Cell [email protected] Excel Programming 2 October 31st 07 02:00 PM
Search in Multiple Sheets [email protected] Excel Discussion (Misc queries) 4 September 12th 07 10:33 PM
Search multiple sheets, then paste results in new sheet Paul M[_6_] Excel Programming 1 January 8th 06 07:26 PM
Search across multiple sheets and subtotal. Pat Bell Excel Programming 1 December 8th 03 03:16 PM


All times are GMT +1. The time now is 07:29 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"