Is this too difficult or too basic to complete

Opus

New Member
Joined
Mar 31, 2010
Messages
33
Can anyone help I am new to VBA? Below is a basic way to find rows that match 3 criteria, work out the average to each column G to M of all the rows that match the criteria and copy and past to a repot sheet.<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p>
<o:p></o:p>
How can I find or match to multiple criteria
Code:
[COLOR=blue][FONT=Verdana]Private Sub[/FONT][/COLOR][COLOR=black][FONT=Verdana] Average()
[/FONT][/COLOR][COLOR=darkgreen][FONT=Verdana]'
[/FONT][/COLOR][COLOR=black][FONT=Verdana]    Application.ScreenUpdating = [/FONT][/COLOR][COLOR=blue][FONT=Verdana]False[/FONT][/COLOR][COLOR=black][FONT=Verdana]
    Call Openreport
    Sheets("Data").Select
    
   [/FONT][/COLOR][COLOR=darkgreen][FONT=Verdana] 'Needs to be variable range
[/FONT][/COLOR][COLOR=black][FONT=Verdana]    Range("A8:P1413").Select
    Range(Selection, Selection.End(xlDown)).Select
    
    [/FONT][/COLOR][COLOR=darkgreen][FONT=Verdana]'Copy and paste main data for manipulation
[/FONT][/COLOR][COLOR=black][FONT=Verdana]    Selection.copy
    Sheets("AV").Select
    Range("A2").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=[/FONT][/COLOR][COLOR=blue][FONT=Verdana]False[/FONT][/COLOR][COLOR=black][FONT=Verdana], Transpose:=[/FONT][/COLOR][COLOR=blue][FONT=Verdana]False[/FONT][/COLOR][COLOR=black][FONT=Verdana]
    
   [/FONT][/COLOR][COLOR=darkgreen][FONT=Verdana] 'Sort columns by Catagory of Company then by Company Name and then by region scoring
[/FONT][/COLOR][COLOR=black][FONT=Verdana]    Selection.Sort Key1:=Range("F2"), Order1:=xlAscending, _
                    Key2:=Range("E2"), Order2:=xlAscending, _
                    Key3:=Range("C2"), Order3:=xlAscending, _
                    Header:=xlNo, _
                    OrderCustom:=1, _
                    MatchCase:=[/FONT][/COLOR][COLOR=blue][FONT=Verdana]False[/FONT][/COLOR][COLOR=black][FONT=Verdana], _
                    Orientation:=xlTopToBottom, _
                    DataOption1:=xlSortNormal, _
                    DataOption2:=xlSortNormal, _
                    DataOption3:=xlSortNormal
                    <o:p></o:p>[/FONT][/COLOR]
[COLOR=black][FONT=Verdana]    Range(ActiveCell, ActiveCell.Offset(Range(0), 5)).Select
            Selection.copy
            Range("A1").Select
            Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
                :=[/FONT][/COLOR][COLOR=blue][FONT=Verdana]False[/FONT][/COLOR][COLOR=black][FONT=Verdana], Transpose:=[/FONT][/COLOR][COLOR=blue][FONT=Verdana]False
[/FONT][/COLOR][COLOR=darkgreen][FONT=Verdana]    '
    '1 copy paste row 2 to row 1 in sheet "AV"
    '2 Find first row that matches F1,E1,C1 after row 1
    '3 If match found, copy row 2 to sheet "AVE" to the first empty row
    '4 delete row 2 in sheet "AV"
    '5 loop stage 1
    '6 If no match found
    '7 copy  row 1 in sheet "AVE" (range G1:M1 sum average down on   sheet to give total average score)
    'to first empty row in sheet "AVER"
    '8 delete rows not empty after row 3 in sheet "AVE"
    '9 Loop stage 1 until row 2 empty
[/FONT][/COLOR][COLOR=yellowgreen][FONT=Verdana]    '
[/FONT][/COLOR][COLOR=black][FONT=Verdana]    
    [/FONT][/COLOR][COLOR=blue][FONT=Verdana]Do Until[/FONT][/COLOR][COLOR=black][FONT=Verdana] IsEmpty(ActiveCell)
         
            
                
[/FONT][/COLOR][COLOR=green][FONT=Verdana]            'Need find all maybe
            'Needs to be veriable range
[/FONT][/COLOR][COLOR=red][FONT=Verdana]            Range("A2:F?").Find(what:=Cells(F1, E1, C1), LookAt:=xlWhole, _
                                                    LookIn:=xlValues, _
[/FONT][/COLOR][COLOR=black][FONT=Verdana]                                                    searchorder:=xlByColumns).Activate
        [/FONT][/COLOR][COLOR=darkgreen][FONT=Verdana]'If Match found Then
