Adapted loop causing "Compile Error"

johnnyb5

Board Regular
Joined
Dec 23, 2014
Messages
89
I've researched a loop from a 2009 question and tried to adapt it to select my cells in columns E & F that are greater than zero and highlight them green (if they are 0 then do nothing). Those columns have formulas that are returning values as appropriate without errors. When I tried to adapt the loop I got a compile error that I don't understand how to fix. The error states that "for each control variable must be variant or object" and it highlights the first "c" in the second row. The loop's adapted instructions are:

'Format GYR in table

i = 6 'start row number
For Each c In ActiveSheet.Range("e6:f" & Range("f" & Rows.Count).End(xlUp).Row)
If Cells(i, 5) > 0 Then
Rng = "e" & i & ":" & "f" & i
Range(Rng).Interior.ColorIndex = 4 'green
'If Cells(i, 4) = "" Then
' Cells(i, 4).Interior.ColorIndex = 3 'red
Else
End If
End If
i = i + 1
Next c

Can anyone help with working out the loop errors so that my cells that are greater than zero in column E & F can get colored green?
 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
Do both columns have to be greater than zero to shade both green or can it be either column E or column F?
 
Upvote 0
Indenting code properly is VERY important. In this case, you have an extra End If. Delete either one of them.

Tho, noticing you have commented code out, I would probably comment out the first end if. And the Else above it
 
Upvote 0
johnnyb5,

If I understand you correctly, here is another macro solution for you to consider.

Sample raw data:


Excel 2007
DEF
612
734
856
900
10
1178
1289
13
Sheet1


After the macro:


Excel 2007
DEF
612
734
856
900
10
1178
1289
13
Sheet1


Please TEST this FIRST in a COPY of your workbook (always make a backup copy before trying new code, you never know what you might lose).

Code:
Sub FormatGYR_in_table()
' hiker95, 09/16/2015, ME887326
Dim c As Range
Application.ScreenUpdating = False
For Each c In ActiveSheet.Range("E6:E" & Range("F" & Rows.Count).End(xlUp).Row)
  If c.Value > 0 Then c.Interior.ColorIndex = 4 'green
  If c.Offset(, 1).Value > 0 Then c.Offset(, 1).Interior.ColorIndex = 4 'green
  If c.Offset(, -1) = vbEmpty Then c.Offset(, -1).Interior.ColorIndex = 3 'red
Next c
Application.ScreenUpdating = True
End Sub

Before you use the macro with Excel 2007 or newer, save your workbook, Save As, a macro enabled workbook with the file extension .xlsm, and, answer the "do you want to enable macros" question as "yes" or "OK" (depending on the button label for your version of Excel) the next time you open your workbook.

Then run the FormatGYR_in_table macro.
 
Upvote 0
Hiker 95,

Thanks so much for the improved code. I tried endless corrections and edits but I could not solve the "Compile" problem. Even when I tried to use your improved code I still got the "Compile" error however, I was able to simply switch the "C" in your code with a "Z" in all the lines and then it worked like a charm without any errors. You hit the nail on the head and I certainly appreciate both your assistance and your patience!!! My macro is working fine now and coloring my lines just as intended.

Sincerely,
JB
 
Upvote 0
Hiker 95,

Thanks so much for the improved code. I tried endless corrections and edits but I could not solve the "Compile" problem. Even when I tried to use your improved code I still got the "Compile" error however, I was able to simply switch the "C" in your code with a "Z" in all the lines and then it worked like a charm without any errors. You hit the nail on the head and I certainly appreciate both your assistance and your patience!!! My macro is working fine now and coloring my lines just as intended.

Sincerely,
JB

johnnyb5,

Thanks for the feedback.

You are very welcome.

Glad I could help you, so that you could find a solution that worked correctly.

And, come back anytime.
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,286
Members
452,631
Latest member
a_potato

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