Error on define Myrange at VBA

maabadi

Well-known Member
Joined
Oct 22, 2012
Messages
2,681
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
 

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
Rich (BB code):
Set MyRange = Range("A1:K" & Lastrow)
and
Rich (BB code):
Debug.Print MyRange.Address
 
Upvote 0
Solution
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
 
Upvote 0
VBA Code:
ws.PageSetup.PrintArea = MyRange.Address
ws.PrintOut Preview:=True
 
Last edited:
Upvote 0
Sorry Compile error: Method or Data member not found on:

ws.PageSetup.PrintArea = ws.MyRange.Address
 
Upvote 0

Forum statistics

Threads
1,214,649
Messages
6,120,728
Members
448,987
Latest member
marion_davis

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
Back
Top