Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default List Search Results in a UserForm

One thing that I've found to be a pain in Excel VBA is performing
search in a document. But with the help from a few of you in thi
forum, I've been able to come up with something that I think work
pretty good (I may be wrong, but what do I know... I've only been usin
Excel and VBA for about a month now). So here's what I'm giving bac
to this group.

This may need a little bit of an intro...
First, just copy and paste this code into a blank UserForm.
Add a CommandButton and rename it to cmdClose.
Add a ListView control and rename it to lvwSearchResults.

Now add some code to call the form and perform a search.
The results will be put into 3 columns: Sheet name, cell address an
cell value.
When you click on any of the entries, it will auto-magically take yo
to the selected sheet/cell.

Enjoy!


Option Explicit


Private Sub cmdClose_Click()
Unload Me
End Sub

Private Sub lvwSearchResults_Click()
Dim i As Integer
Dim strSheet As String
Dim strCell As String

i = lvwSearchResults.SelectedItem.Index
strSheet = lvwSearchResults.ListItems.Item(i)
strCell = lvwSearchResults.ListItems(i).ListSubItems(1).Text

Sheets(strSheet).Select
Range(strCell).Select
End Sub

Private Sub UserForm_Initialize()
Dim oSheet As Object
Dim Firstcell As Range
Dim NextCell As Range
Dim WhatToFind As Variant
Dim strCellText As String

lvwSearchResults.ListItems.Clear
lvwSearchResults.ColumnHeaders.Add (1), , "Sheet Name"
lvwSearchResults.ColumnHeaders.Add (2), , "Cell Address"
lvwSearchResults.ColumnHeaders.Add (3), , "Cell Value"

WhatToFind = Application.InputBox("What are you looking for ?"
"Search", , 100, 100, , , 2)
If WhatToFind < "" And Not WhatToFind = False Then
For Each oSheet In ActiveWorkbook.Worksheets
oSheet.Activate
oSheet.[a1].Activate
Set Firstcell = Cells.Find(What:=WhatToFind
LookIn:=xlValues, LookAt:=xlPart, SearchOrder:=xlByRows
SearchDirection:=xlNext, MatchCase:=False)
If Not Firstcell Is Nothing Then
Firstcell.Activate
lvwSearchResults.ListItems.Add (1), , oSheet.Name
lvwSearchResults.ListItems(1).SubItems(1)
Firstcell.Address ' WhatToFind
strCellText = Firstcell.Value
lvwSearchResults.ListItems(1).SubItems(2)
strCellText
On Error Resume Next
While (Not NextCell Is Nothing) And (No
NextCell.Address = Firstcell.Address)
Set NextCell = Cells.FindNext(After:=ActiveCell)
If Not NextCell.Address = Firstcell.Address Then
NextCell.Activate
lvwSearchResults.ListItems.Add (1),
oSheet.Name
lvwSearchResults.ListItems(1).SubItems(1)
NextCell.Address ' WhatToFind
strCellText = NextCell.Value
lvwSearchResults.ListItems(1).SubItems(2)
strCellText
End If
Wend
End If
Set NextCell = Nothing
Set Firstcell = Nothing
Next oSheet
End If
End Sub

~ Matt

--
Message posted from http://www.ExcelForum.com

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
Help! I can't get any search results Acanesfan Excel Discussion (Misc queries) 4 October 10th 08 10:16 PM
Search Results Loadmaster Excel Discussion (Misc queries) 0 July 29th 08 01:52 PM
Can I create a list from the results of a file search in Excel? jeejee_r Excel Discussion (Misc queries) 1 May 25th 06 07:25 PM
How can I list the results of my macro without overwritng previous results? mattip Excel Programming 3 November 28th 03 03:45 AM
Search or FIND on a userform. Phillips Excel Programming 2 November 26th 03 09:55 AM


All times are GMT +1. The time now is 09:50 PM.

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

About Us

"It's about Microsoft Excel"