Ubound Error

Ben AFF

Board Regular
Joined
Sep 21, 2023
Messages
54
Office Version
  1. 365
Platform
  1. Windows
Hi, Im getting an error when I try to run a Ubound Sub example found in this link https://www.wallstreetmojo.com/vba-ubound/

Please can you help me understand what is wrong/missing in the code? Thank you.

VBA Code:
Sub Ubound_Example2()
    Dim DataRange() As Variant
    Sheets("Data Sheet").Activate
    DataRange = Range("A2", Range("A1").End(xlDown).End(xlToRight))   
    Worksheets.Add   
    Range(ActiveCell, ActiveCell.Offset(UBound(DataRange, 1) - 1, UBound(DataRange, 2) - 1)) = DataRange  
    Erase DataRange
End Sub
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
What error do you get & on which line?
 
Upvote 0
Thanks Fluff.

The error is in this line
VBA Code:
Range(ActiveCell, ActiveCell.Offset(UBound(DataRange, 1) - 1, UBound(DataRange, 2) - 1)) = DataRange

Application-defined or object-defined error.
 
Upvote 0
Which module is that code in?
 
Upvote 0
In running the code in the worksheet window code.
 
Upvote 0
Sorry I see now, I was not running it in a module.
 
Upvote 0
In a worksheet code module that first Range property refers to the sheet the code is in. Since the Activecell is not on the same sheet, you get an error trying to refer to a range on one sheet using cells on a different sheet. If you'd used Activesheet.Range it would have been fine.
 
Upvote 0
Solution
Glad we could help. :)
 
Upvote 0

Forum statistics

Threads
1,215,094
Messages
6,123,071
Members
449,092
Latest member
ipruravindra

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