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

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.

Andrew Poulsom

MrExcel MVP
Joined
Jul 21, 2002
Messages
73,092
You need the Set keyword when assigning an object to a variable:

Set CellColor = Dates!A2
 

claudehollett

Board Regular
Joined
Dec 11, 2003
Messages
89
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
 

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
76,300
Office Version
  1. 365
Platform
  1. Windows
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.
 

claudehollett

Board Regular
Joined
Dec 11, 2003
Messages
89
Using this code returns a compile error "Sub or Function not defined". The word "Worksheet" is highlighted in blue.
 

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
76,300
Office Version
  1. 365
Platform
  1. Windows
Whoops.:oops:

Worksheet should be Worksheets.
 

Forum statistics

Threads
1,137,062
Messages
5,679,392
Members
419,825
Latest member
MegastarMagus

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
Top