Solved

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

Posted on 2014-10-15
5
116 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 51

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Excel formula with SUMIF and SUBTOTAL 13 45
NEED TO DO 2 OR 3 OR 4 NUMBERS LOOK UP 2 22
Excel Auto-Complete lines 4 32
the UDF returns #Value when i open workbook. 19 44
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,…
When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
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…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

739 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