create list based on criteria

Lucianp

Board Regular
Joined
Apr 23, 2008
Messages
75
I want to create a short list based on the condition within a larger list.

On sheet 1, I have a list of variable length. column A holds the names that I want to copy. Column B holds a number, 1,2 or 3.

I want to copy the names from Sheet 1, column A ,if the corresponding cell in column B is equal to 1, into sheet 2 column A.

I have to copy it to a separate list as this will be the basis for a graph. This worksheet will be for use by others but they will only be able to edit the list in sheet 1.

I tried to do this several different ways but I think I'm going down the wrong track.

any ideas please.
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
Try this. I've made it in a button routine, which might be the easiest.
Code:
Private Sub CommandButton1_Click()
LastRow = Sheets("Sheet1").Range("A65536").End(xlUp).Row
    For i = 1 To LastRow
        If Range("B" & i).Value = 1 Then
            LastRow2 = Sheets("Sheet2").Range("A65536").End(xlUp).Row + 1
            Sheets("Sheet2").Range("A" & LastRow2).Value = Sheets("Sheet1").Range("A" & i)
        End If
    Next i
    
    Sheets("Sheet2").Range("A1").Delete Shift:=xlUp
            
End Sub

Edit Sheet1 and Sheet2 as necessary.

This will create a list, starting in A1 on sheet2 with each entry in column A in sheet2 which has a 1 next to it in column B.
 
Upvote 0
This might work

in E1 - enter the catgegory you want to appear in second list

The range starting from E3 could be on another sheet.
thread 240608.xls
ABCDE
1Entercategorytoappearinsecondlist2
2
3AnimalCategoryHelperCategory2
4Dog1 Cat
5Cat21Horse
6Giraffe1 Cow
7Horse22 
8Cow23
9Pig1 
10Fish3 
11Bird1 
12
13
Sheet1
 
Upvote 0
If your data has headers, you can use AdvancedFilter to filter and copy.

Code:
Sub AdvancedFilterMove()
    Dim dataRange As Range
    Dim keyCol As Range, keyValue As Variant
    Dim destinationRange As Range
    Dim critRange As Range
    
    With ThisWorkbook.Sheets("Sheet1"): Rem adjust
        Set keyCol = Range("B:B"): Rem adjust
        keyValue = 1: Rem adjust
        Set dataRange = Range(.Cells(.Rows.Count, 1).End(xlUp), .Cells(1, 2))
    End With
    
    Set destinationRange = ThisWorkbook.Sheets("Sheet2").Range("a1")
          
    Rem set critrange
        With destinationRange.Parent.UsedRange
            Set critRange = .Parent.Cells(1, .Column + .Columns.Count + 1)
        End With
        If critRange.Column < destinationRange.Column + dataRange.Columns.Count Then
            Set critRange = critRange.Parent.Cells(1, destinationRange.Column + dataRange.Columns.Count)
        End If
        
        Rem write criteria
        Set critRange = critRange.Resize(2, 1)
        critRange.Range("A1").Value = keyCol.Range("a1").Value
        critRange.Range("A2").FormulaR1C1 = "'= " & CStr(keyValue)
    Rem filter + move
        destinationRange.Resize(, dataRange.Columns.Count).EntireColumn.ClearContents
            
        dataRange.AdvancedFilter Action:=xlFilterCopy, _
            CriteriaRange:=critRange, CopyToRange:=destinationRange, Unique:=False
    Rem clean-up
        critRange.EntireColumn.Delete
End Sub
 
Upvote 0
Thank you, everyone, for your replies.

I now have a few options to work with and learn before I decide which one I will finally use.

I also read in one of the threads about Worksheet_Change so I may play with that too and see if I can automate it a bit.

This forum is a great learning tool. You get the answer to your specific problem without reading a gazillion books.

thanks again.
 
Upvote 0

Forum statistics

Threads
1,214,646
Messages
6,120,716
Members
448,985
Latest member
chocbudda

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