Solved

Using a macro to generate segregation lines

Posted on 2014-12-30
5
125 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
  • 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 46

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

Easy, flexible multimedia distribution & control

Coming soon!  Ideal for large-scale A/V applications, ATEN's VM3200 Modular Matrix Switch is an all-in-one solution that simplifies video wall integration. Easily customize display layouts to see what you want, how you want it in 4k.

Question has a verified solution.

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

Microsoft Office Picture Manager is not included in Office 2013. This comes as a shock to users upgrading from earlier versions of Office, such as 2007 and 2010, where Picture Manager was included as a standard application. This article explains how…
The advancement in technology has been a great source of betterment and empowerment for the human race, Nevertheless, this is not to say that technology doesn’t have any problems. We are bombarded with constant distractions, whether as an overload o…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

860 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