My Assistant
![]() ![]() |
|
|
Feb 14 2012, 02:10 PM
Post
#1
|
|
|
UtterAccess Ruler Posts: 4,238 From: Downey, CA |
I have a macro that selects a range in column a and b, and searches for a string of characters - in the example below, it's searching for sales how do I alter this code so I don't have to change the coding, and also is there a way to handle if nothing is found ?
Application.Goto Reference:="R1C1:R55C2" Selection.Find(What:="sales", After:=ActiveCell, LookIn:=xlFormulas, LookAt _ :=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _ False, SearchFormat:=False).Activate |
|
|
|
Feb 14 2012, 02:13 PM
Post
#2
|
|
|
UtterAccess Certified! Posts: 6,942 From: Arizona, United States |
QUOTE how do I alter this code so I don't have to change the coding dim strSearch as String strSearch =InputBox("Enter the search term") CODE Application.Goto Reference:="R1C1:R55C2" Selection.Find(What:=strSearch, After:=ActiveCell, LookIn:=xlFormulas, LookAt _ :=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _ False, SearchFormat:=False).Activate I believe this will work. |
|
|
|
Feb 14 2012, 02:20 PM
Post
#3
|
|
|
UtterAccess Certified! Posts: 6,942 From: Arizona, United States |
also bob recently had a post with a suggestion here, in regard to error handling on Find, may want to check it out it may help
|
|
|
|
Feb 14 2012, 02:35 PM
Post
#4
|
|
|
UtterAccess Ruler Posts: 4,238 From: Downey, CA |
Isaac,
that worked GREAT ! Thanks (IMG:style_emoticons/default/cool.gif) I checked out that link as well thanks again !!!!!! Steve |
|
|
|
Feb 14 2012, 02:41 PM
Post
#5
|
|
|
UtterAccess Ruler Posts: 4,238 From: Downey, CA |
hmm - I tried running the query from a button on the quick access toolbar, and it's not working !
(IMG:style_emoticons/default/confused.gif) if I run it from a code window, it works fine - very odd (IMG:style_emoticons/default/confused.gif) |
|
|
|
Feb 14 2012, 03:04 PM
Post
#6
|
|
|
UtterAccess VIP Posts: 4,297 |
Steve
If you use Selection in code called from a command button, there's a good chance that selection is actually the command button. CODE dim strSearch as String
strSearch =InputBox("Enter the search term") Set rngFnd = Range("A1:B55").Find(What:=strSearch, After:=Range("A1:B55"), LookIn:=xlFormulas, LookAt _ :=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _ False, SearchFormat:=False) If Not rngFnd Is Nothing Then Applcation.Goto rngFnd ' any other code End If |
|
|
|
Feb 14 2012, 03:42 PM
Post
#7
|
|
|
UtterAccess Certified! Posts: 6,942 From: Arizona, United States |
Thanks Norie. I have benefitted greatly from your advice about the use of Select and Selection, but sometimes I get lazy about mentioning it each time.
|
|
|
|
Feb 14 2012, 04:21 PM
Post
#8
|
|
|
UtterAccess Ruler Posts: 4,238 From: Downey, CA |
If you use Selection in code called from a command button, there's a good chance that selection is actually the command button. not sure I understand. CODE dim strSearch as String strSearch =InputBox("Enter the search term") Set rngFnd = Range("A1:B55").Find(What:=strSearch, After:=Range("A1:B55"), LookIn:=xlFormulas, LookAt _ :=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _ False, SearchFormat:=False) If Not rngFnd Is Nothing Then Applcation.Goto rngFnd ' any other code End If didn't work - you left off the .Activate but that wasn't the issue - it didn't run either way - and I didn't use the button |
|
|
|
Feb 14 2012, 04:24 PM
Post
#9
|
|
|
UtterAccess Certified! Posts: 6,942 From: Arizona, United States |
What happens if you change
After:=Range("A1:B55") to After:=Range("A1") |
|
|
|
Feb 14 2012, 04:29 PM
Post
#10
|
|
|
UtterAccess VIP Posts: 4,297 |
My mistake, it should be After:=Range("A1").
What I mean is that the command button is selected, so when you have Selection.Find, Selection is the command button. Pretty sure you can't search a command button, or maybe you can.(IMG:style_emoticons/default/dazed.gif) The reason I left Activate off was because if the find doesn't work there's nothing to activate, so the code will fail. The following If checks if the Find has worked and if it has move to the found range/cell using Application.Goto. |
|
|
|
Feb 14 2012, 04:33 PM
Post
#11
|
|
|
UtterAccess Ruler Posts: 4,238 From: Downey, CA |
Norie,
Nope - didn't work CODE Dim strSearch As String
strSearch = InputBox("Enter the search term") Set rngFnd = Range("A1:B55").Find(What:=strSearch, After:=Range("A1"), LookIn:=xlFormulas, LookAt _ :=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _ False, SearchFormat:=False).Activate If Not rngFnd Is Nothing Then Applcation.Goto rngFnd ' any other code End If |
|
|
|
Feb 14 2012, 04:37 PM
Post
#12
|
|
|
UtterAccess Ruler Posts: 4,238 From: Downey, CA |
QUOTE What I mean is that the command button is selected, so when you have Selection.Find, Selection is the command button. I'm not following this. |
|
|
|
Feb 14 2012, 04:38 PM
Post
#13
|
|
|
UtterAccess VIP Posts: 4,297 |
Why did you add Activate back in?
There's another mistake in the code though, it should be Application not Applcation. PS Don't you have Intellisense here?(IMG:style_emoticons/default/smile.gif) |
|
|
|
Feb 14 2012, 04:45 PM
Post
#14
|
|
|
UtterAccess Ruler Posts: 4,238 From: Downey, CA |
I took activate back out - and I fixed the typo - I should've used intellisense (IMG:style_emoticons/default/shocked.gif)
Steve |
|
|
|
Feb 14 2012, 04:55 PM
Post
#15
|
|
|
UtterAccess VIP Posts: 4,297 |
Steve
No problem. About the Selection thing, in Excel Selection could be anything, within reason, including a button. So when you use Selection in the code it could be referring to the button, not the range you want to search. I don't know how you can empirically test it but try playing about with TypeName(Selection). Make any sense now.(IMG:style_emoticons/default/smile.gif) |
|
|
|
![]() ![]() |
|
Go to Top · Lo-Fi Version | Time is now: 25th May 2013 - 11:10 AM |