473,382 Members | 1,367 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

VBA - Excel FindNext Input box

8
Hi there,

I'm trying to set up a "find / next / replace" input box, but I have no clue how to make vb do this. Currently my code has a three step process. I have an input box that asks for a variable to find. Then when it's found, it puts it all the information on a given row into a spreadsheet. This would work just fine if there was only one row with the requested info. This is the code I have for the find function:

Expand|Select|Wrap|Line Numbers
  1. LookFor = InputBox("what is the client name?", "Search")
  2. If LookFor = "" Then
  3.   End
  4. End If
  5.  
  6. Cells.Find(What:=LookFor, After:=ActiveCell, LookIn:=xlFormulas, LookAt _
  7.         :=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
  8.         False, SearchFormat:=False).Activate
Of course I got this from using a macro. I can even add a line for a find next, but I don't have the correct input box to make this work.

I'd appreciate the help!
Jun 12 '07 #1
11 10300
Poudda
8
Okay, I've made it work much better, but now if the item I'm looking for is not in my search request, the program stops working. Here is the new abbreviated code:


Expand|Select|Wrap|Line Numbers
  1. LookFor = InputBox("what is the client name?", "Search")
  2. If LookFor = "" Then
  3.   End
  4. End If
  5.  
  6. Cells.Find(What:=LookFor, After:=ActiveCell, LookIn:=xlFormulas, LookAt _
  7.         :=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
  8.         False, SearchFormat:=False).Activate
  9.  
  10. y = ActiveCell.Row
  11.  
  12.  
  13. 'bunch of code to take values from spreadsheet and put them into a form...
  14.  
  15.  
  16. Do Until z = vbYes
  17.   z = MsgBox("Is this the correct client?", vbYesNoCancel)
  18.   If z = vbNo Then
  19.     Cells.FindNext(After:=ActiveCell).Activate
  20.     y = ActiveCell.Row
  21.     'bunch of code to take values from spreadsheet and put them into a form...
  22.   End If
  23.  
  24.   If z = vbCancel Then
  25.     frmClient.txtRows.Text = fnRow
  26.     fnScrew
  27.   End If
  28.  
  29.   If z = vbYes Then
  30.     frmClient.txtName.SetFocus
  31.   End If
  32. Loop
Jun 13 '07 #2
kadghar
1,295 Expert 1GB
it wont work because the loop will continue forever, since is until you say that's the client you're looking for.

just have in mind what to do in case the user answers always no, or you just dont find the name.

Good Luck
Jun 13 '07 #3
Poudda
8
Actually, selecting "No" works just the way I want it to, it finds the next entry and adds the info from the row info to the form and then prompts the user with a "Yes/No/Cancel" question asking if it's the right client. This only loops for as long as the user chooses for it to loop.

I know my programming isn't as clean as it could be, however this program only appears to break down if the name I enter (which becomes LookFor) in the Cells.Find command isn't in the data list.

The error message I get when I enter a name that isn't there is: "Object variable or With block not set." Of course, I don't know what this means. I've seen the command "With" in some macros I've done, but I've never had to program one and am not sure how this is done within the Cells.Find line.

What I want the program to do if I enter a client name that isn't in the speadsheet, is to let me know that, "The client was not found, please enter another name." or "No client by that name found." And then clear the form and activate the cell at the bottom of the spreadseet (this part already in another module).

Thanks for your response, I hope this describes more clearly the bug I'm trying to fix.
Jun 14 '07 #4
kadghar
1,295 Expert 1GB
hi!!

I see the problem, you are always asking the code to activate the cells.find

what you must do is first check that the cells.find is different from nothing.
If the search is nothing, then put the msgbox ("sorry bla bla bal") else put the other procedure with a loop, (in this case it seemed easier to me to put a flag and reference it)

Hope this helps:

