Hii,
I want a macro that will work something like a filter i.e. it should ask the user to first enter the keyword he wants to search then he should be able to specify the column. This much code i have with me which i have given in the workbook attached. What i want more is to be able to search some other keyword which may be in the same column or other. It would be better if it asks which column. This is similar to using filter on multiple columns the only difference is after every condition is given the rows which meet the criteria should be pasted on a new sheet.
I have some idea for the code as when it is run it pops up an input box which will have 2 input boxes one for keyword & other for column. Also it should have a "next" button for more conditions & an "end" button to specify that all the conditions have been given. I have attached a sheet for reference which has the macro i already have.
Thanx in advance
Copy rows to other sheet based on multiple conditions in multiple columns
-
-
-
Re: Copy rows to other sheet based on multiple conditions in multiple columns
Will this do?
Code
Display MoreSub SelectnCopy() Dim SearchMe, StrtSht, MyCln, ShtNme, ShtNmeShrt, m As String Dim SrchCln, i, x, LoopMe, MyLoop, SplitMe, MyNme As Integer Dim SrchRnge As Range Dim MySplit As Variant MyStart: ShtNme = InputBox("Enter search term") If ShtNme = "" Then Exit Sub MySplit = Split(ShtNme, " ") 'splits sheetname into each search term (this will fail to work if there are spaces in the terms being searched for) MyCln = InputBox("Enter column") If MyCln = "" Then Exit Sub Application.ScreenUpdating = False SrchCln = Range(MyCln & 1).Column ' convert column letter to column number StrtSht = ActiveSheet.Name 'to allow return to this sheet after adding new sheet (add new sheet defaults with activation of the new sheet) Set SrchRnge = Range(Cells(2, SrchCln).Address, Range(Cells(Rows.Count, SrchCln).Address).End(xlUp).Address) 'defines the range to compare the string entered in the inputbox ShtNmeShrt = "" For MyNme = 0 To UBound(MySplit) m = Left(MySplit(MyNme), 3) ShtNmeShrt = ShtNmeShrt & m & "_" Next MyNme ShtNmeShrt = Left(ShtNmeShrt, Len(ShtNmeShrt) - 1) For i = 1 To Sheets.Count 'determine if sheetname already exists, use of Lcase allows case insensitive string comparison (if you want to force case sensitive remove all Lcase) If LCase(Sheets(i).Name) = LCase(ShtNmeShrt) Then GoTo ShtExists ' skips add new sheet if sheet already exists Next i Sheets.Add.Name = ShtNmeShrt 'adds a sheet if not already existing ShtExists: Sheets(StrtSht).Activate Range("A1").EntireRow.Copy (Sheets(ShtNmeShrt).Range("A1")) For SplitMe = 0 To UBound(MySplit) 'nested loop to search terms and copy to new sheet For x = 1 To SrchRnge.Rows.Count If LCase(Range(Cells(x, SrchCln).Address)) = LCase(MySplit(SplitMe)) Then Range(Cells(x, SrchCln).Address).EntireRow.Copy (Sheets(ShtNmeShrt).Range("A" & Rows.Count).End(xlUp).Offset(1, 0)) End If Next x Next SplitMe Application.DisplayAlerts = False If Sheets(ShtNmeShrt).UsedRange.Rows.Count = 1 Then Sheets(ShtNmeShrt).Delete MsgBox "No matches found" GoTo MyEnd End If MsgBox "Sheet " & "[" & ShtNmeShrt & "]" & " has been created" MyEnd: Application.DisplayAlerts = True Application.ScreenUpdating = True If MsgBox("Would you like to search another column?", vbYesNo) = vbYes Then GoTo MyStart End Sub
-
Re: Copy rows to other sheet based on multiple conditions in multiple columns
Hii Justin,
Thanx 4 responding bt i want a code similar to a filter on multiple columns i.e. referring to the attached sheet if i find Apple in column "C" then i should also be able to search between these Apple rows for other keyword e.g. season "Winter" in column "B". Hence all the rows which contain Apple as well as Winter must be pasted in the other sheet -
Re: Copy rows to other sheet based on multiple conditions in multiple columns
Excel 2010 Tables
Filter on multiple columns.
Create new Table.
With macro.
http://c3017412.r12.cf0.rackcdn.com/05_03_11.xlsm
If you get *.zip, don't unzip, just rename *.xlsm
Participate now!
Don’t have an account yet? Register yourself now and be a part of our community!