+ Reply to Thread
Results 1 to 5 of 5

Thread: copying a color from one cell to another

  1. #1
    Registered User
    Join Date
    06-18-2011
    Location
    canada
    MS-Off Ver
    Excel 2007
    Posts
    2

    Unhappy copying a color from one cell to another

    I have this code that I want to use to copy a color:

    Sub color_switcher(start_cell As Cells, end_cell As Cells)
    
    Dim color As Long
    
    color = start_cell.Interior.color
    
    end_cell.Interior.color = color
    
    End Sub

    and here is my test sub:

    Sub test_func()
    
    Call color_switcher(Cells(1, 1), Cells(2, 1))
    
    
    End Sub
    Im getting the error:

    user defined type not defined

    what am I doing wrong?

    thanks, in advance.

  2. #2
    Valued Forum Contributor
    Join Date
    07-17-2005
    Location
    Abergavenny, Wales, UK
    MS-Off Ver
    XL2003, XL2007, XL2010
    Posts
    474

    Re: copying a color from one cell to another

    Hi

    Try using Range not Cells

    [CODE]

    Sub color_switcher(start_cell As Range, end_cell As Range)

    Dim color As Long

    color = start_cell.Interior.color

    end_cell.Interior.color = color

    End Sub
    [/CODE
    --
    Regards
    Roger Govier
    Microsoft Excel MVP

  3. #3
    Registered User
    Join Date
    06-18-2011
    Location
    canada
    MS-Off Ver
    Excel 2007
    Posts
    2

    Re: copying a color from one cell to another

    thanks for the quick reply.

    I tried it with range but got the

    object _global failed error

  4. #4
    Valued Forum Contributor
    Join Date
    06-19-2010
    Location
    Holywell, N Wales, UK
    MS-Off Ver
    Excel 2007
    Posts
    351

    Re: copying a color from one cell to another

    Try replacing
    Sub color_switcher(start_cell As Cells, end_cell As Cells)
    with
    Sub color_switcher(start_cell, end_cell)

  5. #5
    Valued Forum Contributor
    Join Date
    06-19-2010
    Location
    Holywell, N Wales, UK
    MS-Off Ver
    Excel 2007
    Posts
    351

    Re: copying a color from one cell to another

    and to finish off the switch?
    Sub color_switcher(start_cell, end_cell)
    
    Dim color1 As Long, color2 As Long
    
    color1 = start_cell.Interior.color
    color2 = end_cell.Interior.color
    
    start_cell.Interior.color = color2
    end_cell.Interior.color = color1
    
    End Sub

+ 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