Macro to FIND and REPLACE cell's background color only

Sathya89

New Member
Joined
Jun 6, 2012
Messages
18
Office Version
  1. 365
Platform
  1. Windows
After lot of web search and editing, i came up with the following codes to find and replace the cell background color but there are few issues. i'm not good at macros.I'm a beginner with these codes.

1)Time consuming for bigger ranges say 150rows x 300columns
2)It doesn't work as per my requirement for certain conditions.

Here is the code.Please suggest a better code

VBA Code:
Sub ReplaceTheColor()
Dim NewColor As Range
Dim OldColor As Range
Dim Wrange As Range ' Working Range

On Error GoTo ErrHandler

'I will change the color in Range("A7")
Set NewColor = Range("A7")

I will select the from working range what color to be changed
Set OldColor = Application.InputBox("Old Color", "Select The Cell that has the color you want to replace", Type:=8)

'setting my working range
n = Range("C" & Rows.Count).End(xlUp).Row - 1
rc = Cells(8, Columns.Count).End(xlToLeft).Column
Set Wrange = Range(Cells(10, 5), Cells(n, rc))

'Checks the number of cells of each range before applying the action
If NewColor.Cells.Count = 1 And OldColor.Cells.Count = 1 And Wrange.Cells.Count > 0 Then

    NewClr = getRGB2(NewColor)
    Oldclr = getRGB2(OldColor)
    NewColorVars = Split(NewClr, ",")

'I hope this is the part where the codes to be modified.
    
    For Each cell In Wrange
        If getRGB2(cell) = Oldclr Then cell.Interior.Color = RGB(NewColorVars(0), NewColorVars(1), NewColorVars(2))
    Next

Else
GoTo ErrHandler
End If

Exit Sub

ErrHandler:
MsgBox "Wrong Ranges were Selected!" & vbCrLf & "============================" & vbCrLf & "New Color: 1 Cell only" & vbCrLf & "Old Color: 1 Cell only" & vbCrLf & "Replace Range: 1 cell or more", vbCritical, "Error"
End Sub

'This is private function to identity the RGB Number of old and new color (Got it after web search)

Private Function getRGB2(rcell) As String
    Dim C As Long
    Dim R As Long
    Dim G As Long
    Dim B As Long

    C = rcell.Interior.Color
    R = C Mod 256
    G = C \ 256 Mod 256
    B = C \ 65536 Mod 256
    getRGB2 = R & "," & G & "," & B
End Function

The issue is if i want to replace only horizontal borders/Vertical borders based cell color,it should replace cell background of the horizontal borders/Vertical border respectively.Following is the example of what result i required.The above codes are working fine when i just want to replace the cell background color with new color but it is time consuming for larger ranges too.

I hope the vertical and horizontal borders are visible.Thanks in advance.


Weaving design soft.xlsm
FGHIJK
8
9Sample Data
10
11
12
13
14
15Result Required
16
17
18
19
20
21Wrong Result
22
23
Sheet2
 
Last edited by a moderator:

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney
i tired to use the findformat and replaceformat of vba..it is not working
 
Upvote 0

Forum statistics

Threads
1,223,243
Messages
6,170,971
Members
452,371
Latest member
Frana

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