Excel Search Macro Help

Thread Solved

Join Date: Mar 2006
Posts: 195
Reputation: HI2Japan is an unknown quantity at this point 
Solved Threads: 21
HI2Japan HI2Japan is offline Offline
Junior Poster

Excel Search Macro Help

 
0
  #1
Mar 2nd, 2006
Hi all,

I am a beginner at this so sorry for my ignorance.

I'm trying to make a macro that searches an Excel Spread sheet. I plan on using Data validation in cell A1 that the user selects the value they want to see, then I want them to be able to click a button roughly in cell A2 that will initiate the search through that worksheet only. I know how to make the butotn and assign a macro to it, I don't know how to make the macro search off of a value in a cell. The search just needs to take them to the specified location in the worksheet.

I do not know if this will be a difficult task, but all of my searching of google turned up nothing. Any help would be greatly appreciated.

Thank you
Reply With Quote Quick reply to this message  
Join Date: Nov 2005
Posts: 134
Reputation: Yomet is an unknown quantity at this point 
Solved Threads: 10
Yomet Yomet is offline Offline
Junior Poster

Re: Excel Search Macro Help

 
0
  #2
Mar 3rd, 2006
HI2Japan,

I had this sitting from another posting here, just modified it slightly.

Try this out
Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
  1. Public Sub Test()
  2. Dim SR As Integer
  3. Dim ER As Integer
  4. Dim SC As Integer
  5. Dim EC As Integer
  6. Dim RowVar As Integer
  7. Dim ColVar As Integer
  8. Dim found As Boolean
  9.  
  10. 'Change the indexes for your own Row range from 1 to 65536
  11. SR = 1
  12. ER = 20
  13. 'Change the indexes for your own Column range from 1 to 256 - in English from "A" to "IV"
  14. SC = 1
  15. EC = 10
  16. found = False
  17. For RowVar = SR To ER
  18. For ColVar = SC To EC
  19. If Not found Then 'This ensures that you stop at the first occurrence
  20. If Cells(RowVar, ColVar).Formula = "Angstrom" Then 'This is where your search text goes
  21. Cells(RowVar, ColVar).Select
  22. found = True
  23. End If
  24. End If
  25. Next
  26. Next
  27. End Sub

Happy coding

Yomet
Reply With Quote Quick reply to this message  
Join Date: Nov 2005
Posts: 134
Reputation: Yomet is an unknown quantity at this point 
Solved Threads: 10
Yomet Yomet is offline Offline
Junior Poster

Re: Excel Search Macro Help

 
0
  #3
Mar 3rd, 2006
A slightly more elegant way would be
Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
  1. Public Sub Test2()
  2. Dim c
  3.  
  4. For Each c In ActiveSheet.Range("A1:J20")
  5. If c.Value = "Angstrom" Then 'This is where your search text goes
  6. c.Select
  7. Exit For
  8. End If
  9. Next
  10. End Sub

Yomet
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 195
Reputation: HI2Japan is an unknown quantity at this point 
Solved Threads: 21
HI2Japan HI2Japan is offline Offline
Junior Poster

Re: Excel Search Macro Help

 
0
  #4
Mar 3rd, 2006
Thank you for the replies, I will try them out at work on Monday and let you know how it goes.
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 195
Reputation: HI2Japan is an unknown quantity at this point 
Solved Threads: 21
HI2Japan HI2Japan is offline Offline
Junior Poster

Re: Excel Search Macro Help

 
0
  #5
Mar 5th, 2006
Yomet,

Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
  1. Public Sub Test2()
  2. Dim c
  3.  
  4. For Each c In ActiveSheet.Range("A1:J300")
  5. If c.Value = Range("A1") Then 'This is where your search text goes
  6. c.Select
  7. Exit For
  8. End If
  9. Next
  10. End Sub
I tried changing what you gave me to make it work by putting in the Range("A1"). The problem is, and I know it is doing what is programmed, It only selects cell A1. What I really want it to do, is search the worksheet based upon the value in cell A1. Cell A1 will change. I made a drop down list in cell A1 and I want the search to find whatever value the user selects from the list.

How do I modify this to make is search based off of cell A1?

Thank you in advance

Paul
Last edited by Comatose; Mar 5th, 2006 at 9:42 pm. Reason: Code Tags
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 195
Reputation: HI2Japan is an unknown quantity at this point 
Solved Threads: 21
HI2Japan HI2Japan is offline Offline
Junior Poster

Re: Excel Search Macro Help

 
0
  #6
Mar 5th, 2006
Nevermind, I got it. :surprised Thank you for all your help

Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
  1. Sub TESTING2()
  2. Cells.Find(What:=Range("A1"), After:=ActiveCell, LookIn:=xlFormulas, LookAt _
  3. :=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
  4. False, SearchFormat:=False).Activate
  5. Cells.FindNext(After:=ActiveCell).Activate
  6. End Sub
Last edited by Comatose; Mar 5th, 2006 at 9:43 pm. Reason: Yeah Know, Code Tags
Reply With Quote Quick reply to this message  
Join Date: Nov 2005
Posts: 134
Reputation: Yomet is an unknown quantity at this point 
Solved Threads: 10
Yomet Yomet is offline Offline
Junior Poster

Re: Excel Search Macro Help

 
0
  #7
Mar 5th, 2006
Happy to hear you found another way, by using the Macro recorder it looks like - good going!!

Anyway, just to clear up your problems with my code:
The range you sould have put in the line
For Each c In ActiveSheet.Range("A1:J300")
is the range you want to search, not the range A1 that contains the value you are looking for. If you put
Range("A1:A1")
then, of course, it will select A1 since it's the only cell in the search range.
The cell A1 will be selected because the If statement
If c.Value = Range("A1") Then
Will of course be true since it will read (substituting c for the range A1)
If A1.Value - Range("A1") Then

Just so you know.

Happy coding

Yomet
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Visual Basic 4 / 5 / 6 Forum


Views: 22052 | Replies: 6
Thread Tools Search this Thread



Tag cloud for Visual Basic 4 / 5 / 6
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2010 DaniWeb® LLC