range error with worksheet object

cahinton

New Member
Joined
Dec 18, 2014
Messages
6
I'm having a problem with a sub for shading a row. I started by recording a macro, then generalizing it as below:
Sub ShadeRow(aSheet As Worksheet, row As Long, nCols As Integer, blueFlag As Boolean)
'
' Shade iRow X cols blue or green
'
With aSheet.Range(Cells(row, 1), Cells(row, nCols)).Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
If (blueFlag) Then
.ThemeColor = xlThemeColorAccent1
Else
.ThemeColor = xlThemeColorAccent6
End If
.TintAndShade = 0.799981688894314
.PatternTintAndShade = 0
End With
End Sub

vba gives a runtime error 1004 indicating the range method failed. The calling routine gets the worksheet object by
Worksheets(strSheetName), so I'm a little stumped. The other arguments are also correct and it makes no difference if I declare the first parameter as ByRef.
thanks in advance!

 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
Hi cahinton

Just to be sure - you know you've created a Sub that is called from another routine, ie this Sub takes parameters/inputs from another routine, and then uses those inputs to then run this sub?

I've run your ShadeRow() sub using the following code:

Code:
Sub CallSub()
Call ShadeRow(Sheets("Sheet1"), 2, 3, False)
End Sub

and

Code:
Sub CallSub()
Call ShadeRow(Sheets("Sheet1"), 2, 3, True)
End Sub

and both times your ShadeRow() has worked.

Consequently, the issue will be in the parameters being passed to ShadeRow(). I would start by looking at the

Code:
aSheet as Worksheet

parameter. Confirm what it is by using

Code:
Debug.Print aSheet.Name

just before you call ShadeRow() in the 'calling' Sub.

Let me know how you get by.

Cheers

pvr928
 
Upvote 0
Try this.
Code:
Sub ShadeRow(aSheet As Worksheet, row As Long, nCols As Integer, blueFlag As Boolean)
'
' Shade iRow X cols blue or green
    With aSheet
        With .Range(Cells(row, 1), Cells(row, nCols)).Interior
            .Pattern = xlSolid
            .PatternColorIndex = xlAutomatic
            If (blueFlag) Then
                .ThemeColor = xlThemeColorAccent1
            Else
                .ThemeColor = xlThemeColorAccent6
            End If
            .TintAndShade = 0.799981688894314
            .PatternTintAndShade = 0
        End With
    End With

End Sub
 
Upvote 0

Forum statistics

Threads
1,224,828
Messages
6,181,206
Members
453,022
Latest member
RobertV1609

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top