PC Review


Reply
Thread Tools Rate Thread

Declaring an array variable

 
 
fisch4bill
Guest
Posts: n/a
 
      29th Dec 2009
I'm working on a project that involves putting borders around several ranges
of cells. I've been able to condense the code to the following, but, I think
I've seen a one-liner method of declaring and assigning values to an array
variable.

Option Explicit
Sub BorderApplication()
Dim X(5) As XlBordersIndex
Dim N As Integer
X(0) = (xlEdgeLeft)
X(1) = (xlEdgeRight)
X(2) = (xlEdgeTop)
X(3) = (xlEdgeBottom)
X(4) = (xlInsideVertical)
X(5) = (xlInsideHorizontal)
For N = 0 To 5
With Selection.Borders(X(N))
.LineStyle = xlContinuous
.ColorIndex = 0
.Weight = xlThin
End With
Next N
End Sub

Is there a way to assign all these values to X(N) in a single line or am I
confusing this with another language? And on another tack, is there a
different way to loop through the values? Even doing it this way will
streamline my code, but, I'm looking for even more simplicity if it's
available.

Thanks in advance,
Bill
 
Reply With Quote
 
 
 
 
OssieMac
Guest
Posts: n/a
 
      29th Dec 2009
Hi Bill,

Not sure the following is what you are looking for. Because you said you
want the borders on several ranges I have included code demonstrating the use
of Union for formatting multiple ranges.

Note that the space and underscore at the end of a line is a line break in
an otherwise single line of code.

Sub BorderApplication()
Dim X As Variant
Dim N As Integer

X = Array(xlEdgeLeft, _
xlEdgeRight, _
xlEdgeTop, _
xlEdgeBottom, _
xlInsideVertical, _
xlInsideHorizontal)

Dim rngBorders As Range

Set rngBorders = Union(Range("A1:C10"), _
Range("E1:F10"), _
Range("H1:J10"))

For N = 0 To 5
With rngBorders.Borders(X(N))
.LineStyle = xlContinuous
.ColorIndex = 0
.Weight = xlThin
End With
Next N
End Sub

--
Regards,

OssieMac


 
Reply With Quote
 
Rick Rothstein
Guest
Posts: n/a
 
      30th Dec 2009
Here is another way to structure your code...

Sub AddBorders()
Dim V As Variant
For Each V In Array(xlEdgeLeft, xlEdgeRight, xlEdgeTop, xlEdgeBottom, _
xlInsideVertical, xlInsideHorizontal)
With Selection.Borders(V)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = 0
End With
Next
End Sub

--
Rick (MVP - Excel)


"fisch4bill" <(E-Mail Removed)> wrote in message
news:16E03E08-E5D6-4823-87EB-(E-Mail Removed)...
> I'm working on a project that involves putting borders around several
> ranges
> of cells. I've been able to condense the code to the following, but, I
> think
> I've seen a one-liner method of declaring and assigning values to an array
> variable.
>
> Option Explicit
> Sub BorderApplication()
> Dim X(5) As XlBordersIndex
> Dim N As Integer
> X(0) = (xlEdgeLeft)
> X(1) = (xlEdgeRight)
> X(2) = (xlEdgeTop)
> X(3) = (xlEdgeBottom)
> X(4) = (xlInsideVertical)
> X(5) = (xlInsideHorizontal)
> For N = 0 To 5
> With Selection.Borders(X(N))
> .LineStyle = xlContinuous
> .ColorIndex = 0
> .Weight = xlThin
> End With
> Next N
> End Sub
>
> Is there a way to assign all these values to X(N) in a single line or am I
> confusing this with another language? And on another tack, is there a
> different way to loop through the values? Even doing it this way will
> streamline my code, but, I'm looking for even more simplicity if it's
> available.
>
> Thanks in advance,
> Bill


 
Reply With Quote
 
fisch4bill
Guest
Posts: n/a
 
      30th Dec 2009
Thanks to both OssieMac and Rick. This is exactly what I was looking for.
Bill

"Rick Rothstein" wrote:

> Here is another way to structure your code...
>
> Sub AddBorders()
> Dim V As Variant
> For Each V In Array(xlEdgeLeft, xlEdgeRight, xlEdgeTop, xlEdgeBottom, _
> xlInsideVertical, xlInsideHorizontal)
> With Selection.Borders(V)
> .LineStyle = xlContinuous
> .Weight = xlThin
> .ColorIndex = 0
> End With
> Next
> End Sub
>
> --
> Rick (MVP - Excel)
>
>
> "fisch4bill" <(E-Mail Removed)> wrote in message
> news:16E03E08-E5D6-4823-87EB-(E-Mail Removed)...
> > I'm working on a project that involves putting borders around several
> > ranges
> > of cells. I've been able to condense the code to the following, but, I
> > think
> > I've seen a one-liner method of declaring and assigning values to an array
> > variable.
> >
> > Option Explicit
> > Sub BorderApplication()
> > Dim X(5) As XlBordersIndex
> > Dim N As Integer
> > X(0) = (xlEdgeLeft)
> > X(1) = (xlEdgeRight)
> > X(2) = (xlEdgeTop)
> > X(3) = (xlEdgeBottom)
> > X(4) = (xlInsideVertical)
> > X(5) = (xlInsideHorizontal)
> > For N = 0 To 5
> > With Selection.Borders(X(N))
> > .LineStyle = xlContinuous
> > .ColorIndex = 0
> > .Weight = xlThin
> > End With
> > Next N
> > End Sub
> >
> > Is there a way to assign all these values to X(N) in a single line or am I
> > confusing this with another language? And on another tack, is there a
> > different way to loop through the values? Even doing it this way will
> > streamline my code, but, I'm looking for even more simplicity if it's
> > available.
> >
> > Thanks in advance,
> > Bill

>
> .
>

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: declaring variable Göran Andersson Microsoft C# .NET 0 14th Jun 2008 12:04 AM
Help declaring array limit with variable? Ed Microsoft Excel Programming 2 17th Oct 2005 05:35 PM
Declaring variable as a dynamic array? aiyer Microsoft Excel Programming 1 17th Aug 2004 11:01 PM
re: Declaring A Variable Gordon Durgha Microsoft VB .NET 1 16th Mar 2004 07:12 AM
Declaring a variable? pgoodale Microsoft Excel Programming 2 2nd Jan 2004 03:26 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 01:34 PM.