+ Reply to Thread
Results 1 to 11 of 11

Changing the colour of a cell causes an error

  1. #1
    Registered User
    Join Date
    08-12-2011
    Location
    London
    MS-Off Ver
    Excel 2003
    Posts
    17

    Changing the colour of a cell causes an error

    Hi,

    I am using a form to populate a row on a spreadsheet. The code below works when the worksheet is not protected, but when it is the line below causes an error. Does anyone know why changing the colour of the cell is causing a issue?

    Worksheets("Master").Cells(Row, 9).Interior.ColorIndex = 15 'Change colour of cell


    PHP Code: 
    If valid Then

    'Unprotect spreadsheet
    ActiveWorkbook.Unprotect ("fx")

        '
    Find the last row
        Row 
    Range("A65536").End(xlUp).Row
        Row 
    Row 1

        
    'Populate next row
        Worksheets("Master").Cells(Row, 1).Value = customerInput.Value
        Worksheets("Master").Cells(Row, 2).Value = currencyPair.Value
        Worksheets("Master").Cells(Row, 3).Value = direction.Value
        Worksheets("Master").Cells(Row, 4).Value = amount.Value
        Worksheets("Master").Cells(Row, 5).Value = fixType.Value
        Worksheets("Master").Cells(Row, 6).Value = todayDate.Value
        Worksheets("Master").Cells(Row, 7).Value = fixTime.Value
        Worksheets("Master").Cells(Row, 8).Value = dealer.Value
        
        '
    Change colour of cell and put placed in confirmed order
        Worksheets
    ("Master").Cells(Row9) = "Placed" 'Put Placed in cell
        Worksheets("Master").Cells(Row, 9).Interior.ColorIndex = 15 '
    Change colour of cell

        
    'Close form
        Unload Me

        '
    Save spreadsheet
        ActiveWorkbook
    .Save
        
        
    'Protect spreadsheet
        ActiveWorkbook.Protect ("fx")
        

    End If 

  2. #2
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Changing the colour of a cell causes an error

    Hi,
    Precisely because the worksheet is protected. Unprotect it first and then reset it at the end of the macro.

    Regards
    Richard Buttrey

    RIP - d. 06/10/2022

    If any of the responses have helped then please consider rating them by clicking the small star icon below the post.

  3. #3
    Registered User
    Join Date
    08-12-2011
    Location
    London
    MS-Off Ver
    Excel 2003
    Posts
    17

    Re: Changing the colour of a cell causes an error

    Hi Richard,

    I am am unprotecting the worksheet at the start and protecting it at the end and this work when I don't include the line

    Worksheets("Master").Cells(Row, 9).Interior.ColorIndex = 15 'Change colour of cell

    However when I do include it I get an error. Any ideas?

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

    Re: Changing the colour of a cell causes an error

    ewan1979
    What is the error reporting?
    Presumably "Placed" gets placed in the cell correctly?

  5. #5
    Registered User
    Join Date
    08-12-2011
    Location
    London
    MS-Off Ver
    Excel 2003
    Posts
    17

    Re: Changing the colour of a cell causes an error

    Hi barryleajo,

    Yes "placed" gets placed it is changing the colour of thew cell that causes the issue.

    The error message is "Run time error 1004 Application defined or object defined error".

    Any help would be greatly appreciated.

  6. #6
    Registered User
    Join Date
    08-12-2011
    Location
    London
    MS-Off Ver
    Excel 2003
    Posts
    17

    Re: Changing the colour of a cell causes an error

    Any ideas guys?

  7. #7
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Changing the colour of a cell causes an error

    Hi,

    You are unprotecting the workbook not the worksheet. Unprotect the worksheet as I suggested.

    Regards

  8. #8
    Valued Forum Contributor
    Join Date
    06-19-2010
    Location
    Holywell, N Wales, UK
    MS-Off Ver
    Excel 2013
    Posts
    470

    Re: Changing the colour of a cell causes an error

    ewan1979
    The code snippet works fine for me on my PC.
    I suspect its not a sheet protection issue because the cell is written-to successfully by the previous statement.
    I have just noticed that you are using Excel 2003 which may deal with colours differently.
    Try a different way of setting the colour, for example:

    Please Login or Register  to view this content.
    Note you will need to get the correct RGB combination for your grey colour.
    May help
    Barry

  9. #9
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Changing the colour of a cell causes an error

    Hi,

    It also works fine on my PC using both XL 2003 & XL 2007.

    Can you upload the actual workbook?

    Regards

  10. #10
    Registered User
    Join Date
    08-12-2011
    Location
    London
    MS-Off Ver
    Excel 2003
    Posts
    17

    Re: Changing the colour of a cell causes an error

    Hi All,

    Than you kindly for all your help the unprotect worksheet idea worked.

    Thanks again.

  11. #11
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Changing the colour of a cell causes an error

    Quote Originally Posted by ewan1979 View Post
    Hi All,

    Than you kindly for all your help the unprotect worksheet idea worked.

    Thanks again.
    Hi,

    Does that mean your post #3 was wrong when you said you were already unprotecting the worksheet?

    Regards

+ 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