+ Reply to Thread
Results 1 to 11 of 11

VB to copy cell value in sheet1 to cells in sheet2 if criterion met

Hybrid View

  1. #1
    Registered User
    Join Date
    12-05-2013
    Location
    Utah
    MS-Off Ver
    Excel 2013
    Posts
    31

    VB to copy cell value in sheet1 to cells in sheet2 if criterion met

    This following is just an example:

    In sheet1 I have 4 columns; Name, Age, *** and Job.

    Sheet2 has three columns; Name, Age and Job.

    What I need is VB that will check the *** column from Sheet1 and when it is Male, the Name, Age and Job fields from that row are copied into the cells in Sheet2.

    It's been a while since I've worked with VB and I am a bit rusty. Having a hard time figuring it out.

  2. #2
    Forum Expert Mumps1's Avatar
    Join Date
    10-10-2012
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2010, 365
    Posts
    8,026

    Re: VB to copy cell value in sheet1 to cells in sheet2 if criterion met

    Try:
    Sub CopyData()
        Application.ScreenUpdating = False
        Dim LastRow As Long
        LastRow = Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
        Dim rng As Range
        For Each rng In Range("C2:C" & LastRow)
            If rng = "Male" Then
                Range("A" & rng.Row & ":B" & rng.Row).Copy Sheets("Sheet2").Cells(Rows.Count, "A").End(xlUp).Offset(1, 0)
                Range("D" & rng.Row).Copy Sheets("Sheet2").Cells(Rows.Count, "C").End(xlUp).Offset(1, 0)
            End If
        Next rng
        Application.ScreenUpdating = True
    End Sub
    You can say "THANK YOU" for help received by clicking the Star symbol at the bottom left of the helper's post.
    Practice makes perfect. I'm very far from perfect so I'm still practising.

  3. #3
    Registered User
    Join Date
    12-05-2013
    Location
    Utah
    MS-Off Ver
    Excel 2013
    Posts
    31

    Re: VB to copy cell value in sheet1 to cells in sheet2 if criterion met

    Quote Originally Posted by Mumps1 View Post
    Try:
    Sub CopyData()
        Application.ScreenUpdating = False
        Dim LastRow As Long
        LastRow = Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
        Dim rng As Range
        For Each rng In Range("C2:C" & LastRow)
            If rng = "Male" Then
                Range("A" & rng.Row & ":B" & rng.Row).Copy Sheets("Sheet2").Cells(Rows.Count, "A").End(xlUp).Offset(1, 0)
                Range("D" & rng.Row).Copy Sheets("Sheet2").Cells(Rows.Count, "C").End(xlUp).Offset(1, 0)
            End If
        Next rng
        Application.ScreenUpdating = True
    End Sub
    What is the best way to invoke it so that it doesn't copy itself over and over?

  4. #4
    Forum Expert Mumps1's Avatar
    Join Date
    10-10-2012
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2010, 365
    Posts
    8,026

    Re: VB to copy cell value in sheet1 to cells in sheet2 if criterion met

    Assuming that Sheet2 has headers in the first row, try:
    Sub CopyData()
        Application.ScreenUpdating = False
        Sheets("Sheet2").UsedRange.Offset(1, 0).ClearContents
        Dim LastRow As Long
        LastRow = Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
        Dim rng As Range
        For Each rng In Range("C2:C" & LastRow)
            If rng = "Male" Then
                Range("A" & rng.Row & ":B" & rng.Row).Copy Sheets("Sheet2").Cells(Rows.Count, "A").End(xlUp).Offset(1, 0)
                Range("D" & rng.Row).Copy Sheets("Sheet2").Cells(Rows.Count, "C").End(xlUp).Offset(1, 0)
            End If
        Next rng
        Application.ScreenUpdating = True
    End Sub

  5. #5
    Registered User
    Join Date
    12-05-2013
    Location
    Utah
    MS-Off Ver
    Excel 2013
    Posts
    31

    Re: VB to copy cell value in sheet1 to cells in sheet2 if criterion met

    Quote Originally Posted by Mumps1 View Post
    Assuming that Sheet2 has headers in the first row, try:
    Sub CopyData()
        Application.ScreenUpdating = False
        Sheets("Sheet2").UsedRange.Offset(1, 0).ClearContents
        Dim LastRow As Long
        LastRow = Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
        Dim rng As Range
        For Each rng In Range("C2:C" & LastRow)
            If rng = "Male" Then
                Range("A" & rng.Row & ":B" & rng.Row).Copy Sheets("Sheet2").Cells(Rows.Count, "A").End(xlUp).Offset(1, 0)
                Range("D" & rng.Row).Copy Sheets("Sheet2").Cells(Rows.Count, "C").End(xlUp).Offset(1, 0)
            End If
        Next rng
        Application.ScreenUpdating = True
    End Sub
    Works very well except when one of the cells is empty and then instead of leaving it empty on sheet2 it messes up the order

  6. #6
    Forum Expert Mumps1's Avatar
    Join Date
    10-10-2012
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2010, 365
    Posts
    8,026

    Re: VB to copy cell value in sheet1 to cells in sheet2 if criterion met

    When you say that one of the cells is empty, do you mean one of the cells in column C? Could you post a copy of your file? This way it would be easier to see how your data is organized.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Replies: 6
    Last Post: 07-25-2013, 02:58 PM
  2. [SOLVED] If cell value on Sheet1 not found on Sheet2 copy row to Sheet2
    By Yxx in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 06-18-2013, 04:15 AM
  3. Macro to copy range of cell from sheet1 to sheet2 based on a cell value in sheet2
    By drgwhizz in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 01-25-2012, 10:39 AM
  4. Copy 3 cells from sheet1 then paste to sheet2
    By Christeen in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 05-10-2011, 09:16 AM
  5. IF Sheet1 Cell A2 = value, THEN copy Sheet1 Row 2 to Sheet2 Row 2....?
    By dan1980 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 07-21-2009, 10:28 PM

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