Runtime error 91

vijayb2009

New Member
Joined
Jun 26, 2009
Messages
4
Hi,

I'm trying to find a text in a sheet and pass adjacent value to another sheet. While running the code given below, I'm getting runtime error 91. I tried many options, but in vain. Kindly help.Please find the code below

Sub assign_to_pp(tosht As String, fromsht As String)
Dim u, v As Integer
Dim r, q, b As String
q = " Total"
For u = 5 To 104
Sheets(tosht).Select
Sheets(tosht).Cells(u, 2).Select
r = Sheets(tosht).Cells(u, 2)
If r <> "" Then
b = r & q
Sheets(fromsht).Select
Cells(2, 1).Select
Sheets(fromsht).Cells.Find(What:=b, After:=ActiveCell, LookIn:=xlFormulas, LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False).activate
On Error GoTo unext
Sheets(fromsht).Select
v = ActiveCell.Row
Sheets(tosht).Select
Sheets(tosht).Cells(u, 3).Value = Sheets(fromsht).Cells(v, 11).Value
Else
Sheets(tosht).Cells(u, 3).Value = ""
GoTo unext
End If
unext:
Next u
End Sub
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
It happens in the first row itself (that is in row 5). But the data in row 5 is R628, which is not available in fromsht. If i put a data which is available in fromsht, it passes the value & goes to the next increment.
 
Upvote 0
Hi,

First of all let's discuss variable declaration because it's very important.

This line of code declares r and q as variants, and b as a string.
Rich (BB code):
Dim r, q, b As String

Similarly this line of code declares u as a variant as v as an integer.
Rich (BB code):
Dim u, v As Integer


If you're declaring multiple variables on the same line you need to specify each variable's type:
Rich (BB code):
Dim u As Integer, v As Integer
Dim r As String, q As String, b As String
There are 2 'exceptions' to this, but they are not relevant here.


Looking further down the code, the variable q is assigned a value "Total" which does not subsequently change. Instead of declaring q as a variable it could be declared as a constant.
Rich (BB code):
Const q As String = " Total"
 
Dim u As Integer, v As Integer
Dim r As String, b As String

We also see that u and v are being used in reference to row numbers. The number of rows on a worksheet (65536+) exceeds the maximum value an integer data type can hold (32767) so we should declare them as Long Integer data types to avoid the possibility of overflow errors:
Rich (BB code):
Const q As String = " Total"
 
Dim u As Long, v As Long
Dim r As String, b As String


Now let's tidy up the code a bit by removing the unnecessary Select/Selection methods/properties. We end up with something like this (now that the fromsht isn't activated some of the below code is inappropriate, but we will correct that in a moment):
Rich (BB code):
Sub assign_to_pp(tosht As String, fromsht As String)
    Const q As String = " Total"
 
    Dim u As Long, v As Long
    Dim r As String, b As String
    For u = 5 To 104
        r = Sheets(tosht).Cells(u, 2).Value
 
        If r <> "" Then
            b = r & q
 
            Sheets(fromsht).Cells.Find( _
                                    What:=b, _
                                    After:=Sheets(fromsht).Cells(2, 1), _
                                    LookIn:=xlFormulas, _
                                    LookAt:=xlWhole, _
                                    SearchOrder:=xlByRows, _
                                    SearchDirection:=xlNext, _
                                    MatchCase:=False, _
                                    SearchFormat:=False).Activate
 
            On Error GoTo unext
            v = ActiveCell.Row     
 
            Sheets(tosht).Cells(u, 3).Value = Sheets(fromsht).Cells(v, 11).Value
        Else
 
            Sheets(tosht).Cells(u, 3).Value = ""
            GoTo unext
        End If
unext:
        Next u
End Sub

This code is easier to read and analyse. There are two (argubly three) problems.

1. The line that's causing the error is here:
Rich (BB code):
            Sheets(fromsht).Cells.Find( _
                                    What:=b, _
                                    After:=Sheets(fromsht).Cells(2, 1), _
                                    LookIn:=xlFormulas, _
                                    LookAt:=xlWhole, _
                                    SearchOrder:=xlByRows, _
                                    SearchDirection:=xlNext, _
                                    MatchCase:=False, _
                                    SearchFormat:=False).Activate
