Solved

insert borders to a range that will change each time macro runs

Posted on 2014-10-15
5
113 Views
Last Modified: 2014-10-16
I have been using the below code to insert borders but I need a varaition to this:

With ActiveSheet.UsedRange.Borders
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = xlAutomatic
    End With

What I need to do is insert to the range I select, so if I say

Range(Range("B3"), Cells.SpecialCells(xlCellTypeLastCell)).Select

and the range is B3:J15 I just want borders around those cells

if range is B3:J58 I want borders around those cells.

I have tried to change ......With ActiveSheet.UsedRange.Borders
but have not been successful.

Could an Expert assist me please.

Many thanks
0
Comment
Question by:Jagwarman
5 Comments
 
LVL 49

Expert Comment

by:Rgonzo1971
ID: 40381821
Hi,

pls try

Sub Mactro()
With ActiveSheet.UsedRange.Borders
    .LineStyle = xlNone
End With
Set myRange = Range(Range("B2"), Cells.SpecialCells(xlCellTypeLastCell))
With myRange.Borders(xlEdgeLeft)
    .LineStyle = xlContinuous
    .Weight = xlThin
    .ColorIndex = xlAutomatic
End With
With myRange.Borders(xlEdgeBottom)
    .LineStyle = xlContinuous
    .Weight = xlThin
    .ColorIndex = xlAutomatic
End With
With myRange.Borders(xlEdgeTop)
    .LineStyle = xlContinuous
    .Weight = xlThin
    .ColorIndex = xlAutomatic
End With
With myRange.Borders(xlEdgeRight)
    .LineStyle = xlContinuous
    .Weight = xlThin
    .ColorIndex = xlAutomatic
End With
   

End Sub

Open in new window

Regards
0
 
LVL 35

Accepted Solution

by:
Kimputer earned 500 total points
ID: 40381823
As I don't have the full code, here are just some steps to point you in the right direction:

With Selection.Borders

Open in new window


or

With ActiveSheet.Range("B3:J58").Borders

Open in new window

(of course, try to make the range a variable, which you can set dynamically)
0
 

Author Comment

by:Jagwarman
ID: 40381833
Hi Rgonzo 1971 thanks for that, clearly you did what I asked and not what I wanted. Having read through my request again I did say "and the range is B3:J15 I just want borders around those cells" but what I meant was I want borders around every cell in that range. So once again I was not specific enough and got what I asked for. Sorry.
0
 

Author Closing Comment

by:Jagwarman
ID: 40381841
The first one is the one I need because I don't want to insert a range. Thanks
0
 

Expert Comment

by:Erick Wei
ID: 40386090
Try this one:

Sub test()

Call BorderLine(Sheet1.Range("B3:J15"))
Call BorderLine(Sheet1.Range("B3:J58"))

End Sub


Function BorderLine(rng As Range)

    With rng.Borders
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = xlAutomatic
    End With
   
End Function
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

Suggested Solutions

Title # Comments Views Activity
macro for finding text and replacing 7 44
FormulaArray VBA Issue 6 17
Help with Excel formula 6 38
remove upper case characters in excel 4 19
Sparklines have been introduced with Excel 2010 and are a useful tool for creating small in-cell charts, used for example in dashboards. Excel 2010 offers three different types of Sparklines: Line, Column and Win/Loss. What it does not offer is a…
Drop Down List with Unique/Distinct Values (Part II - ComboBox or ListBox and Data Validation List Bonus!) David Miller (dlmille) Intro This article focuses on delivering unique, sorted lists to list objects (e.g., ComboBox, ListBox) and Dat…
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.

895 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

16 Experts available now in Live!

Get 1:1 Help Now