Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Excel range selection problem

Status
Not open for further replies.

idono

Technical User
Jan 16, 2002
71
0
0
US
Hello all,

I have about lost all my hair! I'm trying to format cells in excel from vb. All was going well until I tried to add a border to a selection.

First line works! It selects the row as expected. The other lines I got out of an excel macro(don't work). I get an error message 91:eek:bject variable or with block variable not set. The problem doesn't stem from no with statement on the first two lines. You take those out and it still doesn't work.

Any suggestions?


objNewXLBook.ActiveSheet.Range("A" & X + 1 & ":" & "F" & X + 1).Select

Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Selection.Borders(xlDiagonalUp).LineStyle = xlNone
With Selection.Borders(xlDiagonalUp)
.LineStyle = xlNone
End With
With Selection.Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
With Selection.Borders(xlEdgeTop)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
With Selection.Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
With Selection.Borders(xlEdgeRight)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
With Selection.Borders(xlInsideVertical)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
 
Hi,

I tried to write just:

ActiveSheet.Range("A" & X + 1 & ":" & "F" & X + 1).Select
and the rest

It does work fine.

So, I guess the problem is the var 'objNewXLBook' has not been declared/set yet.

Tin Tin

 
Thanks for the reply TTThio.
I think the var is set and declared. Here is the code.

Dim objNewXLBook As Excel.Workbook

Set objNewXLBook = GetObject(XLS_FMC_Location & "Flowline Backlog.xls")

This sets it and it will open. Answer me this, is the Selection variable set with the .Select call? Or should I dimension a variable for it.

Thoroughly confused
 
TTThio,
One thing I forgot to mention was that I am formatting from an Access module. My original message implies that I am using an Excel module within Excel. This is an application to send a query to an Excel file from Access.
 
Let me experiment with it. I'll reply to you later on.
About 'Selection', you don't need to set a variable for it.
It's a built-in.
 
Hi!

Try it this way:

With objNewXLBook.ActiveSheet.Range("A" & X + 1 & ":" & "F" & X + 1)

.Borders(xlDiagonalDown).LineStyle = xlNone
.Borders(xlDiagonalUp).LineStyle = xlNone
.Borders(xlEdgeLeft).LineStlye = xlContinuous
.Borders(xlEdgeLeft).Weight = xlThin
etc.

One note, any place where you have LineStyle = xlNone can be left out, since that is the default.

hth

Jeff Bridgham
bridgham@purdue.edu
 
jebry,
I would have never thought of it that way, but it works. Thanks a mill for the advice.

Dugger
 
Hey guys...

Range definitions work ALOT better in code with the Cells(row, col) format so that
Code:
Range("A" & X + 1 & ":" & "F" & X + 1)
becomes
Code:
Range(Cells(X + 1, 1), Cells(X + 1, 6))
:) Skip,
metzgsk@voughtaircraft.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top