Solved

Conditionally draw border with VBA

Posted on 2013-06-20
2
390 Views
Last Modified: 2013-06-21
Have an excel question (version 2010) as follows. For example, 2 columns and 7 rows

         A     B
1      Key  Value
2      D1   10
3              20
4      E1    11
5              21
6              31
7      F1    22
would like to draw the border (all solid lines around the cells, only draw the bottom lines as an example), but if there are no values in column A, do not draw anything, simply take the defaults.

         A     B
1      Key  Value
    --------------------
2     D1   10
3             20
    --------------------
4     E1    11
5             21
6             31
    --------------------
7     F1   22
    --------------------

Wrote something like this

Sub T()

With Range("A1:B" & Range("B" & Rows.Count).End(xlUp).Row).Borders
    .LineStyle = xlContinuous
    .Weight = xlThin
    .ColorIndex = xlAutomatic
End With
End Sub

How to add the check cell condition statement?
0
Comment
Question by:jl66
2 Comments
 
LVL 80

Accepted Solution

by:
byundt earned 500 total points
ID: 39264874
You might try a macro like this:
Sub T()
Dim rw As Range
With Range("A1:B" & Range("B" & Rows.Count).End(xlUp).Row)
    With .Borders
        .LineStyle = xlOutline
        .Weight = xlThin
        .ColorIndex = xlAutomatic
    End With
    .Borders(xlInsideHorizontal).LineStyle = xlNone
    .Borders(xlInsideVertical).LineStyle = xlNone
    For Each rw In .Rows
        If rw.Cells(2, 1).Value <> "" Then
            With rw.Borders(xlEdgeBottom)
                .LineStyle = xlDash
            End With
        End If
    Next
End With
End Sub

Open in new window

Brad
0
 

Author Closing Comment

by:jl66
ID: 39265713
Thanks a lot.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Approximate matching with VLOOKUP and MATCH seems to me to be a greatly under-used technique, and one which is vital for getting good performance out of large lookups. Until recently I would always have advised using an exact match for simplicity an…
This article is the result of a quest to better understand Task Scheduler 2.0 and all the newer objects available in vbscript in this version over  the limited options we had scripting in Task Scheduler 1.0.  As I started my journey of knowledge I f…
The viewer will learn how to simulate a series of sales calls dependent on a single skill level and learn how to simulate a series of sales calls dependent on two skill levels. Simulating Independent Sales Calls: Enter .75 into cell C2 – “skill leve…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

759 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now