[/FONT][/COLOR][COLOR=black][FONT=Verdana]            ActiveCell.EntireRow.Select
            Selection.copy
            Sheets("AVE").Select
            [/FONT][/COLOR][COLOR=navy][FONT=Verdana]Do Until[/FONT][/COLOR][COLOR=black][FONT=Verdana] IsEmpty(ActiveCell)
                Selection.Offset(1, 0).Select
            [/FONT][/COLOR][COLOR=navy][FONT=Verdana]Loop[/FONT][/COLOR][COLOR=black][FONT=Verdana]
            Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
                :=False, Transpose:=[/FONT][/COLOR][COLOR=blue][FONT=Verdana]False[/FONT][/COLOR][COLOR=black][FONT=Verdana]
            Sheets("AV").Select
            ActiveCell.EntireRow.Delete
            <o:p></o:p>[/FONT][/COLOR]
[COLOR=black][FONT=Verdana]            [/FONT][/COLOR][COLOR=darkgreen][FONT=Verdana]'Else[/FONT][/COLOR][COLOR=black][FONT=Verdana]
                [/FONT][/COLOR][COLOR=blue][FONT=Verdana]If [/FONT][/COLOR][COLOR=black][FONT=Verdana]IsEmpty(ActiveCell) Then
                    [/FONT][/COLOR][COLOR=blue][FONT=Verdana]Call[/FONT][/COLOR][COLOR=black][FONT=Verdana] AV_Report
            
                [/FONT][/COLOR][COLOR=blue][FONT=Verdana]Else[/FONT][/COLOR][COLOR=black][FONT=Verdana]
                Sheets("AVE").Select
                Range("A1").Select
                ActiveCell.EntireRow.Select
                Selection.copy
                Sheets("AVER").Select
                    [/FONT][/COLOR][COLOR=navy][FONT=Verdana]Do Until[/FONT][/COLOR][COLOR=black][FONT=Verdana] IsEmpty(ActiveCell)
                        Selection.Offset(1, 0).Select
                    [/FONT][/COLOR][COLOR=blue][FONT=Verdana]Loop
[/FONT][/COLOR][COLOR=black][FONT=Verdana]                    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
                        :=[/FONT][/COLOR][COLOR=blue][FONT=Verdana]False[/FONT][/COLOR][COLOR=black][FONT=Verdana], Transpose:=[/FONT][/COLOR][COLOR=blue][FONT=Verdana]False[/FONT][/COLOR][COLOR=black][FONT=Verdana]
                        
                Sheets("AV").Select
                Range("A2").Select
                Range(ActiveCell, ActiveCell.Offset(Range(0), 5)).Select
                Selection.copy
                Range("A1").Select
                Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
                :=[/FONT][/COLOR][COLOR=blue][FONT=Verdana]False[/FONT][/COLOR][COLOR=black][FONT=Verdana], Transpose:=[/FONT][/COLOR][COLOR=blue][FONT=Verdana]False
[/FONT][/COLOR][COLOR=black][FONT=Verdana]                
                [/FONT][/COLOR][COLOR=navy][FONT=Verdana]End If
[/FONT][/COLOR][COLOR=black][FONT=Verdana]                        [/FONT][/COLOR][COLOR=darkgreen][FONT=Verdana]'End If[/FONT][/COLOR][COLOR=black][FONT=Verdana]
    [/FONT][/COLOR][COLOR=navy][FONT=Verdana]  Loop[/FONT][/COLOR][COLOR=black][FONT=Verdana]
    
    
   
    [/FONT][/COLOR][COLOR=blue][FONT=Verdana]Call[/FONT][/COLOR][COLOR=black][FONT=Verdana] Closereport
    Application.ScreenUpdating = [/FONT][/COLOR][COLOR=blue][FONT=Verdana]True[/FONT][/COLOR][COLOR=black][FONT=Verdana]
[/FONT][/COLOR][COLOR=blue][FONT=Verdana]End Sub[/FONT][/COLOR][COLOR=black][FONT=Verdana]<o:p></o:p>[/FONT][/COLOR]
 

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
A couple of things. First it's rarely necessary to Select in VBA. You can work with ranges directly.

However, why do you need VBA for this? Why not a formula on your report sheet?
 
Upvote 0
This is a kpi data base for 23 regions scoring for between 45 and 60 companies, the regions have scored two times now but each region may have just started to use a company so for that region there will be only one score for that company ( each company may have two or more products to score so there name will show in the company column twice or thrice, the data base so fare is 2600 rows long and a sum for this many would slow the book too much.

Also I am new to VBA and I so far only copy and paste then test.
 
Upvote 0
Sorry also I need to calculate the avereage of the two scores from each region for each company whether thay have one score or two Hotpepper.
Any suggestions as I need this by Tuesday or I'm sacked.
 
Upvote 0
A simpler approach might be to create a new column at the end of the data, then run through line by line and simply tag the lines that fill the 3 criteria - you could maybe even use a formula for this.

Then you could run a filter based on this column and then copy or cut the visible rows (after checking there are any results) and then paste them to the new sheet in one go.

HTH
 
Upvote 0

Forum statistics

Threads
1,223,247
Messages
6,171,004
Members
452,374
Latest member
keccles

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