Runtime error 91 VBA Macro

Peteor

Board Regular
Joined
Mar 16, 2018
Messages
152
Sub Macro1()
Dim Cel As Range
Dim Rng As Range
Dim Local_Variable As Object


Set Rng = Worksheets("Index").Range("B5:B142")


For Each Cel In Rng
If Cel.Offset(0, 4) <> "X" And Cel.Offset(0, 8).Value = "X" Then
Cel.Offset(0, -1).Hyperlinks(1).Follow
If Application.WorksheetFunction.CountIf(Worksheets("Sheet1").Range("A1:A68"), Range("B4").Value) = 1 Then
Set Local_Variable = Worksheets("Table 1").Range("AL67:AL2255").Find(Range("B4").Value)
Range("G25") = Local_Variable.Offset(6, -37).Value
Range("G26") = Local_Variable.Offset(7, -37).Value
Range("G27") = Local_Variable.Offset(8, -37).Value
Range("G28") = Local_Variable.Offset(9, -37).Value
Range("G29") = Local_Variable.Offset(10, -37).Value
Range("G30") = Local_Variable.Offset(11, -37).Value


I have had this issue many times, and worked around it rather than figure it out. Local_Variable causes a runtime error 91. Can anyone help me here?
 

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
Your macro has some details.


- You must declare the variable as Range.
Code:
[COLOR=#333333]Dim Local_Variable As [/COLOR][COLOR=#ff0000][B]Range[/B][/COLOR][COLOR=#333333][/COLOR]


- In this part always look for the value B4 in the range.
Code:
[COLOR=#333333]Set Local_Variable = Worksheets("Table 1").Range("AL67:AL2255").Find([/COLOR][COLOR=#ff0000]Range("B4").Value[/COLOR][COLOR=#333333])[/COLOR]
Every time you cycle, always look for the same value.


- You are not indicating the reference of the sheet.
Code:
[COLOR=#333333]Set Local_Variable = Worksheets("Table 1").Range("AL67:AL2255").Find([/COLOR][B][COLOR=#ff0000]sheets("xxxx").[/COLOR][/B][COLOR=#333333][/COLOR]Range("B4").Value[COLOR=#333333])[/COLOR]

- You should check if there is a sheet with the name "Table 1"
Code:
[COLOR=#333333]Set Local_Variable = Worksheets("[/COLOR][B][COLOR=#ff0000]Table 1[/COLOR][/B][COLOR=#333333]").Range("AL67:AL2255").Find([/COLOR][COLOR=#ff0000]sheets("xxxx")[/COLOR].[COLOR=#333333][/COLOR]Range("B4").Value[COLOR=#333333])[/COLOR]

- You must check if he found the data.
Code:
[COLOR=#333333]   Set Local_Variable = Worksheets([/COLOR]"[COLOR=#ff0000]Table 1[/COLOR]").Range("AL67:AL2255").Find([COLOR=#ff0000]sheets("xxxx").[/COLOR]Range("B4").Value[COLOR=#333333])
[/COLOR][COLOR=#ff0000]   If Not Local_Variable Is Nothing Then[/COLOR]
      Range("G25") = Local_Variable.Offset(6, -37).Value
      Range("G26") = Local_Variable.Offset(7, -37).Value
      Range("G27") = Local_Variable.Offset(8, -37).Value
      Range("G28") = Local_Variable.Offset(9, -37).Value
      Range("G29") = Local_Variable.Offset(10, -37).Value
      Range("G30") = Local_Variable.Offset(11, -37).Value
   End If

If you can describe what you need to do with an example, I will gladly help you correct the code.
 
Upvote 0
Dante,

I am trying to parse a large amount of data between two different spreadsheets.

I have added your comments to my code but am having issues with the find function I believe, as it is always returning "Nothing", even when I know it should not. For instance, on the active sheet Cell B4 will have the value of "W2". On the "Table 1" Sheet, Cell AL67 has the value "W2", yet it still returns nothing as written. Here is the code with your comments:

Sub Macro1()
Dim Cel As Range
Dim Rng As Range
Dim Local_Variable As Range


Set Rng = Worksheets("Index").Range("B5:B142")


For Each Cel In Rng
If Cel.Offset(0, 4) <> "X" And Cel.Offset(0, 8).Value = "X" Then
Cel.Offset(0, -1).Hyperlinks(1).Follow
If Application.WorksheetFunction.CountIf(Worksheets("Sheet1").Range("A1:A68"), Range("B4").Value) = 1 Then
With Worksheets("Table 1").Range("AL67:AL2255")
Set Local_Variable = Worksheets("Table 1").Range("AL67:AL2255").Find(ActiveSheet.Range("B4").Value)
If Not Local_Variable Is Nothing Then
Range("G25") = Local_Variable.Offset(6, -37).Value
Range("G26") = Local_Variable.Offset(7, -37).Value
Range("G27") = Local_Variable.Offset(8, -37).Value
Range("G28") = Local_Variable.Offset(9, -37).Value
Range("G29") = Local_Variable.Offset(10, -37).Value
Range("G30") = Local_Variable.Offset(11, -37).Value
 
Upvote 0
Dante,

I am trying to parse a large amount of data between two different spreadsheets.

I have added your comments to my code but am having issues with the find function I believe, as it is always returning "Nothing", even when I know it should not. For instance, on the active sheet Cell B4 will have the value of "W2". On the "Table 1" Sheet, Cell AL67 has the value "W2", yet it still returns nothing as written. Here is the code with your comments:

you must run the macro on the active sheet that contains the value in B4.

Try
Code:
[COLOR=#333333]Set Local_Variable = Worksheets("Table 1").Range("AL67:AL2255").Find(ActiveSheet.Range("B4").Value, , xlvalues, xlwhole)[/COLOR]
 
Upvote 0

Forum statistics

Threads
1,216,136
Messages
6,129,084
Members
449,485
Latest member
greggy

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