Assistance Using VBA and Mod for Shading

JarrydBarnard

New Member
Joined
Jun 11, 2023
Messages
4
Office Version
  1. 365
Platform
  1. Windows
Hi there,

I've got a set of numbers from 1 - 100 in cells A1 - J10, generated by VBA, and am needing to shade the cells containing numbers divisible by 7, but I have to use the Mod code.

My current code is:

Dim Counter_Row As Integer
Dim Column_Row As Integer
Dim val As Integer
val = 1

For Column_Row = 1 To 10

For Counter_Row = 1 To 10
Cells(Counter_Row, Column_Row).Value = val
val = val + 1
Next Counter_Row
Next Column_Row

End Sub

How would I do this with an If function? So every cell that contains a multiple of 7 needs to be shaded red. I've tried so many different ways but just can't get it right. Please help
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
@JarrydBarnard Welcome.

Does this help?

VBA Code:
Dim Counter_Row As Integer
Dim Column_Row As Integer
Dim val As Integer
val = 1

For Column_Row = 1 To 10

For Counter_Row = 1 To 10
Cells(Counter_Row, Column_Row).Value = val
If val Mod 7 = 0 Then Cells(Counter_Row, Column_Row).Interior.Color = vbRed
val = val + 1
Next Counter_Row
Next Column_Row
 
Upvote 1

Forum statistics

Threads
1,224,820
Messages
6,181,155
Members
453,021
Latest member
Justyna P

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