I posted this question already (Admin Edit Need help with my search macro for excel), but I made a mistake in writing the question, so I posted a reply and I am afraid that at a glance it may be viewed as answered. Therefore, I am reposting my question correctly because I desperately need help! My boss needs this and I have tried to manipulate a code that was given to me on this site, but I am not versed in macros. My question is how to search multiple columns of data. I have a month in column B, a year in column C, and a person's name in column D. Below I have included the search macro I have that searches the one column, but I need to be able to search all three columns. I know how to make three input boxes to put the information in that I am searching for, but I don't know how to manipulate this code to search more than one column. Please, please, please help me with this because I am about to go crazy struggling to figure this out. Thank you so much in advance! - Sub TextBoxSearch_Click()
-
Dim searchMon As String
-
-
searchMon = InputBox("Enter Entire Month ex:January")
-
If searchMon = "" Then Exit Sub
-
Sheets("Archive").Select
-
ActiveSheet.Unprotect
-
Range("DZ1").Value = searchMon
-
Range("DZ2").Activate
-
For i = 1 To 120
-
ActiveCell.Value = "=vlookup(DZ1,B4:DQ53," & i & ",FALSE)"
-
ActiveCell.Offset(rowOffset:=0, columnOffset:=1).Activate
-
Next i
-
-
Range("A1").Select
-
ActiveSheet.Protect
-
Sheets("Searched Report").Select
-
Range("A1").Select
-
-
End Sub
The copy function works great, but is there a way to make three input boxes, or do I just have to type the month, year, and tech's name all in the one box? Sorry that I made this more complicated than it was. Sometimes we get focused on looking for one answer that we don't see the more simply answer right in front of our face.
Hi
I'm not sure this will help, but if we are using code (in Excel!) rather that worksheet function, then I would tend to write my own code/functions.
I you know the columns that the particular data is in then perhaps somethin like this or similar may be of interest - Sub FindRow(ByRef iRow As Long)
-
Const cMonthCol As Integer = 2
-
Const cYearCol As Integer = 3
-
Const cTechCol As Integer = 4
-
Const cStartRow As Long = 4
-
-
Dim i As Long
-
Dim strMonth As String
-
Dim strYear As String
-
Dim strTech As String
-
-
iRow = 0
-
-
strMonth = InputBox("Enter Month")
-
If strMonth = "" Then Exit Sub
-
-
strYear = InputBox("Enter Year")
-
If strYear = "" Then Exit Sub
-
-
strTech = InputBox("Enter Technician")
-
If strTech = "" Then Exit Sub
-
-
i = cStartRow
-
-
Do Until Cells(i, cMonthCol) = ""
-
If Trim(UCase(Cells(i, cMonthCol))) = Trim(UCase(strMonth)) And Trim(UCase(Cells(i, cYearCol))) = Trim(UCase(strYear)) And Trim(UCase(Cells(i, cTechCol))) = Trim(UCase(strTech)) Then
-
iRow = i
-
Exit Do
-
End If
-
i = i + 1
-
Loop
-
End Sub
-
-
Sub TestRowFound()
-
Dim i As Long
-
-
FindRow i
-
If i <> 0 Then
-
MsgBox "Match found in row " & i & "."
-
Else
-
MsgBox "No match found."
-
End If
-
End Sub
??
Of course there are many ways of doing this, defining the search range etc., it just depend on layout/circumstances.
MTB
12 3642 NeoPa 32,556
Expert Mod 16PB
This will work much better if you can post the meta-data (info about the layout / structure) of the table in the same way as I use in my example. Click on the Reply button and you will have access to all the codes I've used. PK & FK stand for Primary Key & Foreign Key respectively. Table Name=[tblStudent] - Field; Type; IndexInfo
-
StudentID; AutoNumber; PK
-
Family; String; FK
-
Name; String
-
University; String; FK
-
Mark; Numeric
-
LastAttendance; Date/Time
It would also help to understand what you're after if you were to provide a few examples of how you expect this to work. I'm not at all clear on how you intend to use a multi-column filter. Would you enter the search items in three separate controls on your form? If not, what do you envisage?
NeoPa 32,556
Expert Mod 16PB
Well, you can probably scratch all that as I now find that this question in the MS Access / VBA forum is actually an Excel question.
Let me rename the thread to something that makes better sense then I may have another look.
NeoPa 32,556
Expert Mod 16PB
Right, now we all understand that you're talking about an Excel issue...
You need to explain what you're trying to achieve a lot better than you have.
Please provide a few examples of how you expect this to work. Would you enter the search items separately? If not, what do you envisage?
Right, now we all understand that you're talking about an Excel issue...
You need to explain what you're trying to achieve a lot better than you have.
Please provide a few examples of how you expect this to work. Would you enter the search items separately? If not, what do you envisage?
Yes, you would enter the three searches separately. When you click on a button it would pull up one box that said enter month when you enter the month and click ok another box would open that said enter year when you enter the year and click ok a final box would open that said enter technicians name when you did so and clicked ok the search for those three things would begin. The search would result in a match on one row (where a lot of other information is located in different columns). Then that entire row would be copied to another row where I have set links to make a report. I have made the boxes happen, but I don't know how to make the search actually run. I have no real training on macros only looking at examples that others have done and trial and error from there. If there is another way to search for those three things let me know, I am open to anything. I hope this better explains what I am trying to do.
Hi. I haven't got an immediate solution for you but at least we know more about what you need. The method used for your previous search relied on the data you are searching for being ordered in ascending order - otherwise VLOOKUP would not have worked. You can't easily use a variant of that approach this time because VLOOKUP (or its column-wise counterpart HLOOKUP) are single-value comparison functions, and finding a match in a row of the first column does not necessarily mean that you are near the right row at all when there are two other values to consider.
If it wasn't for your row copy and report linkage I would simply suggest you link the Excel worksheet to Access and use an access parameter query to find the matching record for you, but this won't do your copy and link bit for you.
Excel is an excellent financial spreadsheet but a very poor database - and perhaps you might be better off convincing your boss to let you use a real database to handle such work.
-Stewart
NeoPa 32,556
Expert Mod 16PB
While showing a fairly in-depth knowledge of how VLookup() works Stewart, I think you may be mistaken about this particular call.
When Range_Lookup is set to TRUE, it is indeed true that sorted data is expected (if it's to work reliably). However, when set to FALSE it searches through the data until it finds an exact match, regardless of the order of the data.
NeoPa 32,556
Expert Mod 16PB
Yes, you would enter the three searches separately. When you click on a button it would pull up one box that said enter month when you enter the month and click ok another box would open that said enter year when you enter the year and click ok a final box would open that said enter technicians name when you did so and clicked ok the search for those three things would begin. The search would result in a match on one row (where a lot of other information is located in different columns). Then that entire row would be copied to another row where I have set links to make a report. I have made the boxes happen, but I don't know how to make the search actually run. I have no real training on macros only looking at examples that others have done and trial and error from there. If there is another way to search for those three things let me know, I am open to anything. I hope this better explains what I am trying to do.
As I'm a little the worse for wear at the moment ;), I will indicate an approach that I use when either searching or sorting on multiple column values. If this isn't clear enough I can flesh it out when I'm more compos mentis.
Produce a column at the left of your data which is a composite of the three columns you need to search. For string data this is fairly straightforward (=B2 & C2 & D2 ==> copied down column A).
Simply then, search in this column for the string built up from the three values entered.
Let me know how you get on.
Oops. Sorry for that overlook. NeoPa is indeed correct - data does not need to be sorted for it to work as long as the range indicator is set false. I do too many reference tables which are always sorted, and confused myself here.
NeoPa's suggestion of combining the three columns is good, and indeed I think it is the only practicable way to achieve the outcome and retain the spreadsheet approach.
-Stewart
...When Range_Lookup is set to TRUE, it is indeed true that sorted data is expected (if it's to work reliably). However, when set to FALSE it searches through the data until it finds an exact match, regardless of the order of the data.
The copy function works great, but is there a way to make three input boxes, or do I just have to type the month, year, and tech's name all in the one box? Sorry that I made this more complicated than it was. Sometimes we get focused on looking for one answer that we don't see the more simply answer right in front of our face.
The copy function works great, but is there a way to make three input boxes, or do I just have to type the month, year, and tech's name all in the one box? Sorry that I made this more complicated than it was. Sometimes we get focused on looking for one answer that we don't see the more simply answer right in front of our face.
Hi
I'm not sure this will help, but if we are using code (in Excel!) rather that worksheet function, then I would tend to write my own code/functions.
I you know the columns that the particular data is in then perhaps somethin like this or similar may be of interest - Sub FindRow(ByRef iRow As Long)
-
Const cMonthCol As Integer = 2
-
Const cYearCol As Integer = 3
-
Const cTechCol As Integer = 4
-
Const cStartRow As Long = 4
-
-
Dim i As Long
-
Dim strMonth As String
-
Dim strYear As String
-
Dim strTech As String
-
-
iRow = 0
-
-
strMonth = InputBox("Enter Month")
-
If strMonth = "" Then Exit Sub
-
-
strYear = InputBox("Enter Year")
-
If strYear = "" Then Exit Sub
-
-
strTech = InputBox("Enter Technician")
-
If strTech = "" Then Exit Sub
-
-
i = cStartRow
-
-
Do Until Cells(i, cMonthCol) = ""
-
If Trim(UCase(Cells(i, cMonthCol))) = Trim(UCase(strMonth)) And Trim(UCase(Cells(i, cYearCol))) = Trim(UCase(strYear)) And Trim(UCase(Cells(i, cTechCol))) = Trim(UCase(strTech)) Then
-
iRow = i
-
Exit Do
-
End If
-
i = i + 1
-
Loop
-
End Sub
-
-
Sub TestRowFound()
-
Dim i As Long
-
-
FindRow i
-
If i <> 0 Then
-
MsgBox "Match found in row " & i & "."
-
Else
-
MsgBox "No match found."
-
End If
-
End Sub
??
Of course there are many ways of doing this, defining the search range etc., it just depend on layout/circumstances.
MTB
NeoPa 32,556
Expert Mod 16PB
Interesting code Mike, but Excel search functions are highly optimised and can perform at many times the speed of VBA code checking each cell.
NeoPa 32,556
Expert Mod 16PB
The copy function works great, but is there a way to make three input boxes, or do I just have to type the month, year, and tech's name all in the one box? Sorry that I made this more complicated than it was. Sometimes we get focused on looking for one answer that we don't see the more simply answer right in front of our face.
It's certainly possible for all three to be called from the same piece of code. It's not necessary for each to be triggered by separate events (button clicks etc).
I don't know what code you're using at the moment, but if you post it I can tell you how you can combine it into a single triggered routine.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Mr. Smith |
last post by:
Hello all,
My code can successfully open, write to, format and save several
worksheets in a workbook then save it by a given name, close and quit excel.
My problem is that if I try and do it...
|
by: Mike Knight |
last post by:
(I've also posted this problem on microsoft.public.excel.programming)
I have a MS Access 2003 Database named "AS400 Fields.mdb". This
database contains links to tables on an AS400.
In MS...
|
by: maryam |
last post by:
Hi
i have some data in excel....I want to perform a series of operations on
them and output the results to excel again. I know that some stuff could be
used with VBA. however in this special case...
|
by: D. Shane Fowlkes |
last post by:
This most likely belongs in another forum but I thought I'd start here. I
have a COM Object written in VB6. The DLL will access MS Excel and use it's
Object Library to write a customized report...
|
by: Vae07 |
last post by:
Ok so here is a brief summary of my problem. I need a pop up form that submits input text box information to a pocket excel workbook upon a command botton click. Text box inputs are checked for...
|
by: jari.toukkari |
last post by:
Hi
I use a large txt file of 1,5m lines which VBA reads into memory, and
then can searches certain values out of that data. Seach could be
faster and I wonder if c or some other language could...
|
by: jcnone |
last post by:
How would you populate a 3rd party html form (web page) with data
from an Excel file or a Database file. The form has several text
input forms that I manually populate. Is it possible to automate...
|
by: l2urhwhc4u |
last post by:
Hello,
I really need help with this problem I'm facing. I'm writing a recipe guide program using VBA form. I got to the part where the user can type in an ingredient and I need the program to...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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$) {
}
...
|
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...
|
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...
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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: 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,...
|
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...
| |