Solved

Using a macro to generate segregation lines

Posted on 2014-12-30
5
128 Views
Last Modified: 2014-12-30
I am trying to create a macro that will generate a segregation line between each Rep's business (column A).  (see example - Sheet 2).
The sample attached has about 100 lines of data and three or four Reps.  However, my real data could contain upwards of 4,000 lines.  Also, I would like to automatically create a RED border around column F and columns Z, AA (as one double column).  The problem I am having, is that each time I generate the data, the number of lines changes.  I would want the macro to decide where the end of the data is and create the borders based on the end of the data.

Sheet 1 contains the data as it appears today.  
Sheet 2 contains the data with a sample of how I am trying to 'auto format'.

Any help or guidance would be greatly appreciated.

Thanks.
0
Comment
Question by:Sabealgo
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 18

Expert Comment

by:Simon
ID: 40523813
Please post the sample worksheet you described in your question.
0
 

Author Comment

by:Sabealgo
ID: 40523818
It is attached now.
Sample-Data.xlsx
0
 
LVL 47

Expert Comment

by:Martin Liss
ID: 40523873
Where do you want the top of the red border to be? Row 1? Row 2? Row 3?
0
 
LVL 18

Accepted Solution

by:
Simon earned 500 total points
ID: 40523893
This will do it. Paste the code block into a module. It contains the 'AddBorders' sub and a separate sub to apply the red outline formatting. It uses the 'resize' and 'offset' functions to omit the title rows. You can change the values for the number of rows to exclude as required.

Sub AddBorders()
Const NumberOfTitleRows As Integer = 2
Dim usedRng As Range
Dim checkRng As Range
Dim formatRng As Range
Set usedRng = ActiveSheet.UsedRange
Set checkRng = Intersect(usedRng, Columns(1))
Set checkRng = checkRng.Resize(checkRng.Rows.Count - NumberOfTitleRows).Offset(NumberOfTitleRows, 0)
For Each c In checkRng
    If c.Value <> "" Then
    With Intersect(c.EntireRow, usedRng).Borders(xlEdgeTop)
        .LineStyle = xlContinuous
        .TintAndShade = 0
        .Weight = xlThick
    End With
    End If
Next

Set formatRng = Intersect(usedRng, Columns("F"))
Set formatRng = formatRng.Resize(formatRng.Rows.Count - 1).Offset(1, 0)
Call RedOutlineFormat(formatRng)

Set formatRng = Intersect(usedRng, Columns("z:aa"))
Set formatRng = formatRng.Resize(formatRng.Rows.Count - 1).Offset(1, 0)
Call RedOutlineFormat(formatRng)
End Sub


Sub RedOutlineFormat(rng As Range)
    rng.Borders(xlDiagonalDown).LineStyle = xlNone
    rng.Borders(xlDiagonalUp).LineStyle = xlNone
    With rng.Borders(xlEdgeLeft)
        .LineStyle = xlContinuous
        .Color = -16776961
        .TintAndShade = 0
        .Weight = xlThick
    End With
    With rng.Borders(xlEdgeTop)
        .LineStyle = xlContinuous
        .Color = -16776961
        .TintAndShade = 0
        .Weight = xlThick
    End With
    With rng.Borders(xlEdgeBottom)
        .LineStyle = xlContinuous
        .Color = -16776961
        .TintAndShade = 0
        .Weight = xlThick
    End With
    With rng.Borders(xlEdgeRight)
        .LineStyle = xlContinuous
        .Color = -16776961
        .TintAndShade = 0
        .Weight = xlThick
    End With
End Sub

Open in new window

0
 

Author Closing Comment

by:Sabealgo
ID: 40524155
Thank you for your help.  This works exactly as I needed and you saved me a ton of time.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Do you use a spreadsheet like Microsoft's Excel?  Have you ever wanted to link out to a non excel file on your computer or network drive?  This is the way I found to do it!
In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

696 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question