Hi, I am using the following code to count the number of rows in a range. For some reason, it only works when I have the source sheet activated. I need it to work no matter what sheet you are on and bypass having to activate a specific sheet. The variable sheetname is a string with the name of the worksheet where the data is coming from. Does anyone know what is causing this to not work?
When I'm on the right sheet, I get rowcount of 15 which is correct. When I'm on a different sheet, I get an error that says: "Run-time error '1004': Application-defined or object-defined error". I have researched this error but have not been able to find what I am doing wrong. It looks correct to me based on what I've found. Any help is greatly appreciated.
When I'm on the right sheet, I get rowcount of 15 which is correct. When I'm on a different sheet, I get an error that says: "Run-time error '1004': Application-defined or object-defined error". I have researched this error but have not been able to find what I am doing wrong. It looks correct to me based on what I've found. Any help is greatly appreciated.
Code:
rowcount = Worksheets(sheetname).Range("A2", Range("A1").End(xlDown)).Cells.Count