Hi everyone, this is my first post. Thanks in advance for any help. I used to do extensive Lotus macro programming years ago, and some with older Excel, but am relatively new to VBA used with Excel 2007, so I'm flying by the seat of my pants somewhat with this.
I'm trying to write a macro in Excel 2007 that includes selecting a horizontal range of cells and then putting a border along the bottom of all cells in the range. But for some reason, everything I try gives me an error, usually #1004 "Application-defined or object-defined error." I've tried several different ways of selecting the range, including methods from Microsoft's own support website, but nothing seems to work and I can't for the life of me figure out what I'm doing wrong.
The columns are always the same, but the row being selected changes and so I need to use a variable for the row index. I'll put a number of attempts I've done, below, and what the results were.
In the attempt below, LineRow is a named cell in the spreadsheet that keeps track of which row is to get the border along the bottom. I tried just using LineRow as the row index (returned an error) so I tried using a local variable that takes LineRow's value (also returned an error). Either way, it bombs as soon as it gets to the Application.GoTo statement with a 1004 error.
z = LineRow For i = 1 To 9 Application.Goto Cells(z, i) With Selection.Borders(xlEdgeBottom) .LineStyle = xlContinuous .ThemeColor = 3 .TintAndShade = -0.499984740745262 .Weight = xlThick End With Next i
In the next attempt (below), LineRange is a spreadsheet cell that contains a concatenated range (e.g. A1:I16). This works when I set the PrintArea at the end of the macro, but with the code below, all that happens is that the cell named LineRange gets a border at the bottom. At least it doesn't bomb but the effect is definitely not what I'm looking for.
Range("LineRange").Select With Selection.Borders(xlEdgeBottom) .LineStyle = xlContinuous .ThemeColor = 3 .TintAndShade = -0.499984740745262 .Weight = xlThick End With
In the last attempt I'll post (below), I finally took the Select statement right from the MS support site and put in my own cell numbers - LineRow being the row number, with hard-coded column numbers. As with the others, as soon as it gets to the Select statement, it bombs with a 1004 error.
ActiveSheet.Range(Cells(LineRow, 1), Cells(LineRow, 9)).Select With Selection.Borders(xlEdgeBottom) .LineStyle = xlContinuous .ThemeColor = 3 .TintAndShade = -0.499984740745262 .Weight = xlThick End With
I would greatly appreciate any help with this to understand what will finally work, as well as hopefully to understand why the above attempts do not. Thanks in advance!
Last edited by starhugger; 11-11-2010 at 09:21 PM.
Hi, and welcome aboard,
I too cut my teeth on the old Lotus \ macros, (or was it the / ) which preceded the dialect of VBA called Lotus Script that they eventually used.
Since you say your columns are always the same, then one way using your LineRange variable and assuming columns B:F
HTHRange("B" & Range("LineRow") & ":F" & Range("LineRow")).Borders(xlEdgeBottom).Weight = xlThin
Last edited by Richard Buttrey; 11-11-2010 at 08:28 PM.
Richard Buttrey
If this was useful then please rate it appropriately.
Click the small star iconat the bottom left of my post.
If you want to refer to a cell, either as A1 or with its defined range name, you need to useLineRow is a named cell in the spreadsheet that keeps track of which row is to get the border along the bottom
This will return the value of the cell.z = Range("LineRow") ' or also z = [Linerow]
A cell can not contain a concatenated range. What exactly does the cell contain? Perhaps the text "A1:I16"? If so, you could useLineRange is a spreadsheet cell that contains a concatenated range (e.g. A1:I16). This works when I set the PrintArea at the end of the macro, but with the code below, all that happens is that the cell named LineRange gets a border at the bottom.
not pretty, and there are most probably better ways for achieving that.Range(Range("linerange").Value).Select
Your third code suffers from the same problem as the first.
cheersActiveSheet.Range(Cells([LineRow], 1), Cells([LineRow], 9)).Select
Thank you Richard and Teylyn! All of your suggestions worked!
Richard, yours worked although I need a way to control the thickness, colour, etc. and I'm not sure how to do all that with your statement format.
Teylyn's solutions also work and allow me to specify the rest of the border characteristics. So does that mean I can use a variable or named range as part of a statement's range as long as I surround it in square brackets?
Many thanks to both of you! I've been pulling my hair out by the roots for days trying to figure that one out.![]()
the square brackets are an alternative to addressing a range with
Whatever works within the round brackets of the Range() also works within []Range(<the range>) ' alternative [<the range>]
Thanks Teylyn.
Now my only question is: how do I mark this thread as "solved" for others who might be looking for the same thing?
If you are satisfied with the solution(s) provided, please mark your thread as Solved.
How to mark a thread Solved
Go to the first post
Click edit
Click Go Advanced
Just below the word Title you will see a dropdown with the word No prefix.
Change to Solved
Click Save
Okay, I figured out how to do that. Thanks again!
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks