Copy the cell fill color to the adjacent call, VBA code HELP, "object required" error

lmuller92

New Member
Joined
Aug 17, 2016
Messages
13
Hello, i use excell 2010

My conditional formatting so far will color code names e.g. all cells that say mike have a blue fill color.... no problem that was easy.

But i need the cells to the left of my names column to have the same fill color, e.g. cell f2 is blue and says mike, and i want e2 to recognize that the cell is blue in the adjacent cell and fill its self with the same color. e2 will have a calculated % value.

here is the code i have so far but i keep getting object required, any advise?!

Sub copyValuesAndFormats()
Dim intRow As Integer
Dim rngCopy As Range
Dim rngPaste As Range




'Loop from Rows 1 through 35
For intRow = 1 To 35


'Set the copy and paste range
'CHANGE THE SHEET TO MATCH YOURS
Set rngCopy = DataInColumn.Range("F" & intRow + 1)
Set rngPaste = DataInColumn.Range("E" & intRow + 1)


'Test to see if rows have a value
If rngCopy.Text <> "" Then


'Since it has a value, copy the value and color
rngPaste.Interior.Color = rngCopy.Interior.Color


'If you wish to copy the color of the font as well, uncomment the next line
'rngPaste.Font.Color = rngCopy.Font.Color
End If
Next intRow
End Sub
 
I really appreciate the quick responses, but now i am getting "compile error: expected: end of statement" referring to the . after the sheet name

Set rngCopy = ("DataInColumn").Range("F" & intRow + 1)
Set rngPaste = ("DataInColumn").Range("E" & intRow + 1)
............................................^..its that period that is highlighted with the error

Never mind i saw why i had the compile error, but nothing happens when i run the macro
 
Upvote 0

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
You are missing the word Sheets as in the code I posted.
 
Upvote 0
If using 2010 or later because you are using conditional formatting change...

Code:
rngPaste.Interior.Color = rngCopy.Interior.Color
to
Code:
rngPaste.Interior.Color = rngCopy.FormatConditions(1).Interior.Color

but as Rick has stated you can do this without VBA.
 
Last edited:
Upvote 0
If using 2010 or later because you are using conditional formatting change...

Code:
rngPaste.Interior.Color = rngCopy.Interior.Color
to
Code:
rngPaste.Interior.Color = rngCopy.FormatConditions(1).Interior.Color

but as Rick has stated you can do this without VBA.

Thank you it almost work now! what happened when i ran it:

F2 was brown, i ran the macro and E2 remained white but E3-E35 are all brown. so close!
 
Upvote 0
Code:
rngCopy.Offset(,-1).Interior.Color = rngCopy.FormatConditions(1).Interior.Color
 
Last edited:
Upvote 0
Code:
rngCopy.Offset(,-1).Interior.Color = rngCopy.FormatConditions(1).Interior.Color

hmm it works but the whole column E becomes brown, i can make it so that only F2 and E2 are the same but the macro wont continue down the sheet, i attchived this by changing

'Loop from Rows 1 through 35For intRow = 2 To 35

to

'Loop from Rows 1 through 35
For intRow = 2 To 2



i need E2 to match the color in F2, E3 to match F3 and so on down the list

here is the entire code updated i also removed the + 1 were they appeared


Code:
Sub copyValuesAndFormats()Dim intRow As Integer
Dim rngCopy As Range
Dim rngPaste As Range








'Loop from Rows 1 through 35
For intRow = 2 To 35




'Set the copy and paste range
'CHANGE THE SHEET TO MATCH YOURS
Set rngCopy = Sheets("DataInColumn").Range("F" & intRow)
 Set rngPaste = Sheets("DataInColumn").Range("E" & intRow)




'Test to see if rows have a value
If rngCopy.Text <> "" Then




'Since it has a value, copy the value and color
rngCopy.Offset(, -1).Interior.Color = rngCopy.FormatConditions(1).Interior.Color




'If you wish to copy the color of the font as well, uncomment the next line
'rngPaste.Font.Color = rngCopy.Font.Color
End If
Next intRow
End Sub
 
Upvote 0
OK, I can see the issue but not easy to test if the condition is being applied.

The easy way around it is to evaluate the condition again (but then you might as well follow Rick's suggestion... notice there is a pattern here).

And wrap your formula in
IFERROR(YOUR FORMULA,"")
 
Last edited:
Upvote 0

Forum statistics

Threads
1,221,310
Messages
6,159,176
Members
451,543
Latest member
cesymcox

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