Application-Defined or Object-Defined Error on Range Resize

gui132

New Member
Joined
Aug 26, 2019
Messages
5
When i try to resize a range after offsetting it I get an Application-Defined or Object-Defined Error.
Can't understand why.

Code:
Sub test()




Dim sourceSht As Worksheet
Dim targetSht As Worksheet
Dim LastRow As Long
Dim selectedData As Range




Set sourceSht = ActiveWorkbook.Sheets("Colaboradores")
LastRow = sourceSht.ListObjects("Table3").Range.Columns("BB").Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
Set targetSht = ActiveWorkbook.Sheets("Sheet3")


'Check for existance of table
'SELECT ALL NEEDED DATA


Set selectedData = sourceSht.Range("Table3[UE i" & vbLf & "(área)], Table3[UE ii" & vbLf & "(unidade)],Table3[2013 -1ºSemestre]:Table3[2019-2ºSemestre]").Offset(-1, 0)


h = selectedData.Rows.Count + 1 <<<< Because of the offset add one row to the range

Areas = selectedData.Areas.Count
w = 0
For i = 1 To Areas
w = w + selectedData.Areas(i).Columns.Count
Next i




Set selectedData = selectedData.Resize(h, w) <<<<<<<<<<<< Error occurs here


'COPY AND PASTE INTO NEW SHEET
selectedData.Copy
targetSht.Range("A1").PasteSpecial xlPasteValues


'CREATE NEW TABLE


Set selectedData = targetSht.Range("A1").Resize(h, w)  <<<<<<<<< This works fine
targetSht.ListObjects.Add(xlSrcRange, selection, , xlYes).Name = "MyTable"




End Sub
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
When i try to resize a range after offsetting it I get an Application-Defined or Object-Defined Error.
Can't understand why.

Code:
Sub test()




Dim sourceSht As Worksheet
Dim targetSht As Worksheet
Dim LastRow As Long
Dim selectedData As Range




Set sourceSht = ActiveWorkbook.Sheets("Colaboradores")
LastRow = sourceSht.ListObjects("Table3").Range.Columns("BB").Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
Set targetSht = ActiveWorkbook.Sheets("Sheet3")


'Check for existance of table
'SELECT ALL NEEDED DATA


Set selectedData = sourceSht.Range("Table3[UE i" & vbLf & "(área)], Table3[UE ii" & vbLf & "(unidade)],Table3[2013 -1ºSemestre]:Table3[2019-2ºSemestre]").Offset(-1, 0)


h = selectedData.Rows.Count + 1 <<<< Because of the offset add one row to the range

Areas = selectedData.Areas.Count
w = 0
For i = 1 To Areas
w = w + selectedData.Areas(i).Columns.Count
Next i




Set selectedData = selectedData.Resize(h, w) <<<<<<<<<<<< Error occurs here


'COPY AND PASTE INTO NEW SHEET
selectedData.Copy
targetSht.Range("A1").PasteSpecial xlPasteValues


'CREATE NEW TABLE


Set selectedData = targetSht.Range("A1").Resize(h, w)  <<<<<<<<< This works fine
targetSht.ListObjects.Add(xlSrcRange, selection, , xlYes).Name = "MyTable"




End Sub

Fixed it. Resize does not work when you have multiple areas. Had to resize each area individually and join them with UNION.
 
Upvote 0

Forum statistics

Threads
1,214,911
Messages
6,122,194
Members
449,072
Latest member
DW Draft

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