Consulting

Results 1 to 9 of 9

Thread: Problem with Colors?!

  1. #1
    VBAX Regular
    Joined
    Jan 2008
    Posts
    28
    Location

    Problem with Colors?!

    Hi All,

    I have created an Excel file, ih which I set my own colors via VBA:

    ThisWorkbook.Colors(1)=RGB(15,15,15) ' something like this

    It worked until I saved it as an add-in. As an add-in, the VBA ignores my commands on changing some standard colors.

    How can I fix it?

    Thank you!

    Alex

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Is it because you are using ThisWorkbook, so it is changing the colour palette of the addin, not the workbook being worked upon?
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  3. #3
    VBAX Regular
    Joined
    Jan 2008
    Posts
    28
    Location
    Thank you for the insight! And how to fix it?

  4. #4
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Use ActiveWorkbook, not ThisWorkbook.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  5. #5
    VBAX Regular
    Joined
    Jan 2008
    Posts
    28
    Location
    It also does not work. The matter is -- there is the following code in my xla file:

    Private Sub Workbook_Open()
    ActiveWorkbook.Colors(1) = RGB(100,100,100)
    ....
    ActiveWorkbook.Colors(56) = RGB(10,200,400)
    end sub

    It causes a run-time error '91': Object variable or With block variable not set

  6. #6
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    1) there is nothing wrong with that code.

    2) why would you put it in Workbook_Open, that fires when the addin is opened, which will also be the activeworkbook at that time. You need to create application events, and check each workbook being opened to see if it needs to be acted upon.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  7. #7
    VBAX Regular
    Joined
    Jan 2008
    Posts
    28
    Location
    Could you make a hint on how to create such an event handler?

    Thank you very much indeed!

  8. #8
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    [vba]

    Public WithEvents App As Application

    Private Sub App_WorkbookOpen(ByVal Wb As Workbook)
    'do some test if this workbook as referenced by wb is a target wb
    'If target wb then
    Wb.Colors(1) = RGB(15, 15, 15)
    'etc
    'End If
    End Sub

    Private Sub Workbook_Open()
    Set App = Application
    End Sub
    [/vba]

    This is workbook event code.
    To input this code, right click on the Excel icon on the worksheet
    (or next to the File menu if you maximise your workbooks),
    select View Code from the menu, and paste the code
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  9. #9
    VBAX Regular
    Joined
    Jan 2008
    Posts
    28
    Location
    Thanks a lot! It solves my problem.

    ALex

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •