Solved

Conditionally draw border with VBA

Posted on 2013-06-20
2
395 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 81

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

This tutorial explains how to create a series of drop-down lists that are dependent upon prior selections to guide (“force”) the user to make the correct selection and reduce data errors within Microsoft Excel. Excel 2010 was used for this tutorial;…
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…
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.

863 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

28 Experts available now in Live!

Get 1:1 Help Now