Macro for breaking list into sections using underline

jdmc45

Board Regular
Joined
May 8, 2011
Messages
146
Office Version
  1. 365
Platform
  1. Windows
Hi,

I am trying to create a macro to put a top border on the row when a selection in a row changes. There are only 3 selections so will be 2 lines per worksheet. So, Column C will be

Apples
Apples
Apples
Oranges
Oranges
Bananas
Bananas
Bananas
Bananas

I would like to have a line between Apples and Oranges and Oranges and Bananas, that is, whenever the value in the column changes. My code thus far is:

Code:
Sub Line()
Dim M As Long, i As Long
M = Range("C4:C2000")
For i = 0 To M
If Range("C" & i + 4) = "Apples" Then i = i + 1
If Range("C" & (i - 1) + 4) = "Apples" And Range("C" & i + 4) = "Oranges" Then
    
Range("C" & (i - 1) + 4).Select
    With Selection.Borders(xlEdgeTop)
        .LineStyle = xlContinuous
        .ColorIndex = 0
        .TintAndShade = 0
        .Weight = xlThin
    End With
    
Next i
     
If Range("C" & i + 4) = "Oranges" Then i = i + 1
If Range("C" & (i - 1) + 4) = "Oranges" And Range("C" & i + 4) = "Bananas" Then
Range("C" & (i - 1) + 4).Select
    With Selection.Borders(xlEdgeTop)
        .LineStyle = xlContinuous
        .ColorIndex = 0
        .TintAndShade = 0
        .Weight = xlThin
    End With
End If
End Sub

Any help will be much appreciated!!
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
Hello jdmc45,

This will start will cell "C4" and go down column "C" to the last cell that has as value. Each group will be underlined.
Code:
Sub UnderlineGroups()

   Dim Cell As Range
   Dim Prev As Variant
   Dim Rng As Range
   Dim RngEnd As Range
   Dim Wks As Worksheet
   
     Set Wks = ActiveSheet
     
     Set Rng = Range("C4")
     Set RngEnd = Wks.Cells(Rows.Count, Rng.Column).End(xlUp)
     If RngEnd.Row < Rng.Row Then Exit Sub Else Set Rng = Wks.Range(Rng, RngEnd)
     
       For Each Cell In Rng
         If IsEmpty(Prev) Then Prev = Cell.Value
         If Cell.Value <> Prev Then
            Prev = Cell.Value
            With Cell.Borders(xlEdgeTop)
              .LineStyle = xlContinuous
              .Weight = xlThin
            End With
         End If
       Next Cell
   
End Sub

Sincerely,
Leith Ross
 
Upvote 0
Thanks so much for that Leith! Just one more thing, I wasn't specific enough but I would actually like to create the under/overline for rows C to J, what extra code is needed to perform this?

Thanks again for your time,

James
 
Upvote 0
So to confirm, the condition depends on column C, the under/over line covers rows C to J.

:)
 
Upvote 0
Hello jdmc45,

Sorry, I was feeding my cats. This will extend the line across to column "J".
Rich (BB code):
Sub UnderlineGroups()

   Dim Cell As Range
   Dim Prev As Variant
   Dim Rng As Range
   Dim RngEnd As Range
   Dim Wks As Worksheet
   
     Set Wks = ActiveSheet
     
     Set Rng = Range("C4")
     Set RngEnd = Wks.Cells(Rows.Count, Rng.Column).End(xlUp)
     If RngEnd.Row < Rng.Row Then Exit Sub Else Set Rng = Wks.Range(Rng, RngEnd)
     
       For Each Cell In Rng
         If IsEmpty(Prev) Then Prev = Cell.Value
         If Cell.Value <> Prev Then
            Prev = Cell.Value
            With Cell.Resize(ColumnsSize:=7).Borders(xlEdgeTop)
              .LineStyle = xlContinuous
              .Weight = xlThin
            End With
         End If
       Next Cell
   
End Sub</pre>
 
Upvote 0
You an are excel legend Leith! Thank you!

For anyone else who wants to use this code, this is a tiny syntax error, the column sizing line of code should read

Code:
With Cell.Resize(ColumnSize:=8).Borders(xlEdgeTop)

It is Column, not Columns, and it is inclusive of column c so the ColumnSize will be 8.

Thanks again!
 
Upvote 0
Hello jdmc45,

Oops, sorry about that. Nice catch and correction.
 
Upvote 0

Forum statistics

Threads
1,224,595
Messages
6,179,799
Members
452,943
Latest member
Newbie4296

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