lookup, single criteria, multiple sheets, multiple results.

nitesh123

New Member
Joined
Nov 7, 2012
Messages
9
someone please put me out of my misery. i have finally broken down after 5 hours of struggle and decided to ask for help.

this is the situation.

sheet 1

Casedatenameproductprice
A10-octjohnmobile1000
B15-octmarktv1500

<tbody>
</tbody>

sheet 2

casedatenamecategorypurpose
A16-octanthonyphonerepair
D30-octmathewlaptopfix
E1-novsomeonesomethingsomething

<tbody>
</tbody>

i will have multiple sheets like this, about 10 in total each having different columns apart from the first 3.
I need to create a macro so that when i enter a case letter (eg A) in a cell and run the macro, it gives me this result in a new sheet.

sheet 3

case
datename
A10-octjohn
A16-octanthony

<tbody>
</tbody>

i need only the columns with same headings to be copied. i dont care for the rest. the arrangement of the first 3 columns will be same in all sheets..only the last few columns will be different. but that shouldnt be a problem right? because i dont need those last few different columns.

Im still learning vb so new to the codes, if someone could please write for me a simple to understand code that i can modify.

Thanks.
 

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
Welcome to the board.
Maybe this will get you started...
Code:
Sub GetCases()
    Dim sCaseID As String
    Dim i As Integer, r As Integer
    Dim fnd As Range, fnd1 As String
    Dim arSource As Variant
    Dim shSource As Worksheet
    Dim rgSource As Range
    Dim shDestin As Worksheet
    
    arSource = Array("Sheet1", "Sheet2") 'add Source sheets
    
    sCaseID = InputBox("Enter Case ID")
    
    r = 1
    Set shDestin = Sheets.Add(Before:=Worksheets(1))
    shDestin.Name = """" & sCaseID & """" & " on " & Format(Now, "mm.dd.yy @ hh.mm.ss")
    shDestin.Cells(r, 1) = "Case"
    shDestin.Cells(r, 2) = "Date"
    shDestin.Cells(r, 3) = "Name"
    
    For i = 0 To UBound(arSource)
        Set shSource = Sheets(arSource(i))
        Set rgSource = shSource.Cells.CurrentRegion.Columns(1)
        Set fnd = Nothing
        With rgSource
            Set rgSource = .Offset(1).Resize(.Rows.Count - 1, .Columns.Count)
            Set fnd = .Find(sCaseID, , xlValues, xlWhole)
            If Not fnd Is Nothing Then
                fnd1 = fnd.Address
                Do
                    r = r + 1
                    shDestin.Cells(r, 1).Resize(1, 3) = fnd.Resize(1, 3).Value
                    Set fnd = .FindNext(fnd)
                Loop While fnd.Address <> fnd1
            End If
        End With
    Next i
    shDestin.Cells.Columns.AutoFit
    
    MsgBox """" & sCaseID & """" & " was found " & r - 1 & " times on " & UBound(arSource) + 1 & " sheets"
End Sub
 
Upvote 0
THANK YOU!!!! this works perfectly as i wanted. it just took 15 mins to customize the code. i appreciate your help. God bless you!
 
Upvote 0
hey warship...one more question. Suppose i want to sort the results that are pasted in the new sheet by date for example, from smallest to largest. is there a way to include this in the macro so i dont have to manually place the filter and sort each time?
 
Upvote 0
You're very welcome.

Add this below: shDestin.Cells.Columns.AutoFit
This sorts by Date then within each date by Name

Code:
    shDestin.Sort.SortFields.Clear
    shDestin.Sort.SortFields.Add Key:=Range("B:B"), Order:=xlAscending
    shDestin.Sort.SortFields.Add Key:=Range("C:C"), Order:=xlAscending
    With shDestin.Sort
        .SetRange shDestin.Cells.CurrentRegion
        .Header = xlYes
        .Apply
    End With
 
Upvote 0
Hi Warship,

How would I adapt this code if there was more than one result in each sheet and I wanted them all?

Regards

Paul
 
Upvote 0

Forum statistics

Threads
1,221,418
Messages
6,159,790
Members
451,589
Latest member
Harold14

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top