How do I remove formatting from the non-active area of a spreadsheet?

Zenwood

Board Regular
Joined
Sep 2, 2017
Messages
67
I use this macro on many different spreadsheets and would like to remove the excess formatting. If you look at the sample spreadsheet you see the highlight goes row by row all the way across. I only want the formatting to apply to the active area. In the case of the sample spreadsheet columns A to C.

Can I modify the Macro to format the active area or can I remove the excess formatting?

Sub GroupRowA_byColor()


' 10.23.17



Dim h, i, j, k, s, r, g, b As Integer

h = 1 ' Find the end of this column
i = 1 ' Compare items in the column
j = 2 ' Start with this row
k = 240 ' Start with this color

s = k ' same color

r = 205 ' Base Red
g = 255 ' Base Green
b = 0 ' Base Blue

Do While (Cells(j, h) <> "")

If (Cells(j, i) <> "") And (Cells(j - 1, i) = Cells(j, i)) Then
k = k ' keep the color on match
Else

If k = s Then
k = k - 50 ' Change the color
Else
k = k + 50 ' Back to original color
End If

End If

Rows(j).Interior.Color = RGB(r, g, k)


j = j + 1

Loop

End Sub

-------
Spreadsheet sample

[TABLE="width: 256"]
<colgroup><col width="64" span="4" style="width:48pt"> </colgroup><tbody>[TR]
[TD="class: xl63, width: 64"]Order[/TD]
[TD="class: xl63, width: 64"]Item[/TD]
[TD="class: xl63, width: 64"]Quantity[/TD]
[TD="class: xl64, width: 64"] [/TD]
[/TR]
[TR]
[TD="class: xl65"]100[/TD]
[TD="class: xl65"]stuff.1[/TD]
[TD="class: xl65"]1[/TD]
[TD="class: xl65"] [/TD]
[/TR]
[TR]
[TD="class: xl65"]100[/TD]
[TD="class: xl65"]stuff.1[/TD]
[TD="class: xl65"]2[/TD]
[TD="class: xl65"] [/TD]
[/TR]
[TR]
[TD="class: xl65"]100[/TD]
[TD="class: xl65"]stuff.1[/TD]
[TD="class: xl65"]3[/TD]
[TD="class: xl65"] [/TD]
[/TR]
[TR]
[TD="class: xl66"]200[/TD]
[TD="class: xl66"]stuff.2[/TD]
[TD="class: xl66"]4[/TD]
[TD="class: xl66"] [/TD]
[/TR]
[TR]
[TD="class: xl66"]200[/TD]
[TD="class: xl66"]stuff.2[/TD]
[TD="class: xl66"]5[/TD]
[TD="class: xl66"] [/TD]
[/TR]
[TR]
[TD="class: xl66"]200[/TD]
[TD="class: xl66"]stuff.2[/TD]
[TD="class: xl66"]6[/TD]
[TD="class: xl66"] [/TD]
[/TR]
[TR]
[TD="class: xl65"]300[/TD]
[TD="class: xl65"]stuff.3[/TD]
[TD="class: xl65"]7[/TD]
[TD="class: xl65"] [/TD]
[/TR]
</tbody>[/TABLE]
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
This is formatting the entire row
Code:
Rows(j).Interior.Color = RGB(r, g, k)

Change it to
Code:
Range("A" & j & ":C" & j).Interior.Color = RGB(r, g, k)
 
Upvote 0
Instead of using...

Code:
Rows(j).Interior.Color = RGB(r, g, k)

do...

Code:
Range(Cells(j, 1), Cells(j, 3)).Interior.Color = RGB(r, g, k)
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,177
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