The question to ask is, "What happens if the search value isn't found?"
The answer is the range.find() method returns Nothing. You can't activate Nothing, so you will get a runtime error in that scenario.

Therefore you need to introduce some defensive coding to cater for it.


2. Your Error handling structure has a problem.

You're using this Goto statement to branch to the below line in your procedure:
Rich (BB code):
On Error GoTo unext
Rich (BB code):
unext:
        Next u

This is no good. You must use a Resume statement in your error handler to continue with the main part of your procedure otherwise the error handler will remain enabled and you will experience unexpected behaviour. I recommend you read this:
http://www.cpearson.com/excel/ErrorHandling.htm

Taking all the above into account I think we get to something like this:
Rich (BB code):
Sub assign_to_pp(tosht As String, fromsht As String)
    Const q As String = " Total"
 
    Dim u As Long, v As Long
    Dim r As String, b As String
    Dim rngFound As Range
 
    For u = 5 To 104
        r = Sheets(tosht).Cells(u, 2).Value
 
        If r <> "" Then
            b = r & q
 
            Set rngFound = Sheets(fromsht).Cells.Find( _
                                    What:=b, _
                                    After:=Sheets(fromsht).Cells(2, 1), _
                                    LookIn:=xlFormulas, _
                                    LookAt:=xlWhole, _
                                    SearchOrder:=xlByRows, _
                                    SearchDirection:=xlNext, _
                                    MatchCase:=False, _
                                    SearchFormat:=False)
 
            If Not rngFound Is Nothing Then
                v = rngFound.Row
                Sheets(tosht).Cells(u, 3).Value = Sheets(fromsht).Cells(v, 11).Value
            End If
        Else
 
            Sheets(tosht).Cells(u, 3).Value = ""
        End If
    Next u
End Sub

Hope that helps...
 
Last edited:
Upvote 0
If you're declaring multiple variables on the same line you need to specify each variable's type:
Code:
Dim u As Integer, v As Integer
Dim r As String, q As String, b As String
There is an exception to this, but it's not relevant here.
Wow.. excellent answer there Colin, I knew if I bumped the thread someone competent would answer.:biggrin:

However I'm curious about the declarations, sorry to hijack the thread, what exceptions are there, and why on earth can't one shorthand the declarations?! I've been doing that for ages until I saw an offhand remark here that it didn't work at all.
 
Upvote 0
Hi snowblizz,

Wow.. excellent answer there Colin, I knew if I bumped the thread someone competent would answer.:biggrin:

However I'm curious about the declarations, sorry to hijack the thread, what exceptions are there, and why on earth can't one shorthand the declarations?! I've been doing that for ages until I saw an offhand remark here that it didn't work at all.

The 2 exceptions I was referring to are


(1) Using Type-Declaration characters

eg. This will declare a,b and c as strings:
Code:
Dim a$, b$, c$

(2) Using Deftype Statements

eg. This will declare a,b and c as strings:
Code:
Option Explicit
 
DefStr A-C
 
Sub Declare_Strings2()
 
    Dim a, b, c
 
End Sub


As for why - because that's how VBA/VB6 was designed. :)

Hope that helps...
 
Upvote 0
Hi snowblizz,

As for why - because that's how VBA/VB6 was designed. :)

Hope that helps...
Alright, thanks. Another entry in the big book of "Wow didn't know that".

Yeah, it was mostly an exasperated, rhetoric question. I'm not going to into the amount of frustration this, combined with a lack of incrementing, and array handling I've had. It is almost like VBA wasn't really meant as programming language. :rolleyes::biggrin:
 
Upvote 0
Hi Colin,

Please accept my sincere thanks.

Probably the best answer an amateur programmer like me can get from an expert like you.

It's working fine.

Thank you again.

Regards

Vijay
 
Upvote 0

Forum statistics

Threads
1,222,786
Messages
6,168,220
Members
452,171
Latest member
saeid025

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