Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

runtime error 91 trying to set color of cells 1

Status
Not open for further replies.

vba317

Programmer
Mar 5, 2009
708
US
I am trying to write a module that changes the color of specific cells. I keep on getting this error 91. I have highlighted in blue where the error is. I do have a blank spreadsheet open currently. Any help would be appreciated.

Code:
Public Sub xLFmtFillColorRed(intTopRow As Integer, intBotRow As Integer, intLCol As Integer, intRCol As Integer)
' ****************************************************************************************************
' *** THIS SUB FILLS IN THE COLOR RED FOR A RANGE OF CELLS ON ACTIVE OBJECT ***
' ****************************************************************************************************
'Call xLFmtFillColorRed(intTopRow, intBotRow, intLCol, intRCol)
    Dim strLeftLetter As String
    Dim strRightLetter As String
    Dim intRow As Integer
    strLeftLetter = ConvColLet(intLCol)
    strRightLetter = ConvColLet(intRCol)
    For intRow = intTopRow To intBotRow
 [blue]    goXL.ActiveSheet.Range("" & (strLeftLetter) & (intRow) & ":" & (strRightLetter) & (intRow) & "").Select [/blue]
        With Selection.Interior
            .ColorIndex = 4
            .Pattern = xlSolid
            .PatternColorIndex = xlAutomatic
        End With
    Next intRow
End Sub
 
Is that a VBA in Excel?
If so, what is [blue][tt]goXL.[/tt][/blue] ?

And what are the values of: strLeftLetter, strRightLetter, intRow ?

Have fun.

---- Andy
 
Public goXL As Excel.Application ' The Excel Object variable
intTopRow = 3
intBotRow = 5
intLCol = 4
intRCol = 7


Call xLFmtFillColorRed(intTopRow, intBotRow, intLCol, intRCol)
So intLCol is D and intRCol is G

Tom
 
So you call it from Access, that's why you have goXL As Excel.Application
So I assume you set the goXL prior to calling this Sub.

This works for me, the cells do change to Green:

Code:
Dim intRow As Integer
Dim strLeftLetter As String
Dim strRightLetter As String

intRow = 7
strLeftLetter = "B"
strRightLetter = "D"

With goXL
  .ActiveSheet.Range(strLeftLetter & intRow & ":" & strRightLetter & intRow).Select
  With .Selection.Interior
    .ColorIndex = 4
    .Pattern = xlSolid
    .PatternColorIndex = xlAutomatic
  End With
End With

Have fun.

---- Andy
 
What do you mean by setting goXL? I am still getting the error so I must not be. How do you set it?

Tom
 
You stated: "I keep on getting this error 91" - do you have any description of this error? What does it say?

You declared:[tt]
Public goXL As Excel.Application [/tt]

What do you do with [tt]goXL[/tt] before you get to the (blue) line of code you error on?

Have fun.

---- Andy
 
The error I am getting is Runtime error 91. Object variable or with block variable not set. Just a thought. This is a newly created database. Is there a reference that might need to be set? I am not doing anything with goXL other that what I have in the code you see.

Tom
 
Andy,
I have found the error of my ways thanks to your question. Under normal circumstances I call a routine called XL open which does this command - Set dBase = CurrentDb. I have added this routine back in and everything works.

Thanks
Tom
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top