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

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
@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,215,073
Messages
6,122,975
Members
449,095
Latest member
Mr Hughes

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