Why do I get a Run-time error 91 when debugging a cell value in my VBA Excel application ?

Hi Experts,

I have a VBA application which loops through the cells in a column in Excel. Each cell is tested for a particular value - ="N". When I run the application it seems to ignore this test and carries on regardless.

When debugging the code I get a 'Run-time error 91 Object variable or With block variable not set ' message, even though I've Set the sheet object.

I've attached the code below.

   Dim wbInput              As Workbook
   Dim wsInputSource   As Worksheet
   Dim sFileName          As String

   On Error Resume Next
   sFileName = Application.GetOpenFilename
   Set wbInput = Workbooks.Open(sFileName)

            Application.ScreenUpdating = False

            Set wsInputSource = wbInput.Sheets("Manchester")

            For r = 2 To 50
               If wsInputSource.Cells(r, 2).Value = "N" Then
                     If wsInputSource.Cells(r, 3) <> 0 Then
                        wsInputSource.Cells(r, 4).value = "New"
                     End If
               End If
            Next r

...etc etc....

Open in new window

Any ideas
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Rory ArchibaldCommented:
Start by commenting out the On Error Resume Next. It's generally a bad idea to just stick that at the top of a routine.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Phillip BurtonDirector, Practice Manager and Computing ConsultantCommented:
1. Where does it breakdown? Which line number?
2. I'm assuming that the workbook you are opening actually has a spreadsheet called "Manchester".
Phillip BurtonDirector, Practice Manager and Computing ConsultantCommented:
Line 20 doesn't use ".Value" - I don't know if that is significant or not.
C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

TocogroupAuthor Commented:
Ok......I've commented out the On Error line.

It's now returning a 'Run-Time error 1004 Application-defined or object-defined error' on the sFilename = Application.GetOpenFilename statement. I'm prompted to enter a password, which I do, and then it falls over.

Yes, the sheet 'Manchester' exists.
Phillip BurtonDirector, Practice Manager and Computing ConsultantCommented:
Does it work if the workbook is not password protected?
Rory ArchibaldCommented:
Are you sure it's on that line and not the next one? GetOpenFileName shouldn't prompt you for a password as it's not opening anything.
TocogroupAuthor Commented:
Thanks both,

I managed to get it through the open file procedure. The problem was the sheet filename: the creator had addeda space to the end of Manchester.
I'm going to split the points on this one because I've learned two things:
1. Comment out the On Error statement when debugging.
2. Always check the sheet name for hidden characters

Much appreciated
Rory ArchibaldCommented:
#1 should be "Don't just put 'On Error Resume Next' at the top of a procedure. Use it only where required.

TocogroupAuthor Commented:
Thanks Rory. Point taken
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Excel

From novice to tech pro — start learning today.