Expand|Select|Wrap|Line Numbers
  1. lookfor = InputBox("what is the client name?", "Search")
  2. If lookfor = "" Then
  3.   End
  4. End If
  5.  
  6. Flag1:
  7. If Not Cells.Find(What:=lookfor, After:=ActiveCell, LookIn:=xlFormulas, LookAt _
  8.     :=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
  9.     False, SearchFormat:=False) Is Nothing Then
  10.  
  11.   Cells.Find(What:=lookfor, After:=ActiveCell, LookIn:=xlFormulas, LookAt _
  12.       :=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
  13.       False, SearchFormat:=False).Activate
  14.  
  15.   If MsgBox("Is this the correct client?", vbYesNoCancel) <> vbYes Then
  16.     GoTo Flag1
  17.   End If
  18. Else
  19.   MsgBox ("The client is not in the list")
  20. End If
Jun 14 '07 #5
kadghar
1,295 Expert 1GB
oh, and you can replace all that macro stuf with something like:
Expand|Select|Wrap|Line Numbers
  1. if not cells.find(lookfor) is nothing then
  2. cels.find(lookfor).activate
or something like that
Jun 14 '07 #6
Poudda
8
Thank you a billion times over.

This solves the problem. I had no idea that goto commands even existed any more (last time I used one of these was on my commodore 64, back when we had to have a number for every line of code!).

The If Not.... Is Nothing Then command was also key to fix the problem.

Cheers!
Jun 14 '07 #7
kadghar
1,295 Expert 1GB
Thank you a billion times over.

This solves the problem. I had no idea that goto commands even existed any more (last time I used one of these was on my commodore 64, back when we had to have a number for every line of code!).

The If Not.... Is Nothing Then command was also key to fix the problem.

Cheers!
I'm glad it was useful

Kad
Jun 14 '07 #8
Proaccesspro
132 100+
I'm glad it was useful

Kad

Could this be used in Access??? I'm trying to do something very similar via a search and replace functrion....
Jul 12 '07 #9
kadghar
1,295 Expert 1GB
Could this be used in Access??? I'm trying to do something very similar via a search and replace functrion....

yeap, just remember that you're not using cells in acces...

I cant tell you exactly how is done since i havnt got acces at the works pc, but give it a try and if you have some trouble i'll check it out latter at home.
Jul 12 '07 #10
Proaccesspro
132 100+
yeap, just remember that you're not using cells in acces...

I cant tell you exactly how is done since i havnt got acces at the works pc, but give it a try and if you have some trouble i'll check it out latter at home.

Can you position where on the screen the search box appears??
Jul 12 '07 #11
Killer42
8,435 Expert 8TB
...I had no idea that goto commands even existed any more
Sure it exists, but the use of Goto still represents (usually) lazy coding. With proper control structures, GoTo should generally not be required.
Jul 13 '07 #12

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Tom | last post by:
Wta is the code structure to put Rst.FindNext in a loop? I'm using what I show below. It works fine but my instinct says it should be in some standard loop rather than using the GoTo structure. ...
20
by: Atchoum | last post by:
I have a DLL that opens an Excel workbook and add-in. If I close the app that calls the DLL before closing Excel, when I close Excel everything is fine. But if I close Excel while the app is still...
0
by: Jono | last post by:
Hello, I've been getting this message when closing excel (not necessarily when closing the workbook by itself, but when closing Excel and the workbook at the same time): ...
13
by: Shelley | last post by:
Compare Current Year Worksheet with Previous Year Worksheet and if SSN exists in Current Year Worksheet & Not in Previous Year - Copy this Row from Current Year Worksheet & Paste into Previous Year...
1
Corster
by: Corster | last post by:
I went through a great deal of hassle to figure this out for myself, but now it is complete, I would like to share it with the world! I know afew other people have had trouble with FindFirst and...
3
by: atrottier | last post by:
I am tring to import an Excel file directly to a table in Access 2003. The code runs but it locks up the app and I need to do a ctrl/alt/del to get out. Here is the code: Private Declare Function...
6
by: grego9 | last post by:
I am trying to write some code to get Excel 2000 to insert a blank row wherever there is a "NO" in column O (above the NO). The code below nearly does this - but not quite!. The problem I have is...
0
by: savakis | last post by:
Can onyone please help me please. I have duplicate items in column A:A which I find via combobox1. These items are placed in listbox1. I then fill in textbox1 and textbox2 for column C and D, and...
1
by: accessvbanewbie | last post by:
I would like to export a recordset from access to excel but after each record is exported I want to insert a new row. The first recordset does this ok. However, the second recordset onwards does not...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.