+ Reply to Thread
Results 1 to 12 of 12

Search range and copy cells to sheet2 if, if statement is true

Hybrid View

  1. #1
    Registered User
    Join Date
    07-11-2012
    Location
    Miami, USA
    MS-Off Ver
    Excel 2007
    Posts
    32

    Search range and copy cells to sheet2 if, if statement is true

    Goodnight everyone,

    I am working on a project and am just beginning to use VBA and am asking for some help.

    I would like to copy from Sheet1 only the Name(Range A2:A100) into a new sheet, Sheet2, if the Recommendation(D2:D100) on Sheet1 is equal to "Y". If the recommendation is "N" then do not copy the Name. The data stops at row 101.

    Therefore,
    If Sheet1 cells of range "D2:D100" match if statement "Y"
    then
    Copy matching Sheet1 cells of range "A2:A100" to Sheet2.

    The copied information should populate in Sheet2 starting at A2 then continue to the next row below and so-forth.

    If any more information is needed I'll be glad to provide some,

    Thanks everyone.

  2. #2
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Search range and copy cells to sheet2 if, if statement is true

    1) Turn on the Data > Filter for that data set
    2) Use the drop down in column D to filter for "Y"
    3) Copy all visible rows all at once to another sheet.

    Should take you about 5 seconds.
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    ?None of us is as good as all of us? - Ray Kroc
    ?Actually, I *am* a rocket scientist.? - JB (little ones count!)

  3. #3
    Registered User
    Join Date
    07-11-2012
    Location
    Miami, USA
    MS-Off Ver
    Excel 2007
    Posts
    32

    Re: Search range and copy cells to sheet2 if, if statement is true

    Thanks JBeaucaire.

    I'm looking to add VBA to a macro button on Sheet2 so when I click it the information from Sheet1 would populate over. When I send out the report, Sheet1 will be veryhidden so only Sheet 2 will be displayed.

    I've never dealt with strings before or ranges in VBA so this is the challenge that I am facing.

  4. #4
    Valued Forum Contributor AlvaroSiza's Avatar
    Join Date
    09-19-2007
    Location
    Staffordshire
    MS-Off Ver
    2007
    Posts
    591

    Re: Search range and copy cells to sheet2 if, if statement is true

    If it must be code:

    Sub SendIfYes()
    
    Dim ws1        As Worksheet
    Dim ws2        As Worksheet
    Dim rngSource  As Range
    Dim rngDest    As Range
    
    Set ws1 = ThisWorkbook.Sheets("Sheet1")
    Set ws2 = ThisWorkbook.Sheets("Sheet2")
    Set rngSource = ws1.Range("D2:D100")
    
    With Application
       .ScreenUpdating = False
       .EnableEvents = False
    End With
       
       For Each rngCell In rngSource
          If rngCell.Value = "Y" Then
             With ws2
                Set rngDest = .Range("A64333").End(xlUp).Offset(1)
             End With
             rngDest.Value = rngCell.Offset(0, -3).Value
          End If
       Next
       
    With Application
       .EnableEvents = True
       .ScreenUpdating = True
    End With
    
    End Sub
    Perhaps it was the Noid who should have avoided me...
    If you are satisfied with my solution click the small star icon on the left. Thanks
    1. Make a copy of your workbook and run the following code on your copy (just in case)
    2. With excel open, press ALT+F11 to open the Visual Basic Editor (VBE). From the "Insert" menu, select "Module".
    3. Paste the code from above into the empty white space. Close the VBE.
    4. From the developer tab, choose "Macros", select the Sub Name, and click "Run".

  5. #5
    Registered User
    Join Date
    07-11-2012
    Location
    Miami, USA
    MS-Off Ver
    Excel 2007
    Posts
    32

    Re: Search range and copy cells to sheet2 if, if statement is true

    I'll give it a try AlvaroSiza. Thanks for the quick reply!

  6. #6
    Forum Expert
    Join Date
    12-10-2006
    Location
    Sydney
    MS-Off Ver
    Office 365
    Posts
    3,565

    Re: Search range and copy cells to sheet2 if, if statement is true

    Hi pinstripe05,

    Welcome to the forum!!

    See how this goes:

    Option Explicit
    Sub Macro1()
    
        'http://www.excelforum.com/excel-programming-vba-macros/857705-search-range-and-copy-cells-to-sheet2-if-if-statement-is-true.html
    
        Dim rngCell As Range
        Dim lngMyRow As Long
        
        Application.ScreenUpdating = False
        
        For Each rngCell In Sheets("Sheet1").Range("D2:D100")
            If rngCell.Value = "Y" Then 'Criteria to copy value (case sensitive)
                If lngMyRow = 0 Then
                    lngMyRow = 2 'Initial row output. Change to suit.
                Else
                    lngMyRow = lngMyRow + 1
                End If
                Sheets("Sheet2").Range("A" & lngMyRow).Value = Sheets("Sheet1").Range("A" & rngCell.Row)
            End If
        Next rngCell
        
        Application.ScreenUpdating = True
    
    End Sub
    Regards,

    Robert
    ____________________________________________
    Please ensure you mark your thread as Solved once it is. Click here to see how
    If this post helps, please don't forget to say thanks by clicking the star icon in the bottom left-hand corner of my post

  7. #7
    Registered User
    Join Date
    07-11-2012
    Location
    Miami, USA
    MS-Off Ver
    Excel 2007
    Posts
    32

    Re: Search range and copy cells to sheet2 if, if statement is true

    You too Trebor76! I'll let you all know how it goes shortly.

  8. #8
    Registered User
    Join Date
    07-11-2012
    Location
    Miami, USA
    MS-Off Ver
    Excel 2007
    Posts
    32

    Re: Search range and copy cells to sheet2 if, if statement is true

    Thank you so much AlvaroSiza! You're code worked wonder. Thank you too Trebor76, the help is much appreciated! Have a wonderful night

  9. #9
    Registered User
    Join Date
    07-11-2012
    Location
    Miami, USA
    MS-Off Ver
    Excel 2007
    Posts
    32

    Re: Search range and copy cells to sheet2 if, if statement is true

    A further question AlvaroSiza, if I would also like to add Store(C2:C100) along with Name(A2:100) how would I incorporate that into the code?

  10. #10
    Forum Expert
    Join Date
    12-10-2006
    Location
    Sydney
    MS-Off Ver
    Office 365
    Posts
    3,565

    Re: Search range and copy cells to sheet2 if, if statement is true

    Hi pinstripe05,

    Try this:

    Option Explicit
    Sub Macro1()
    
        'http://www.excelforum.com/excel-programming-vba-macros/857705-search-range-and-copy-cells-to-sheet2-if-if-statement-is-true.html
    
        Dim rngCell As Range
        Dim lngMyRow As Long
        
        Application.ScreenUpdating = False
        
        For Each rngCell In Sheets("Sheet1").Range("D2:D100")
            If rngCell.Value = "Y" Then 'Criteria to copy value (case sensitive)
                If lngMyRow = 0 Then
                    lngMyRow = 2 'Initial row output. Change to suit.
                Else
                    lngMyRow = lngMyRow + 1
                End If
                With Sheets("Sheet2")
                    .Range("A" & lngMyRow).Value = Sheets("Sheet1").Range("A" & rngCell.Row)
                    .Range("B" & lngMyRow).Value = Sheets("Sheet1").Range("C" & rngCell.Row)
                End With
            End If
        Next rngCell
        
        Application.ScreenUpdating = True
    
    End Sub
    Robert

  11. #11
    Registered User
    Join Date
    07-11-2012
    Location
    Miami, USA
    MS-Off Ver
    Excel 2007
    Posts
    32

    Re: Search range and copy cells to sheet2 if, if statement is true

    Worked great. Thank you Robert!

  12. #12
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Search range and copy cells to sheet2 if, if statement is true

    I have marked this thread solved for you.
    In the future please select Thread Tools from the menu above and mark the thread as solved. Thanks.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1