Results 1 to 11 of 11

Macro to Set Filter and Copy past

Threaded View

  1. #1
    Registered User
    Join Date
    02-14-2012
    Location
    hyderabad
    MS-Off Ver
    Excel 2003
    Posts
    22

    Macro to Set Filter and Copy past

    Hi All,

    I have created a Macro to for below.

    A)I have Codes on Sheet 2
    B) Source data on Sheet1
    C) Set filter in Sheet1, filter the data in sheet1 based on the codes on sheet2, copy data and paste it in Sheet3

    However, am facing following problems.

    A) Header row is not copied and pasted
    B)Initially it has to filter the data for the first code in Sheet2 and paste it in Sheet3 and later it has to set filter for second code and copy past so on.

    Could you please check the code and make changes.


    Option Explicit
    
    Sub CopyRowsThatHaveTheRightCode()
    Dim iSourceRow As Long
    Dim iDestinationRow As Long
    Dim iCode As Long
    Dim varCodes As Variant
    Dim booCopyThisRow As Boolean
    
    ' Get the pass codes
    varCodes = Worksheets("Sheet2").Range("A2").Resize(2, 1)
    'Loop through all rows in source sheet
    
    iDestinationRow = 0
    For iSourceRow = 1 To 500 ' or however many rows you have
    booCopyThisRow = False
    
    For iCode = LBound(varCodes, 1) To UBound(varCodes, 1)
    If varCodes(iCode, 1) _
    = Worksheets("Sheet1").Range("s:s").Cells(iSourceRow, 1) Then ' Code matches.
    booCopyThisRow = True
    Exit For
    End If
    Next iCode
    
    If booCopyThisRow = True Then
    ' Copy into next available destination row
    iDestinationRow = iDestinationRow + 1
    Worksheets("Sheet1").Range("A:Z").Cells(iSourceRow, 1).Resize(1, 28).Copy _
    Destination:=Worksheets("Sheet3").Range("a1").Cells(iDestinationRow, 1)
    End If
    Next iSourceRow
    
    End Sub
    Last edited by davesexcel; 03-03-2012 at 02:52 PM. Reason: Code tags required when showing VBA code

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