473,699 Members | 2,127 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

VBA - Excel FindNext Input box

8 New Member
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 10319
Poudda
8 New Member
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 Recognized Expert Top Contributor
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 New Member
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 Recognized Expert Top Contributor
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 Recognized Expert Top Contributor
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 New Member
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 Recognized Expert Top Contributor
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 New Member
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 Recognized Expert Top Contributor
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

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

Similar topics

4
16331
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. RstTicket.FindFirst " = " & Me!TicketID If RstTicket.NoMatch = False Then <<Some Code>> CheckForAnotherTicket: RstTicket.FindNext " = " & Me!TicketID
20
6503
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 open, an instance of Excel remains in the background. Then even if I close the app, it remains there. What am I suppose to do, in my DLL, to completely release Excel. If I use oExcel.Quit() then I shut down Excel... TIA
0
3726
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): -------------------------- Run-time error '1004': Method of 'Worksheets' of object '_Global' failed -------------------------- When I hit Debug, it highlights the code marked below:
13
6955
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 Worksheet Compare Previous Year Worksheet with Current Year Worksheet and if SSN exists in Previous Year Worksheet & Not in Current Year Worksheet - Delete this Row out of Previous Year Worksheet - THIS IS WHERE I'M HAVING TROUBLE. This is the...
1
6715
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 FindNext, so I hope this will be helpful! 'This is the counter to help with FindNext Dim vblCnt As Integer Private Sub btnSearch_Click() On Error GoTo Err_btnSearch_Click Dim vblSearch As String Dim vblRSC As Recordset
3
4741
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 GetOpenFileName Lib "comdlg32.dll" Alias _ "GetOpenFileNameA" (pOpenfilename As OPENFILENAME) As Long Private Type OPENFILENAME lStructSize As Long hwndOwner As Long hInstance As Long lpstrFilter As String
6
1990
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 that if there are 2 or 3 consecutive NO's in column O then this code inserts 2 or 3 rows above the first "NO" and doesn't insert a row inbetween each "NO" NB the data range will change - so I will have to use some sort of search function to locate...
0
1006
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 then fill the found cells with some text. This is OK. I then have option2 where I am trying to put text in found cells that are only selected in my listbox1 this is set for multiselect. The option2 does not work. Can anyone check this out please....
1
3053
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 not export line excelsheet.Cells(9, 2) = rsschedulesrecords.Fields(2).Value. Below is my code. Please help? Private Sub CreateDailyRoster(rsschedulesrecords As DAO.Recordset) Dim excelapp As New Excel.Application Dim excelfile As...
0
8706
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8630
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9055
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
7786
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6550
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5889
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4391
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
2364
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2016
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.