Solved

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

Posted on 2014-11-07
9
227 Views
Last Modified: 2014-11-07
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
   
' OPEN THE REQUIRED WORKBOOK
   
   sFileName = Application.GetOpenFilename
   Set wbInput = Workbooks.Open(sFileName)

' TEST IF NEW BUSINESS
   
            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
Thanks
Toco
0
Comment
Question by:Tocogroup
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 3
9 Comments
 
LVL 85

Accepted Solution

by:
Rory Archibald earned 250 total points
ID: 40427990
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.
0
 
LVL 24

Assisted Solution

by:Phillip Burton
Phillip Burton earned 250 total points
ID: 40428038
1. Where does it breakdown? Which line number?
2. I'm assuming that the workbook you are opening actually has a spreadsheet called "Manchester".
0
 
LVL 24

Expert Comment

by:Phillip Burton
ID: 40428040
Line 20 doesn't use ".Value" - I don't know if that is significant or not.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:Tocogroup
ID: 40428052
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.
0
 
LVL 24

Expert Comment

by:Phillip Burton
ID: 40428060
Does it work if the workbook is not password protected?
0
 
LVL 85

Expert Comment

by:Rory Archibald
ID: 40428078
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.
0
 

Author Closing Comment

by:Tocogroup
ID: 40428113
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
Toco
0
 
LVL 85

Expert Comment

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

:)
0
 

Author Comment

by:Tocogroup
ID: 40428410
Thanks Rory. Point taken
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Excel can be a tricky bit of software to get your head around. Whilst you’ll be able to eventually get to grips with the basic understanding of how to get by, there are a few Excel tips that not everybody will even know about let alone know how to d…
This article describes how you can use Custom Document Properties to store settings and other information in your workbook so that they will be available the next time you open the workbook.
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

729 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question