Solved

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

Posted on 2014-10-15
5
114 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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

A little background as to how I came to I design this code: Around 5 years ago I designed an add-in that formatted Excel files to a corporate standard, applying different cell colours and font type depending on whether the cells contained inputs,…
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 Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.

806 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