Help with new Run-Time Error PLEASE!!

logandiana

Board Regular
Joined
Feb 21, 2017
Messages
107
The error is a Run-time error 21473219767 (80028029)
Automation error
Invalid forward reference, or reference to uncompiled type


VBA Code:
Dim DD As Workbook
Dim Data, Exp As Worksheet
Dim LR1 As Long
Set DD = ThisWorkbook
Set Data = DD.Sheets("Data")
Set Exp = DD.Sheets("export")
LR1 = Data.Cells(Rows.Count, 5).End(xlUp).Row     '''<<<<<<< ERRORS here
...


THIS CODE HAS BEEN RUNNING STABLE FOR YEARS!!
I run this code several times each day. The code hasn't been changed in years and has always run just fine.
This morning was a different story. Several users are getting the same message when they try so that tells me its not just my machine.

What is causing this error? What changed between yesterday and today?
 

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
Strange
But try these amendments

Rich (BB code):
    Dim DD As Workbook
    Dim Data As Worksheet, Exp As Worksheet
    Dim LR1 As Long
    Set DD = ThisWorkbook
    Set Data = DD.Sheets("Data")
    Set Exp = DD.Sheets("export")
    LR1 = Data.Cells(Data.Rows.Count, 5).End(xlUp).Row
 
Upvote 0
Strange
But try these amendments

Yes! Just separating the declarations worked, but... WHY!!??!???

Why does code work thousands of times untouched for years, then all of a sudden decide, "You know I should have said something years ago, but I think you are going to need to separate those or I am going to refuse to work"

What could have caused this?
 
Upvote 0
Never mind why it has changed recently.
The reason it works now is that your code is declared correctly
In your original code variable Data is type VARIANT and should be type WORKSHEET
- for some reason VBA is no longer happy with type VARIANT (I have no idea why!)

Regardless of any code you may have seen elsewhere VBA requires that EVERY variable type is assigned separately otherwise VBA treats it as a variant
That is not the case, I believe, with some similar languages
 
Upvote 0
I understand now, and more importantly that I have been declaring variables wrong all these years. I have always declared stuff like Dim LR1, LR2, LR3 as long. And only the LR3 is long while LR1 and LR2 are variants.
I need to go back and change a bunch of my previous work, even though it works, it’s not correct. I do learn something new every time though!
 
Upvote 0
Never mind why it has changed recently.
The reason it works now is that your code is declared correctly
In your original code variable Data is type VARIANT and should be type WORKSHEET
- for some reason VBA is no longer happy with type VARIANT (I have no idea why!)

Regardless of any code you may have seen elsewhere VBA requires that EVERY variable type is assigned separately otherwise VBA treats it as a variant
That is not the case, I believe, with some similar languages

Yet, if someone like me uses the macro recorder (and it is provided to help people who are not programmers), it generates the code the same way it always has which, apparently now, is (still) incorrect.

If it is incorrect, why is the macro recorder still producing it?
 
Upvote 0
Yet, if someone like me uses the macro recorder (and it is provided to help people who are not programmers), it generates the code the same way it always has which, apparently now, is (still) incorrect.

If it is incorrect, why is the macro recorder still producing it?
The problem was with user's invalid variables declaration. The macro recorder never produces that part of the code.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,182
Members
453,021
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