1004 runtime error when using row count in opened file

lestersk

New Member
Joined
Nov 16, 2017
Messages
4
Would someone please explain why I get the error. in Excel 2013

Run-time error '1004':
Application-defined or object defined error

1004%20error.JPG


Senario (Description followed by code)
I have opened a workbook and worksheet (wsBS) from within the main worksheet (wsData).
I want to find the last row of the main worksheet and the last row of the opened worksheet.

LastRow and lastRowBS are defined as Integers

The following code works for the main worksheet (wsData)
Code:
LastRow = wsData.Cells(Rows.Count, 1).End(xlUp).Row

The following code for the opened worksheet fails
Code:
lastRowBS = wsBS.Cells(Rows.Count, 1).End(xlUp).Row

However the following code for the opened worksheet works (workaround)
Code:
lastRowBS = wsBS.Range("A65536").End(xlUp).Row

can anyone please explain why?

Thank you
 
Last edited by a moderator:

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN
Do not understand this:
I have opened a workbook and worksheet (wsBS) from within the main worksheet (wsData).

What do you mean by "main worksheet"

Do you mean active worksheet.
And what is "wsBS"

I think your saying worksheet when you mean Workbook
 
Upvote 0
Is the opened workbook an xls file?
 
Upvote 0
You haven't provided enough information or sample code to be sure but it's likely to be because the workbook you are dynamically opening is an older file version (xls) and does not have as many rows as the original worksheet. Because you have not qualified the range in Rows.Count it is attempting to start in row 1 million plus (the number of rows in the original worksheet) instead of the maximum 65536 of the opened workbook.

This

Code:
lastRowBS = wsBS.Cells([COLOR=#b22222]wsBS.[/COLOR]Rows.Count, 1).End(xlUp).Row

Should fix it (though untested).
 
Upvote 0
ScottR you are an absolute genius. Apologies for not providing enough information however from the vestigial quantity of code I provided, you have diagnosed and resolved the issue. wsBS is indeed a worksheet in a workbook of type .xls . Thank you very much.
 
Upvote 0
Thank you for your response however ScottR has resolved the issue. Apologies for the lack of information however my terminology was correct.
 
Upvote 0
Yes and that was what was causing the issue. You pointed me in the right direction and ScottR has resolved the issue.
 
Upvote 0
ScottR you are an absolute genius.

Two of us now agree. The movement is growing!

Seriously though, glad to be of assistance (especially as you posted with code tags) but one wrapping up post to confirm the issue was sorted probably would have covered it!
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,228
Messages
6,170,871
Members
452,363
Latest member
merico17

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