Error on define Myrange at VBA

maabadi

Well-known Member
Joined
Oct 22, 2012
Messages
1,344
Office Version
  1. 2019
  2. 2016
Platform
  1. Windows
I want to set myrange based on cell value, but when run my macro. I see runtime error 91.
object not defined or set block variable

Sub SetRangeforPrint()
Dim MyRange As Range
Dim ws As Worksheet
Dim Lastrow As Long
If Range("B65").Value <> "" Then
Lastrow = Range("B65").Value * 34 + 6
Debug.Print Lastrow
MyRange = Range("A1:K" & Lastrow)
Debug.Print MyRange
End If
End Sub
 

Some videos you may like

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)

MARK858

MrExcel MVP
Joined
Nov 12, 2010
Messages
13,859
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
  2. Mobile
Rich (BB code):
Set MyRange = Range("A1:K" & Lastrow)
and
Rich (BB code):
Debug.Print MyRange.Address
 
Solution

maabadi

Well-known Member
Joined
Oct 22, 2012
Messages
1,344
Office Version
  1. 2019
  2. 2016
Platform
  1. Windows
Thanks Mark858. Very well working.
 

MARK858

MrExcel MVP
Joined
Nov 12, 2010
Messages
13,859
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
  2. Mobile
You're welcome
 

maabadi

Well-known Member
Joined
Oct 22, 2012
Messages
1,344
Office Version
  1. 2019
  2. 2016
Platform
  1. Windows

ADVERTISEMENT

Sorry. And one other thing:

I modified code to this but again have problems:
Sub SetRangeforPrint()
Dim MyRange As Range
Dim ws As Worksheet
Dim Lastrow As Long
Dim PrintArea As String

If Range("B65").Value <> "" Then
Lastrow = Range("B65").Value * 34 + 6
Debug.Print Lastrow
Set MyRange = Range("A1:K" & Lastrow)
Debug.Print MyRange.Address
End If
Set ws = Worksheets("sheet2")
Set PrintArea = ws.MyRange.Address
PrintArea.PrintOut Preview:=True

End Sub
 

MARK858

MrExcel MVP
Joined
Nov 12, 2010
Messages
13,859
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
  2. Mobile
VBA Code:
ws.PageSetup.PrintArea = MyRange.Address
ws.PrintOut Preview:=True
 
Last edited:

maabadi

Well-known Member
Joined
Oct 22, 2012
Messages
1,344
Office Version
  1. 2019
  2. 2016
Platform
  1. Windows

ADVERTISEMENT

Sorry Compile error: Method or Data member not found on:

ws.PageSetup.PrintArea = ws.MyRange.Address
 

maabadi

Well-known Member
Joined
Oct 22, 2012
Messages
1,344
Office Version
  1. 2019
  2. 2016
Platform
  1. Windows
Thanks again. & Have nice days always.
 

Watch MrExcel Video

Forum statistics

Threads
1,122,850
Messages
5,598,446
Members
414,240
Latest member
xnanx

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