Run-time error '1004': Application-defined or object-defined error While searching for shapes within a specified area in worksheet using VBA.

Wonnters

New Member
Joined
Feb 21, 2025
Messages
1
Office Version
  1. 365
  2. 2024
  3. 2016
Platform
  1. Windows
So currently I have mad a whole bunch of programs to allow the user to generate shapes and groups them to save them in a library containing all the "units". Then in another sheet I have a cell drop down list that uses data validation to pull from a table which contains all the Unit names in one column. A generate button will then clear the work area and paste the unit shape group into the unit. This is my code at the moment shown at the bottom. A picture of the work area is also posted at the bottom. For some reason the first time I use generate unit to clear the work area it works fine but as soon as I try to generate a new one the clear function of this code has the error stated in the title. I am so lost. If somebody could help that would be great! Let me know fi there is anything else you may need to help.

VBA Code:
Sub Generate_Unit()
'
' Generate_Unit Macro
'

'

    Dim Unit        As Variant
    Dim workArea    As Variant
    Set workArea = Range("E12:P46")
    Dim sh          As Shape
    Dim FoundCell   As Range
    Dim r           As Object
    
    Unit = Range("C3:D3").Value
    
'    Range("E12:P46").Select
    
    For Each sh In ThisWorkbook.Sheets("Sheet1").Shapes
        If Not Intersect(Range(sh.TopLeftCell, sh.BottomRightCell), workArea) Is Nothing Then
            sh.Select False
        End If
    Next sh
    
'    If Selection Is Empty Then
'        Range("A1").Select
'    Else
'        Selection.Delete
'        Range("A1").Select
'    End If


    Set r = Selection

    If TypeName(r) <> "Range" Then
        Selection.Delete
        Range("A1").Select
    End If

    Range("A1").Select
    
    Set FoundCell = Sheets("Units").Range("A:A").Find(Unit, , xlValues, xlWhole, , , True, , False)
        If Not FoundCell Is Nothing Then
            Sheets("Units").Activate
            FoundCell.Select
            UnitIndex = ActiveCell.Row - 1
            Range("T1").Value = UnitIndex
        End If
            
        
    For Each sh In ActiveSheet.Shapes
        If Not Intersect(Range(sh.TopLeftCell, sh.BottomRightCell), Range("F" & UnitIndex * 35 + 1, "Q" & (UnitIndex + 1) * 35)) Is Nothing Then _
            sh.Select False
    Next sh
    
    Set r = Selection
    If TypeName(r) <> "Range" Then
        Selection.Copy
        Sheets("Sheet1").Activate
        workArea.Select
        ActiveSheet.Paste
        Range("A1").Select
    Else
        MsgBox ("Did not find unit")
        Sheets("Sheet1").Activate
        Range("A1").Select
        Exit Sub
    End If

End Sub

1740164044495.png
 

Forum statistics

Threads
1,226,837
Messages
6,193,254
Members
453,784
Latest member
Chandni

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