Object Required error

claudehollett

Board Regular
Joined
Dec 11, 2003
Messages
89
CellColor Dim As Range
CellColor = Dates!A2

The code above returns an error "Object Required".
I am trying to get the range Dates!A2 into the variable CellColor. What am I doing wrong?
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
Even placing "Set CellColor" in the code still returns the error.
Here is my complete code:

Private Sub Workbook_SheetCalculate(ByVal Sh As Object)

'This code generates totals on the active sheet. All cell's with an Interior Color of light green
'will total in the Exempt total. All with no fill will total in Taxable Total
Dim myCell As Range
Dim iCol As Integer
Dim myTotal
Dim CellColor As Range
Dim SumRange As Range
Dim FinalRow As Range
Set FinalRow = Range("A65536").End(xlUp)
FinalRow = Range("A65536").End(xlUp).Row
Set CellColor = Dates!A2
SumRange = Range(Cells(4, "AI"), Cells(FinalRow - 3, "AI"))
iCol = CellColor.Interior.ColorIndex 'get the target color
For Each myCell In SumRange 'look at each cell in the designated range
If myCell.Interior.ColorIndex = iCol Then 'if the cell color matches the target Color
myTotal = WorksheetFunction.Sum(myCell) + myTotal 'add the value in the cell to the total
End If
Next myCell
Cells(FinalRow - 1, "AI") = myTotal

End Sub
 
Upvote 0
If you want to refer to cell A2 on a worksheet called Dates you need something like this.
Code:
 Set CellColor = Worksheet("Dates").Range("A2")
I'm not 100% sure how VBA will interpret Dates!A2.
 
Upvote 0
Using this code returns a compile error "Sub or Function not defined". The word "Worksheet" is highlighted in blue.
 
Upvote 0
Whoops.:oops:

Worksheet should be Worksheets.
 
Upvote 0

Forum statistics

Threads
1,217,677
Messages
6,137,941
Members
450,100
Latest member
mscetr

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