formating a row: with block not set error - help!

wack

New Member
Joined
Jan 7, 2004
Messages
14
Hi all!

I am dumping data from access to excel and want to bold the total row. It will not let me becuase I get a run-time error '91' - object or with block variable not set.

I have tried a bunch of different stuf and still the same error. Any ideas?

Here is my code:

xlw.Sheets("Results by Service Line").Select
lastRow = xlw.Sheets("Results by Service Line").Range("a1").SpecialCells(xlLastCell).row
lastRow = lastRow + 1
xlw.Sheets("Results by Service Line").Rows(lastRow).Select
With selection.font
.Bold = True
End With
 

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.

Ekim

Well-known Member
Joined
Jun 30, 2002
Messages
1,416
Wack,

You will get an “error '91' - object or with block variable not set” when you do not define variables which refer to objects (workbooks, worksheets, ranges etc.) You define such objects by using the Set command.

In your case, you have not define what “xlw” is.

The following macro will bold items in column A, from A1 to the “lastrow”.
Code:
Option Explicit
Sub myFormat()
Dim xlw As Workbook
Dim ws As Worksheet
Dim lastrow As Long

Set xlw = ThisWorkbook
Set ws = xlw.Sheets("Results by Service Line")

lastrow = ws.Range("a1").SpecialCells(xlLastCell).Row

With ws.Range("A1:A" & lastrow)
    With .Font
       .Bold = True
    End With
End With

End Sub

It is always a good idea to put “Option Explicit” at the start of your code because this will force you to declare all your variables.

HTH

Mike
 
Master Excel Bundle

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

Forum statistics

Threads
1,152,161
Messages
5,768,539
Members
425,480
Latest member
br400821

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Top