VBA ActiveSheet vs declared worksheet

OxfordCurmudgeon

New Member
Joined
Oct 24, 2023
Messages
14
Office Version
  1. 2010
  2. 2003 or older
Platform
  1. Windows
I'm baffled. This code works:

Set DataSourceSheet = ActiveSheet
With DataSourceSheet
DataHoldRows = .UsedRange.Rows.Count
DataHoldCols = .UsedRange.Columns.Count
DataHold = .Range(.Cells(1, 1), .Cells(DataHoldRows, DataHoldCols))
End With

This code does not:

With ActiveSheet
DataHoldRows = .UsedRange.Rows.Count
DataHoldCols = .UsedRange.Columns.Count
DataHold = .Range(.Cells(1, 1), .Cells(DataHoldRows, DataHoldCols))
End With

I get "Run-time error '13': Type mismatch" on the DataHold = line. The first two assignments work correctly either way. I've used the Watch window to verify that ActiveSheet is pointing where I thought it would.

The code to declare the variables was unchanged:

Dim DataSourceSheet As Worksheet
Dim DataHold() As Variant
Dim DataHoldRows As Integer
Dim DataHoldCols As Integer

I'm trying to gain some mastery of Excel VBA and would like to understand why the code is failing.
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
Use:

VBA Code:
Dim DataHold as Variant

not:

VBA Code:
Dim DataHold() as Variant

The Value property returns a Variant type, not a Variant array, and the late binding context causes the type mismatch.
 
Upvote 0
Solution
Use:

VBA Code:
Dim DataHold as Variant

not:

VBA Code:
Dim DataHold() as Variant

The Value property returns a Variant type, not a Variant array, and the late binding context causes the type mismatch.
Thank you for the prompt response!

Your suggestion works, but I'm still confused. To me, ActiveSheet (created by VBA) and DataSourceSheet (created by my code) are each instances of a Worksheet object -- and should be interchangeable.
 
Upvote 0
ActiveSheet returns a generic Object type (the active sheet might not be a worksheet), hence it is late bound if you don’t assign it to a typed variable.
 
Upvote 0
ActiveSheet returns a generic Object type (the active sheet might not be a worksheet), hence it is late bound if you don’t assign it to a typed variable.
Thank you! That's the mistake I was making: Assuming ActiveSheet was a WorkSheet object. With that corrected, everything else falls into place.
 
Upvote 0

Forum statistics

Threads
1,224,824
Messages
6,181,187
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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