copy data to other sheet based on conditions

Posted on 2011-04-22
Last Modified: 2012-05-11
I have this excel sheet which I'm attaching. In the first column I have some numbers and the 2nd column I have a yes/no value where yes is indicated by a value of '1'. I wish to copy the values from the 1st column when the row of the 2nd column equals '1'. How do I do this? I guess with some vb script?

Can someone give me some code in vb because I'd like to learn it too?

If there is an easier way as well can you please show it to me?

Thank you,
Victor copy-data-to-other-sheet.xls
Question by:Victor Kimura
    LVL 12

    Expert Comment

    Where do you want to copy the values TO?
    LVL 18

    Accepted Solution


    Try this

    Sub kTest()
    Dim k(), ka, i As Long, n As Long
    ka = [a1].CurrentRegion.Resize(, 2)
    ReDim k(1 To UBound(ka, 1), 1 To UBound(ka, 2))
    For i = 2 To UBound(ka, 1)
        If ka(i, 2) = 1 Then
            n = n + 1
            k(n, 1) = ka(i, 1): k(n, 2) = ka(i, 2)
        End If
    Sheets("Sheet2").Range("a1").Resize(n, 2).Value2 = k
    End Sub

    Open in new window


    Author Closing Comment

    by:Victor Kimura
    Thank you. Works nice.

    Featured Post

    Better Security Awareness With Threat Intelligence

    See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    Excel -- need lookup or match function 4 38
    IF OR formula Excel 8 24
    Update Column Based On Match 3 17
    Messed up my code 6 5
    INDEX and MATCH can be used to great effect to replace HLOOKUP and VLOOKUP as it does not have the limitation of needing the data to be sorted so that the reference value is in the first column or row. It also has the ability to perform a bi-directi…
    Workbook link problems after copying tabs to a new workbook? David Miller (dlmille) Intro Have you either copied sheets to a new workbook, and after having saved and opened that workbook, you find that there are links back to the original sou…
    The viewer will learn how to simulate a series of coin tosses with the rand() function and learn how to make these “tosses” depend on a predetermined probability. Flipping Coins in Excel: Enter =RAND() into cell A2: Recalculate the random variable…
    The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…

    746 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now