Run time error appears on 7th Iteration of code

orangebloss

Board Regular
Joined
Jun 5, 2013
Messages
51
Office Version
  1. 365
Platform
  1. Windows
Hi

My code works fine for the first 6 iterations but then throws a Run-Time error '91': Object variable or With block variable not set and I'm lost as to why! Any thoughts on the cause appreciated

VBA Code:
Sub Updateconfig()

       Dim rngData As Range, cell As Range, rngStart As Range
       Dim Profile As Worksheet, Configtotal As Worksheet
       Dim Configtotaltable As ListObject
       Dim Count As Integer, foundcolumn As Integer, pastecolumn As Integer
      
      
      
      
     
       Set Profile = Worksheets("Profiles")
       Set Configtotal = Worksheets("Config totals")
       Set foundCell = ActiveSheet.Range("H:AC").Find(ActiveCell.Value, , xlValues, xlWhole)
       Set rngStart = ActiveSheet.Range("F6")
       Count = 0
      
      
       Set Configtotaltable = Configtotal.ListObjects("Config_Dates")
       Configtotal.ListObjects("Config_Dates").Range.AutoFilter Field:=3
      '  Configtotal.Select
      Range("H6:AC300").Clear
 
      ActiveSheet.Range("F6").Select
     
     
  
 With Configtotal
        Set rngData = .Range(.Range("E6"), .Range("E" & .Rows.Count).End(xlUp))
      
       End With
   
      ' Establish "For" loop.
      For Each cell In rngData.Cells
         If cell.Value = "CME" Then
            'If the value in the first cell of the row is CME then
           
            CopyCMEData
             Set foundCell = ActiveSheet.Range("H:AC").Find(ActiveCell.Value, , xlValues, xlWhole)
            ' MsgBox (foundCell)
            foundcolumn = foundCell.Column
            ' MsgBox (foundcolumn)
           
             pastecolumn = foundcolumn - 6
            ' MsgBox (pastecolumn)
             rngStart.Offset(Count, pastecolumn).PasteSpecial paste:=xlPasteValues
          Application.CutCopyMode = False
            Count = Count + 1
            rngStart.Offset(Count, 0).Select
        
         
          
Else
CopyNPDData
Set foundCell = ActiveSheet.Range("H:AC").Find(ActiveCell.Value, , xlValues, xlWhole)
             Set foundCell = ActiveSheet.Range("H:AC").Find(ActiveCell.Value, , xlValues, xlWhole)
             'MsgBox (foundCell)
             foundcolumn = foundCell.Column
               
             '   MsgBox (foundcolumn)
             pastecolumn = foundcolumn - 6
            
           '  MsgBox (pastecolumn)
             rngStart.Offset(Count, pastecolumn).PasteSpecial paste:=xlPasteValues
          Application.CutCopyMode = False
            Count = Count + 1
            rngStart.Offset(Count, 0).Select
          
    End If
    
      Next cell
End Sub

The code looks at the RStart date, finds the column that the date is in and then pastes predetermined values - this works fine until the 7th iteration when it throws a wobbly when it gets to this section of code:
VBA Code:
  CopyCMEData
             Set foundCell = ActiveSheet.Range("H:AC").Find(ActiveCell.Value, , xlValues, xlWhole)
            ' MsgBox (foundCell)
            foundcolumn = foundCell.Column

LeverRStart2022/012022/022022/032022/042022/052022/062022/072022/082022/092022/102022/11
NPD2022/03
0.05882353​
0​
0.11764706​
0​
0.11764706​
0​
1.05882353​
0​
1.35294118​
CME2022/05
0.05882353​
0​
0.02941176​
0​
0.02941176​
0​
0.70588235​
CME2022/09
0.05882353​
0​
0.02941176​
CME2022/10
0.05882353​
0​
CME2022/10
0.05882353​
0​
CME2022/11
0.05882353​
CME2022/29
CME2022/30
 

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
VBA Code:
Set foundCell = ActiveSheet.Range("H:AC").Find(ActiveCell.Value, , xlValues, xlWhole)

That searches for a value using the range "Find" method. You have to handle the scenario where nothing is found. Example
VBA Code:
    If foundCell Is Nothing Then
        MsgBox "'" & ActiveCell.Value & "' not found in range", vbOKOnly Or vbCritical, Application.Name
        Exit Sub
    End If
 
Upvote 0
Solution
VBA Code:
Set foundCell = ActiveSheet.Range("H:AC").Find(ActiveCell.Value, , xlValues, xlWhole)

That searches for a value using the range "Find" method. You have to handle the scenario where nothing is found. Example
VBA Code:
    If foundCell Is Nothing Then
        MsgBox "'" & ActiveCell.Value & "' not found in range", vbOKOnly Or vbCritical, Application.Name
        Exit Sub
    End If
Thanks! I also found out that I hadn't extended the find range far enough!
 
Upvote 0

Forum statistics

Threads
1,215,061
Messages
6,122,921
Members
449,094
Latest member
teemeren

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