Closed Thread
Results 1 to 3 of 3

Lookup return cell format

  1. #1
    Registered User
    Join Date
    10-25-2023
    Location
    Denmark
    MS-Off Ver
    365
    Posts
    3

    Lookup return cell format

    Hi everyone,

    *Reposted as I had replied to my previous post with the code which possibly made the post go under the radar*


    I have been looking for a vba code that creates a lookup function that returns cell formats.

    I have a sheet with a list of color names in column A and color formats in column B. This sheet is called "Data".
    In a different sheet called "Working sheet", which is in the same workbook, I would like to choose a color name from a data validation list and then have the lookup function return the corrosponding color format in the cell next to the name.

    I have found the code below and inserted it as a module and enabled "Microsoft scripting runtime" under references, but I cant make it work. Can anyone explain if the code should work and if so what I need to do differently?

    Thank you in advance.


    Public xDic As New Dictionary
    Public strWB As String
    Public strWS As String

    Function CLookup(ByRef FndValue, ByRef LookupRng As Range, ByRef xCol As Long)
    Dim xFindCell As Range
    On Error Resume Next

    strWB = LookupRng.Test.xlsm '*** Remember the Workbook where the data and color are coming from
    strWS = LookupRng.Data '*** Remember the Worksheet where the data and color are coming from

    Set xFindCell = LookupRng.Find(FndValue, , xlValues, xlWhole)

    If xFindCell Is Nothing Then
    CLookup = ""
    xDic.Add Application.Caller.Address, ""
    Else
    CLookup = xFindCell.Offset(0, xCol - 1).Value
    xDic.Add Application.Caller.Address, xFindCell.Offset(0, xCol - 1).Address

    End If
    End Function

    Sub Worksheet_Change(ByVal Target As Range)
    Dim I As Long
    Dim xKeys As Long
    Dim xDicStr As String
    Dim rngLoc As Range
    On Error Resume Next
    Application.ScreenUpdating = False
    xKeys = UBound(xDic.Keys)
    If xKeys >= 0 Then
    For I = 0 To UBound(xDic.Keys)
    xDicStr = xDic.Items(I)
    If xDicStr <> "" Then
    Range(xDic.Keys(I)).Interior.Color = Application.Workbooks(strWB).Worksheets(strWS).Range(xDic.Items(I)).Interior.Color
    Else
    Range(xDic.Keys(I)).Interior.Color = xlNone
    End If
    Next
    Set xDic = Nothing
    End If
    Application.ScreenUpdating = True
    End Sub
    Attached Files Attached Files

  2. #2
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2406 (Windows 11 23H2 64-bit)
    Posts
    81,483

    Re: Lookup return cell format

    Administrative Note:

    We would very much like to help you with your query, however you need to include code tags around your code.

    Please take a moment to add the tags. Posting code between [code]Please [url=https://www.excelforum.com/login.php]Login or Register [/url] to view this content.[/code] tags makes your code much easier to read and copy for testing, and it also maintains VBA formatting.

    Please see Forum Guideline #2 about code tags and adjust accordingly. Click on Edit to open your post, then highlight your code and click the # icon at the top of your post window. More information about these and other tags can be found here.
    Ali


    Enthusiastic self-taught user of MS Excel who's always learning!
    Don't forget to say "thank you" in your thread to anyone who has offered you help.
    You can reward them by clicking on * Add Reputation below their user name on the left, if you wish.

    Forum Rules (updated August 2023): please read them here.

  3. #3
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2406 (Windows 11 23H2 64-bit)
    Posts
    81,483

    Re: Lookup return cell format

    Administrative Note:

    Unfortunately, this is a duplicate thread, and you are allowed only ONE thread per issue here.

    Please see Forum Rule #2 about thread duplication.

    I am closing this thread, but you may continue here in the original thread: https://www.excelforum.com/excel-pro...-workbook.html

    Thread closed.

Closed Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Replies: 1
    Last Post: 09-18-2021, 01:29 PM
  2. [SOLVED] How to return date format in cell instead of general format?
    By rayted in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 05-19-2021, 10:37 AM
  3. [SOLVED] Lookup: Lookup employee id and return value in cell x basued on most recent start date
    By jekeith in forum Excel Formulas & Functions
    Replies: 8
    Last Post: 05-21-2018, 12:56 PM
  4. Replies: 3
    Last Post: 08-16-2016, 07:58 AM
  5. Lookup a blank cell in a Lookup table and return a figure.
    By jonnops in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 03-20-2014, 07:33 AM
  6. lookup functions to return both contents and format of a cell
    By jerseyguy1996 in forum Excel General
    Replies: 3
    Last Post: 03-16-2009, 12:48 PM
  7. using lookup to return more than one answer in date format
    By Geni Slaughter in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 11-28-2005, 02:40 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