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

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
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,215,073
Messages
6,122,975
Members
449,095
Latest member
Mr Hughes

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