Trying to figure out error 91 with object.

praymond

New Member
Joined
Oct 6, 2023
Messages
2
Office Version
  1. 365
Platform
  1. Windows
VBA Code:
Sub func2()
    'Define variables
    Dim time1 As Date
    Dim time2 As Date
    Dim time3 As Date
    Dim time3Value As Double
    Dim targetFinishTime As Date
    Dim allocatedTaktTime As Date
    Dim targetTime As Date
    Dim breakTime As Date
    Dim lunchTime As Date
    allocatedTaktTime = Range("P6").Value
    
    time1 = TimeValue("06:10:00 AM")
    time2 = TimeValue("09:15:00 AM")
    time3 = TimeValue("12:00:00 PM")
    time3Value = CDbl(time3)
    
    breakTime = Range("D7").Value
    lunchTime = Range("D8").Value

    targetTime = time1 + allocatedTaktTime
    
        If targetTime >= time1 And targetTime <= time2 Then
            
            Dim searchRange As Range
            Dim foundCell As Range
            Dim followingRow As Range
            Dim foundCell2 As Range
            Dim followingRow2 As Range

            Set searchRange = ThisWorkbook.Sheets("Sheet1").UsedRange
            Set foundCell = searchRange.Find(time3Value, LookIn:=xlValues)
                If Not foundCell Is Nothing Then
                MsgBox ("Nothing")
                Else
                MsgBox ("It works")
                End If
            foundCell.Offset(1, 0).Range("A:BO").Copy
            Range("12:12").Insert Shift:=xlDown
            
            
            MsgBox Format(Range("E13").Value, "hh:mm:ss AM/PM")
            MsgBox (Range("E14").Value)
            MsgBox (time3Value)
            
        End If
        
End Sub

The problem is towards the bottom
VBA Code:
foundCell.Offset(1, 0).Range("A:BO").Copy

What I'm trying to do is copying the row A15:BO15 and insert it after row 12.
1696609627592.png
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
The problems I see in your process:
-the vba generated "12:00:00 PM" very likely will be different from the typed 12:00:00 PM; even a zillionth will make the Find to fail. You should preliminarlely scan all the "times" in your worksheet and Round the content of the cell with 5 decimals (this corresponds to 1 second), and similarly round time3Value with the same 5 decimals. Or you shoouldn't use Find, but a loop to scan the right column against time3Value and compare rounded value of column against the rounded value for time3Value
-you should double check that in the sheets your "times" are real times and not pure strings: format them as Numbers with 4 decimals, if they change the displayed value they are "times", otherwise they are strings
-your MsgBox ("It works") will indeed showup when FoundCell be Nothing (ie the Find failed)
-even if Find works and get an address, you cannot address Range("A:BO"); if you need pointing the next row you should use Range("A1:B1") (or you could use "A2:B2" avoiding Offset(1,0))

So your job is more complex than what you thought
 
Upvote 0

Forum statistics

Threads
1,215,137
Messages
6,123,254
Members
449,093
Latest member
Vincent Khandagale

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