What's wrong with this code?

Hi I have defined wb1 as a Workbook.

I am trying to run this code wb1.ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Select
It is supposed to select the last row in a cell, however it gives me an error

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

I am sure it has something to do with Syntax, not sure how to correct it. Any help will be greatly appreciated. Thanks.

Recommended Answers

All 6 Replies

I ran this test code and it seemed to work fine:

Sub fred()
Dim wb1 As New Excel.Workbook

Set wb1 = ActiveWorkbook

wb1.ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Select

End Sub

It seemed to work for me. How are you instantiating the wb1 variable?

I did something similar to instantiate, and I know the instantiations are correct, however I have more than one workbook open at a time, and it refuses to work when I add wb1.ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Select

Check whether u hv referenced the excel object to your project.
Also check for the xlUp value at runtime.

The error doesn have anything to do with VB. It is triggered from the excel mod.....

I have it compiles opens all the workbooks I have instantiated. I can activate other workbooks select a particular cell in any workbook that I activate, but I cannot execute the above command. So, I am at a loss. It definitely has something to do with syntax becuase when I type wb1. I get a intellisense recommendations after I choose ActiveSheet... I get nothing from the intellisense so I am not sure how to proceed.

Also, when I hover over the code which is highlighted in yellow.

I get the values for Rows.Count as 1048576 and when I hover at the xlUp I get -4162

Try executing the code in excel using macros. Replace Rows.Count with the number you got (1048576). It would give a detailed description on what error you encountered.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.