Advanced Color Coding of Multiple cells (CAD sheet)

jnewlin

New Member
Joined
Aug 2, 2007
Messages
18
I'm doing a CAD drawing in Excel and need to reference a cell's named range in VBA. Basically on Sheet1, I enter a number on say A1. Say column 1 is named BaseValues. I want to be able to reference BaseValues when the value in any cell in column 1 is changed. Here is some code I'm working with:

Private Sub Worksheet_Change(ByVal Target As Range)

' Loop through all names in workbook.
For Each n In ActiveWorkbook.Names
Dim b As Integer

' Set Error Handling so macro will not fail when it
' encounters a name that does not refer to a worksheet range.
On Error Resume Next
' Check to see if the name refers to the ActiveSheet.
If Mid(n.RefersTo, 2, InStr(n.RefersTo, "!") - 2) = _
ActiveSheet.Name Then b = 1
Next
If Intersect(Target, Range(n.Name)) Is Nothing Then
Exit Sub
Else
If ActiveCell.Value > 4 Then
For Each d In Range(n.Name).Value
d.Interior.ColorIndex = 0
Next d
ElseIf ActiveCell.Value = 4 Then
For Each d In Range(n.Name).Value
d.Interior.ColorIndex = 3
Next d
ElseIf ActiveCell.Value = 3 Then
For Each d In Range(n.Name).Value
d.Interior.ColorIndex = 44
Next d
ElseIf ActiveCell.Value = 2 Then
For Each d In Range(n.Name).Value
d.Interior.ColorIndex = 6
Next d
ElseIf ActiveCell.Value = 1 Then
For Each d In Range(n.Name).Value
d.Interior.ColorIndex = 4
Next d
Else
For Each d In Range(n.Name).Value
d.Interior.ColorIndex = 0
Next d
End If
End If
End Sub

You can see I'm trying to search through all the names on the worksheet and trying to find which name my cell is a part of. The second part uses that range in changing the color of all cells within that range.

Any suggestions on how to find the named range a cell is a part of and then how to reference it later in the subroutine?
 

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.

Forum statistics

Threads
1,214,827
Messages
6,121,818
Members
449,049
Latest member
cybersurfer5000

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