Consulting

Results 1 to 3 of 3

Thread: Extract Data From another sheet using VBA

  1. #1

    Extract Data From another sheet using VBA

    I have a data sheet in a workbook which I need to extract all the rows where Col G is equal to "Y" and insert the adjacent Cells (Cols A:F) into another sheet.

    Is there a piece of VBA code that can do this please?

    Many thanks

  2. #2
    Moderator VBAX Guru Simon Lloyd's Avatar
    Joined
    Sep 2005
    Location
    UK
    Posts
    3,003
    Location
    We would need to see a sample workbook to give you a workable solution but a general code would be:[VBA]Sub copy_over_data()
    Dim Rng As Range, MyCell As Range
    Set Rng = Sheets("Sheet1").Range("G1:G" & Sheets("Sheet1").Range("G" & Rows.Count).End(xlUp).Row)
    For Each MyCell In Rng
    If LCase(MyCell.Value) = LCase("Y") Then
    MyCell.Offset(0,1).Copy Destination:=Sheets("Sheet2").Range("A" & Rows.Count).End(xlUp).Offset(1, 0)
    End If
    Next MyCell
    End Sub[/VBA]
    Regards,
    Simon
    Please read this before cross posting!
    In the unlikely event you didn't get your answer here try Microsoft Office Discussion @ The Code Cage
    If I have seen further it is by standing on the shoulders of giants.
    Isaac Newton, Letter to Robert Hooke, February 5, 1675 English mathematician & physicist (1642 - 1727)

  3. #3
    Thanks for your response.

    I have tried to adapt the code you provided, which unfortunately didn't work as follows:

    [vba]
    Sub copy_over_data()
    Dim Rng As Range, MyCell As Range
    Set Rng = Sheets("Data(Timesheets)").Range("G1:G" & Sheets("Data(Timesheets)").Range("G" & Rows.Count).End(xlUp).Row)
    For Each MyCell In Rng
    If LCase(MyCell.Value) = LCase("Y") Then
    MyCell.Offset(0, 1).Copy Destination:=Sheets("Sheet1").Range("A" & Rows.Count).End(xlUp).Offset(1, 0)
    End If
    Next MyCell
    End Sub
    [/vba]

    I've attached a workbook with a sample set of data.

    To elaborate - I need to extract all the information in Cols A:F where the value of the adjacent cell in Col G is "Y". This extract then needs to be placed in Sheet1.

    Many thanks again.

Posting Permissions

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