vba to select entries from a table and copy into a new table

cjcass

Well-known Member
Joined
Oct 27, 2011
Messages
683
Office Version
  1. 2016
Platform
  1. Windows
Hi,
I'm looking for some code that will select entries in a table based on the criteria in two cells and then copy the selected entries into another table. The two tables are on different sheets. Hopefully the pic below is fairly self-explanatory. So, in the example, the yellow cells in the 'Results' wksht are the criteria (which will vary) and when a different question is selected from a dropdown list in cell C20 (Criteria 2) I would like the vba to execute - it will take the two criteria, go and find the relevant information in the table in the 'Comments' wksheet and then paste them into cells C25 down. The table columns will vary in their number of entries/rows populated. The questions will be fixed in position from B4:Z4.
Hope you can help?
33dxr47.jpg
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
Do you definitely want VBA? With the data as presented, it's possible to do this using (a little complex) formulas like this:


Book1
BCDEFG
2Which fruit do you like best?Which cars do you like best?Which ### do you like?
3Department 1I like apples and pearsDepartment 7BentleyDepartment 4###1
4Department 2PearsDepartment 10Ford and RenaultDepartment 1###2
5Department 3I like orangesDepartment 3JaguarDepartment 2###3
6Department 4I don't like anythingDepartment 4PeugeotDepartment 3###4
7Department 1Date and plumsDepartment 1Fiats and RollersDepartment 4###5
8Department 2ApplesDepartment 2VansDepartment 1###6
9Department 3Bananas and mangoDepartment 3###7
10Department 4Pears
11Department 4Oranges and Lemons
Comments



Book1
BC
2Criteria 1:Department 4
3Criteria 2:Which fruit do you like best?
4
5Result:I don't like anything
6Pears
7Oranges and Lemons
Results
Cell Formulas
RangeFormula
C5{=IFERROR(INDEX(INDEX(Comments!$B$2:$G$11,,MATCH($C$3,Comments!$B$2:$F$2,0)+1),SMALL(IF(INDEX(Comments!$B$2:$G$11,,MATCH($C$3,Comments!$B$2:$F$2,0))=$C$2,ROW(INDEX(Comments!$B$2:$G$11,,MATCH($C$3,Comments!$B$2:$F$2,0)))-ROW(Comments!$B$2)+1),ROWS($C$5:$C5))),"")}
Press CTRL+SHIFT+ENTER to enter array formulas.


WBD
 
Upvote 0
Hi WBD, thanks for the initial solution but I'm really looking for VBA as the tables could ultimately be very big (up to 50,000 rows) so I really don't want to use large array formulas, thanks.
 
Upvote 0
Hey,

OK. You'll need to change the values at the top of the code to match the cells/rows where the data resides but give this a shot:

Code:
' Change these constants as appropriate
Private Const DepartmentCriteriaCell = "C2"
Private Const QuestionCriteriaCell = "C3"
Private Const ResultsCell = "C5"
Private Const QuestionRow = "2:2"
Public Sub GetCriteriaResults()

Dim commentsSheet As Worksheet
Dim resultsSheet As Worksheet
Dim lastRow As Long
Dim thisRow As Long
Dim qColumn As Variant
Dim criteriaDepartment As String
Dim criteriaQuestion As String
Dim resultCount As Long

' Turn off screen updating
Application.ScreenUpdating = False

' Set up the sheets
Set commentsSheet = Sheets("Comments")
Set resultsSheet = Sheets("Results")

' Clear out the results
lastRow = resultsSheet.Cells(resultsSheet.Rows.Count, resultsSheet.Range(ResultsCell).Column).End(xlUp).Row
If lastRow > resultsSheet.Range(ResultsCell).Row Then
    resultsSheet.Range(resultsSheet.Range(ResultsCell), resultsSheet.Cells(lastRow, resultsSheet.Range(ResultsCell).Column)).ClearContents
End If

' Get the criteria
criteriaDepartment = resultsSheet.Range(DepartmentCriteriaCell).Value
criteriaQuestion = resultsSheet.Range(QuestionCriteriaCell).Value

' Find the question column
qColumn = Application.Match(criteriaQuestion, commentsSheet.Range(QuestionRow), 0) ' Specify the row with the questions on it

' Quit out if we didn't find it
If IsError(qColumn) Then
    MsgBox "Unable to find the question in the Comments sheet", vbExclamation
    Application.ScreenUpdating = True
    Exit Sub
End If

' Find the last row in this column
lastRow = commentsSheet.Cells(commentsSheet.Rows.Count, qColumn).End(xlUp).Row

' Now look for the appropriate answers
resultCount = 0
For thisRow = commentsSheet.Range(QuestionRow).Row + 1 To lastRow
    If commentsSheet.Cells(thisRow, qColumn).Value = criteriaDepartment Then
        resultsSheet.Range(ResultsCell).Offset(resultCount, 0).Value = commentsSheet.Cells(thisRow, qColumn + 1).Value
        resultCount = resultCount + 1
    End If
Next thisRow

' Turn on screen updating again
Application.ScreenUpdating = True

End Sub

WBD
 
Upvote 0
Works brilliantly!!
Thanks again WBD - a great help :)
 
Upvote 0
Hi WBD (if you're around),
I messed up yesterday when I came back and said it worked brilliantly, stupidly I still had your array formulas in the Results column so the results were displaying anyway!!
When I removed the array formulas it stopped working, so as it stands at the moment the code doesn't appear to be working - no results are coming through when cell C3 is changed from the dropdown list in the Results tab.
The values at the top of the code match the cells/rows where the data resides, so that's not the issue.
Any thoughts?
Rgds,
 
Upvote 0

Forum statistics

Threads
1,223,910
Messages
6,175,316
Members
452,634
Latest member
cpostell

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