+ Reply to Thread
Results 1 to 4 of 4

Thread: Macro to compare columns in 2 worksheets and paste to 3rd worksheet

  1. #1
    Registered User
    Join Date
    10-28-2010
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    2

    Macro to compare columns in 2 worksheets and paste to 3rd worksheet

    Hello,

    I've been trying to come up with a macro to suit my need but haven't been able to figure it out. This is what I am trying to do:

    1. I can create a spreadsheet from a database that lists all action items from various reviews. This spreadsheet cannot be edited but can be refreshed at any point in time to update the action item status.

    2. I created a new blank worksheet in this workbook and filled in various review numbers that I care about [Column A].

    3. The original spreadsheet with action items contains the action item numbers in one column which contain the review number within them [Column B]. So for review number AI-R5-2010-584659 the original spreadsheet would have all action items listed by AI-R5-2010-584659-01, AI-R5-2010-584659-02, etc.

    4. I want to create a macro that compares the review number portion of the action item number in Column B of "Sheet1" to the review number in "Sheet2" and then copy all matching rows from "Sheet1" to a new worksheet [Sheet 3].

    5. Additionally, I would like to copy the header row in Sheet1 to Sheet3 as well.

    If I can automate this process it will be of great help for the future.

    Hopefully this all makes sense and thanks for the help!
    Attached Files Attached Files

  2. #2
    Forum Guru
    Join Date
    01-15-2007
    Location
    Brisbane, Australia
    MS-Off Ver
    2007
    Posts
    5,359

    Re: Macro to compare columns in 2 worksheets and paste to 3rd worksheet

    Hi

    See how this goes.
    Sub aaa()
      Dim OutSH As Worksheet
      Set OutSH = Sheets("Sheet3")
      OutSH.Rows("1:1").Value = Sheets("SHeet1").Rows("1:1").Value
      
      With Sheets("sheet2")
        .Range("A:A").Copy Destination:=.Range("D:D")
        .Range("D1").Value = Sheets("Sheet1").Range("B1").Value
        For Each ce In .Range(.Range("D2"), .Range("D2").End(xlDown))
          ce.Value = ce.Value & "*"
        Next ce
        Set critrng = .Range(.Range("D1"), .Range("D1").End(xlDown))
      End With
      
      Sheets("Sheet1").Range("A1").CurrentRegion.AdvancedFilter action:=xlFilterCopy, copytorange:=OutSH.Range("A1:N1"), criteriarange:=critrng
      Sheets("Sheet2").Range("D:D").ClearContents
      
    End Sub
    rylo

  3. #3
    Registered User
    Join Date
    10-28-2010
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    2

    Re: Macro to compare columns in 2 worksheets and paste to 3rd worksheet

    Awesome! This worked just as expected.

    I added a few things to the code to make it a little more functional. The only other thing I would like to do is make all of the headers drop down lists in the "Project Action Items" worksheet. Any ideas?

    Here is the code I have:

    Sub AIList()
    
      Sheet1.ListObjects("List1").UpdateChanges xlListConflictDialog
    
      On Error Resume Next
      Application.DisplayAlerts = False
      Sheets("Project Action Items").Delete
      Application.DisplayAlerts = True
      On Error GoTo 0
    
      Dim OutSH As Worksheet
      Set OutSH = Sheets.Add
      OutSH.Name = "Project Action Items"
      OutSH.Rows("1:1").Value = Sheet1.Rows("1:1").Value
      
      With Sheet2
        .Range("A:A").Copy Destination:=.Range("D:D")
        .Range("D1").Value = Sheets("owssvr(1)").Range("B1").Value
        For Each ce In .Range(.Range("D2"), .Range("D2").End(xlDown))
          ce.Value = ce.Value & "*"
        Next ce
        Set critrng = .Range(.Range("D1"), .Range("D1").End(xlDown))
      End With
      
      Sheet1.Range("A1").CurrentRegion.AdvancedFilter action:=xlFilterCopy, copytorange:=OutSH.Range("A1:N1"), criteriarange:=critrng
      Sheet2.Range("D:D").ClearContents
      Sheets("Project Action Items").Columns.AutoFit
      Sheets("Project Action Items").Rows("1:1").Select
        Selection.Font.Bold = True
      
    End Sub
    Thanks!
    Last edited by jsticca; 10-29-2010 at 01:48 PM.

  4. #4
    Forum Guru
    Join Date
    01-15-2007
    Location
    Brisbane, Australia
    MS-Off Ver
    2007
    Posts
    5,359

    Re: Macro to compare columns in 2 worksheets and paste to 3rd worksheet

    Hi

    How about an autofilter.

    outsh.range("A1").currentregion.autofilter
    rylo

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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.2.0