Apply case to named range

Sumeluar

Active Member
Joined
Jun 21, 2006
Messages
274
Office Version
  1. 365
  2. 2016
  3. 2010
Platform
  1. Windows
  2. MacOS
  3. Mobile
Hello all - I've looking al over for a solution to my dilemma at no avail. After extensive Google search I found the below code which is applying Case to anything on column "C" which is not ideal for my need, the question is: Can I get someone's help to modify the code that only applies to Named Ranges "Sub_Task_1", "Sub_Task_2", "Sub_Task_3" and "Sub_Task_4"? Those four ranges are all on column "C".



Sub Indentation()

Dim ws As Worksheet
Dim i As Long
Dim lastrow As Long
Set ws = Worksheets("2022")
lastrow = ws.Cells(Rows.Count, "C").End(xlUp).Row
For i = 1 To lastrow
Select Case ws.Range("C" & i).IndentLevel

Case 0
ws.Range("C" & i).Font.Bold = False
ws.Range("C" & i).Font.ColorIndex = 1
ws.Range("C" & i).Interior.Color = RGB(255, 255, 255)

Case 1
ws.Range("C" & i).Font.ColorIndex = 3
ws.Range("C" & i).Font.Bold = True
ws.Range("C" & i).Interior.Color = RGB(221, 235, 247)

Case 2
ws.Range("C" & i).Font.ColorIndex = 5
ws.Range("C" & i).Font.Bold = False
ws.Range("C" & i).Interior.Color = RGB(242, 242, 242)

Case 3
ws.Range("C" & i).Font.ColorIndex = 7
ws.Range("C" & i).Font.Bold = False

Case 4
ws.Range("C" & i).Font.ColorIndex = 4
ws.Range("C" & i).Font.Bold = False

End Select
Next
End Sub

Thank you!
Any assistance is greatly appreciated.
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
Hi,
untested but see if this update to your code will do what you want

VBA Code:
Sub Indentation()
    Dim rng As Range, cell As Range
    Dim i   As Long, indent As Long
   
    Set rng = Range("Sub_Task_1,Sub_Task_2,Sub_Task_3,Sub_Task_4")
   
    For i = 1 To rng.Areas.Count
      For Each cell In rng.Areas(i).Cells
       indent = cell.IndentLevel
       indent = IIf(indent > 4, 4, indent) + 1
        With cell
            .Font.Bold = Choose(indent, True, False, False, False)
            .Font.ColorIndex = Choose(indent, 1, 3, 5, 7, 4)
            .Interior.Color = Choose(indent, RGB(221, 235, 247), RGB(221, 235, 247), RGB(242, 242, 242), _
                                             RGB(221, 235, 247), RGB(221, 235, 247))
        End With
      Next cell
    Next i
   
End Sub

Adjust code as required to meet specific project need

Dave
 
Upvote 0
Maybe something like this (not tested).
VBA Code:
Sub Indentation()

    Dim ws As Worksheet
    Dim TestRange As Range, R As Range

    Set ws = Worksheets("2022")

    Set TestRange = Application.Union(ws.Range("Sub_Task_1"), ws.Range("Sub_Task_2"), ws.Range("Sub_Task_3"), ws.Range("Sub_Task_4"))
    Debug.Print "Test Range: " & TestRange.Address
    For Each R In TestRange
        Debug.Print R.Address
            Select Case R.IndentLevel

            Case 0
                R.Font.Bold = False
                R.Font.ColorIndex = 1
                R.Interior.Color = RGB(255, 255, 255)

            Case 1
                R.Font.ColorIndex = 3
                R.Font.Bold = True
                R.Interior.Color = RGB(221, 235, 247)

            Case 2
                R.Font.ColorIndex = 5
                R.Font.Bold = False
                R.Interior.Color = RGB(242, 242, 242)

            Case 3
                R.Font.ColorIndex = 7
                R.Font.Bold = False

            Case 4
                R.Font.ColorIndex = 4
                R.Font.Bold = False
            End Select
    Next R
End Sub

(Tip: when posting code, please try to use 'code tags' to format the code as I have done above
as it makes the code easier to read.)
 
Upvote 0
Hi,
untested but see if this update to your code will do what you want

VBA Code:
Sub Indentation()
    Dim rng As Range, cell As Range
    Dim i   As Long, indent As Long
  
    Set rng = Range("Sub_Task_1,Sub_Task_2,Sub_Task_3,Sub_Task_4")
  
    For i = 1 To rng.Areas.Count
      For Each cell In rng.Areas(i).Cells
       indent = cell.IndentLevel
       indent = IIf(indent > 4, 4, indent) + 1
        With cell
            .Font.Bold = Choose(indent, True, False, False, False)
            .Font.ColorIndex = Choose(indent, 1, 3, 5, 7, 4)
            .Interior.Color = Choose(indent, RGB(221, 235, 247), RGB(221, 235, 247), RGB(242, 242, 242), _
                                             RGB(221, 235, 247), RGB(221, 235, 247))
        End With
      Next cell
    Next i
  
End Sub

Adjust code as required to meet specific project need

Dave
Dave - Thank you for the code, it works good.
 
Upvote 0
Maybe something like this (not tested).
VBA Code:
Sub Indentation()

    Dim ws As Worksheet
    Dim TestRange As Range, R As Range

    Set ws = Worksheets("2022")

    Set TestRange = Application.Union(ws.Range("Sub_Task_1"), ws.Range("Sub_Task_2"), ws.Range("Sub_Task_3"), ws.Range("Sub_Task_4"))
    Debug.Print "Test Range: " & TestRange.Address
    For Each R In TestRange
        Debug.Print R.Address
            Select Case R.IndentLevel

            Case 0
                R.Font.Bold = False
                R.Font.ColorIndex = 1
                R.Interior.Color = RGB(255, 255, 255)

            Case 1
                R.Font.ColorIndex = 3
                R.Font.Bold = True
                R.Interior.Color = RGB(221, 235, 247)

            Case 2
                R.Font.ColorIndex = 5
                R.Font.Bold = False
                R.Interior.Color = RGB(242, 242, 242)

            Case 3
                R.Font.ColorIndex = 7
                R.Font.Bold = False

            Case 4
                R.Font.ColorIndex = 4
                R.Font.Bold = False
            End Select
    Next R
End Sub

(Tip: when posting code, please try to use 'code tags' to format the code as I have done above
as it makes the code easier to read.)
rlv01 - That is exactly what I was looking for, it works like a charm, thank you!
 
Upvote 0

Forum statistics

Threads
1,223,711
Messages
6,174,025
Members
452,542
Latest member
Bricklin

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