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
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.
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 | ||||||||
---|---|---|---|---|---|---|---|---|
F | G | H | I | J | K | |||
8 | ||||||||
9 | Sample Data | |||||||
10 | ||||||||
11 | ||||||||
12 | ||||||||
13 | ||||||||
14 | ||||||||
15 | Result Required | |||||||
16 | ||||||||
17 | ||||||||
18 | ||||||||
19 | ||||||||
20 | ||||||||
21 | Wrong Result | |||||||
22 | ||||||||
23 | ||||||||
Sheet2 |
Last edited by a moderator: