+ Reply to Thread
Results 1 to 8 of 8

Need VBA Code to "VLOOKUP" but keep source cell Formatting

Hybrid View

  1. #1
    Registered User
    Join Date
    10-03-2012
    Location
    US
    MS-Off Ver
    Excel 2003
    Posts
    2

    Need VBA Code to "VLOOKUP" but keep source cell Formatting

    Hello all,

    I'm new to the forum and am needing some help. Attached is a VERY simple spreadsheet with Animals on Sheet1 and a table on Sheet2, also with the same animal names, but corresponding colors (each with different formatting). I know there is no way for the VLOOKUP to output the format of the source, so I'm looking for a VBA code to Find the matching Animal from Sheet1, on Sheet2, then output the Formatted Corresponding color back to Sheet 1, adjacent to that animal. For example,

    Lookup Sheet1, A3 ("Lion") on Sheet2, A2:B5, then output Sheet2, B5 (Red) to Sheet1,B3.

    I am using Excel 2003, but have the ability to use 2007 if needed. Thank you for your time.
    Attached Files Attached Files
    Last edited by flyyboy84; 11-01-2012 at 10:37 AM.

  2. #2
    Forum Contributor
    Join Date
    09-25-2012
    Location
    Philadelphia, PA
    MS-Off Ver
    Excel 2010
    Posts
    154

    Re: Need VBA Code to "VLOOKUP" but keep source cell Formatting

    Try this. This will copy the format that corresponds to the lookup value and paste the format to Sheet 1

    Sub test()
    
    Dim rCell As Range
    Dim x As Integer
    
    'First use a vlookup to pull in the color of each animal
    For Each rCell In Range("a2", Range("a2").End(xlDown))
    rCell.Offset(0, 1).Formula = "=vlookup(" & rCell.Address(0, 1) & ",Sheet2!$A$2:$B$5,2,false)"
    
    'Then find the format of the color on Sheet 2 and copy it.
    Sheets("sheet2").Activate
    x = WorksheetFunction.Match(rCell.Value, Range("a1", Range("a1").End(xlDown)), False)
    Cells(x, 2).Select
    Selection.Copy
    
    'Then paste the format to the adjacent cell the loop is running for
    Sheets("sheet1").Select
    rCell.Select
    rCell.Offset(0, 1).Select
        With Selection
            .PasteSpecial xlPasteFormats
        End With
    
    
    
    Next rCell
    
    
    
    
    
    End Sub

  3. #3
    Forum Contributor
    Join Date
    08-14-2012
    Location
    USA
    MS-Off Ver
    Excel 2007, MS 365 (Windows 10 Pro 64-bit)
    Posts
    818

    Re: Need VBA Code to "VLOOKUP" but keep source cell Formatting

    Quote Originally Posted by jkj115 View Post
    Try this. This will copy the format that corresponds to the lookup value and paste the format to Sheet 1

    Sub test()
    
    Dim rCell As Range
    Dim x As Integer
    
    'First use a vlookup to pull in the color of each animal
    For Each rCell In Range("a2", Range("a2").End(xlDown))
    rCell.Offset(0, 1).Formula = "=vlookup(" & rCell.Address(0, 1) & ",Sheet2!$A$2:$B$5,2,false)"
    
    'Then find the format of the color on Sheet 2 and copy it.
    Sheets("sheet2").Activate
    x = WorksheetFunction.Match(rCell.Value, Range("a1", Range("a1").End(xlDown)), False)
    Cells(x, 2).Select
    Selection.Copy
    
    'Then paste the format to the adjacent cell the loop is running for
    Sheets("sheet1").Select
    rCell.Select
    rCell.Offset(0, 1).Select
        With Selection
            .PasteSpecial xlPasteFormats
        End With
    
    
    
    Next rCell
    
    
    
    
    
    End Sub

    I got an error message when try your code:
    "Run-time error '1004'"
    Select method of Range class failed"

    Regards,
    tt3

  4. #4
    Forum Contributor
    Join Date
    09-25-2012
    Location
    Philadelphia, PA
    MS-Off Ver
    Excel 2010
    Posts
    154

    Re: Need VBA Code to "VLOOKUP" but keep source cell Formatting

    On what line of the code (go to VBA and hit f8 to click through the code step by step)? What are the names of your sheets

  5. #5
    Forum Contributor
    Join Date
    08-14-2012
    Location
    USA
    MS-Off Ver
    Excel 2007, MS 365 (Windows 10 Pro 64-bit)
    Posts
    818

    Re: Need VBA Code to "VLOOKUP" but keep source cell Formatting

    Hi jkj115,

    The error is at below line and the names are Sheet1 and Sheet2

    Cells(x, 2).Select

    Regards,
    tt3

  6. #6
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,834

    Re: Need VBA Code to "VLOOKUP" but keep source cell Formatting

    Try
    Sub test()
        Dim r As Range
        With CreateObject("Scripting.Dictionary")
            .CompareMode = 1
            For Each r In Sheets("sheet2").Cells(1).CurrentRegion.Columns(1).Cells
                Set .Item(r.Value) = r(, 2)
            Next
            For Each r In Sheets("sheet1").Cells(1).CurrentRegion.Columns(1).Cells
                If .exists(r.Value) Then .Item(r.Value).Copy r(, 2)
            Next
        End With
    End Sub

  7. #7
    Forum Contributor
    Join Date
    08-14-2012
    Location
    USA
    MS-Off Ver
    Excel 2007, MS 365 (Windows 10 Pro 64-bit)
    Posts
    818

    Re: Need VBA Code to "VLOOKUP" but keep source cell Formatting

    Quote Originally Posted by jindon View Post
    Try
    Sub test()
        Dim r As Range
        With CreateObject("Scripting.Dictionary")
            .CompareMode = 1
            For Each r In Sheets("sheet2").Cells(1).CurrentRegion.Columns(1).Cells
                Set .Item(r.Value) = r(, 2)
            Next
            For Each r In Sheets("sheet1").Cells(1).CurrentRegion.Columns(1).Cells
                If .exists(r.Value) Then .Item(r.Value).Copy r(, 2)
            Next
        End With
    End Sub
    Hi jindon,
    Your code is perfect and thank you for your help.

    Regards,
    tt3

  8. #8
    Registered User
    Join Date
    10-03-2012
    Location
    US
    MS-Off Ver
    Excel 2003
    Posts
    2

    Re: Need VBA Code to "VLOOKUP" but keep source cell Formatting

    This works PERFECTLY. Thank you so much jindon; you don't know how much time you just saved me!!!

+ 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