Solved

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

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

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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
onOpen 14 43
Index/Match with Multiple Criteria 2 16
Name Rotation 11 30
Excel Worksheet Function of VBA Code 5 19
INDEX and MATCH can be used to great effect to replace HLOOKUP and VLOOKUP as it does not have the limitation of needing the data to be sorted so that the reference value is in the first column or row. It also has the ability to perform a bi-directi…
Improved? Move/Copy Add-in Replacement - How to avoid the annoying, “A formula or sheet you want to move or copy contains the name XXX, which already exists on the destination worksheet.” David Miller (dlmille)  It was one of those days… I wa…
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
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.

743 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

10 Experts available now in Live!

Get 1:1 Help Now