With command

tiredofit

Well-known Member
Joined
Apr 11, 2013
Messages
1,926
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
I have values in cells A1 to B2 on Sheet1.

I want to read those values into an array and paste onto Sheet2.

Why is it that this works:

Rich (BB code):
    Dim MyArray() As Variant
    
    MyArray() = Sheet1.Cells(1, 1).CurrentRegion.Value
    
    Sheet2.Cells(1, 1).CurrentRegion.Delete
    Sheet2.Cells(1, 1).Resize(UBound(MyArray(), 1), UBound(MyArray(), 2)).Value = MyArray()



but this fails:

Rich (BB code):
    Dim MyArray() As Variant
    
    MyArray() = Sheet1.Cells(1, 1).CurrentRegion.Value
        
    With Sheet2.Cells(1, 1)

        .CurrentRegion.Delete
        .Resize(UBound(MyArray(), 1), UBound(MyArray(), 2)).Value = MyArray()

    End With



wth the error message:

Rich (BB code):
    Run-time error '434'

    Object required

Thanks

<strike>
</strike>
 
Last edited:

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
The With statement holds a reference to Cells(1, 1). You then delete that entire block of cells, so the reference is invalid from that point on, but you then try to resize it, which causes the error. It's essentially like you used a variable:

Code:
    Dim MyArray() As Variant
    
    MyArray() = Sheet1.Cells(1, 1).CurrentRegion.Value
    dim rg as range
    set rg = Sheet2.Cells(1, 1)

        rg.CurrentRegion.Delete
        ' rg is now invalid, because you deleted it, so the next line will raise an error
        rg.Resize(UBound(MyArray(), 1), UBound(MyArray(), 2)).Value = MyArray()
 
Last edited:
Upvote 0
Thanks.

With the "With command" I am still trying to think of it like factorising in maths (ie ab+ac = a(b+c) )but I guess it's not exactly 100% the same!
 
Upvote 0
Not exactly, no! Think of it more like an implicit variable.
 
Upvote 0

Forum statistics

Threads
1,224,819
Messages
6,181,153
Members
453,021
Latest member
Justyna P

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