Excel VBA Formatting Cells in a Different Workbook

Thank you for looking at my question,

I am trying to apply some formatting to cells in a different workbook than the user is working in.

I want to fill the header row with a colour to differentiate header from data.

The number of columns in the target worksheet depends on a value set by the user (intPeriodRange)

The code I have tried is as below but it doesn't work - how should I change it so that the colour fill works please?


    With sheetobject.Range(Cells(1, 1), Cells(2, intPeriodRange + 6)).Interior
        .Pattern = xlSolid
        .PatternColorIndex = xlAutomatic
        .ThemeColor = xlThemeColorAccent5
        .TintAndShade = 0.599963377788629
        .PatternTintAndShade = 0
    End With
Gary CroxfordOperations Support AnalystAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Rgonzo1971Commented:
Hi,

pls try

    With sheetobject.Range(Cells(1, 1), Cells(1, intPeriodRange + 6)).Interior
         .Pattern = xlSolid
         .PatternColorIndex = xlAutomatic
         .ThemeColor = xlThemeColorAccent5
         .TintAndShade = 0
         .PatternTintAndShade = 0
    End With
    With sheetobject.Range(Cells(2, 1), Cells(2, intPeriodRange + 6)).Interior
         .Pattern = xlSolid
         .PatternColorIndex = xlAutomatic
         .ThemeColor = xlThemeColorAccent5
         .TintAndShade = 0.599963377788629
         .PatternTintAndShade = 0
    End With
End Sub

Open in new window

Regards
Gary CroxfordOperations Support AnalystAuthor Commented:
Rgonzo,

this didn't make any change
Rgonzo1971Commented:
Could you send a dummy? with actual and desired result
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

Gary CroxfordOperations Support AnalystAuthor Commented:
Rgonzo,

Have attached the file that is output (Actual), how I hoped it would look (Desired), the code that produces the result and the source data
Period-Issue---Actual.xlsx
Period-Issue---Desired.xlsx
Process-Period-Issue-Data.xlsm
PeriodIssueData.csv
Rgonzo1971Commented:
then try

    With sheetobject.Range(Cells(1, 1), Cells(2, intPeriodRange + 6)).Interior
         .Pattern = xlSolid
         .Color = RGB(142, 169, 219)
    End With

Open in new window

Gary CroxfordOperations Support AnalystAuthor Commented:
RGonzo1971,

This didn't make any change either.

Using
    With sheetobject.range("A1:Q2").Interior
         .Pattern = xlSolid
         .Color = RGB(142, 169, 219)
    End With

does apply the colour format but this isn't dynamic
Rgonzo1971Commented:
on you code you use

On Error Resume Next this not be used for all the code you cannot see where error appears

the line MyExcelFilePath = "C:\YourFolder\Period-Issue.xlsx" has a blank instead of -
No error and sheetobject not created since On error resume next

pls revise your code
Gary CroxfordOperations Support AnalystAuthor Commented:
Rgonzo1971,

The dashes are put into the filenames when they are uploaded to EE.

The line MyExcelFilePath = "C:\YourFolder\Period Issue.xlsx"  should have a blank.

I know this because there has already been a lot of processing on the data before we get to the issue of formatting the target file and that runs fine.

I have removed the On Error Resume Next statement
Rgonzo1971Commented:
Hi,

pls try

With sheetobject.Range(sheetobject.Cells(1, 1), sheetobject.Cells(2, intPeriodRange + 6)).Interior
         .Pattern = xlSolid
         .Color = RGB(142, 169, 219)
    End With

Open in new window

Rgonzo1971Commented:
or

    With sheetobject
        With .Range(.Cells(1, 1), .Cells(2, intPeriodRange + 6)).Interior
             .Pattern = xlSolid
             .Color = RGB(142, 169, 219)
        End With
    End With

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Gary CroxfordOperations Support AnalystAuthor Commented:
Bingo! Thank you for your help
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Excel

From novice to tech pro — start learning today.