Help deleting row from workbook... "Object doesn't support this property or method"

MistakesWereMade

Board Regular
Joined
May 22, 2019
Messages
103
My program is supposed to run the master file, copy itself to a new workbook, delete certain entries, add formulas to a column, save, close, and restart with the master file still open.

It gets to the point where it is supposed to start deleting rows, but it pops up the error "Object doesn't support this property or method". If anyone could help me fix my code, or help optimize it, please let me know!

Thanks in advance!

VBA Code:
Sub GetUniqueAndCount()

    Dim d As Object, c As Range, k, tmp As String, rng As Range, x As Long, i As Long, ThisWb As Workbook, wbTemp As Workbook, ws As Worksheet
    
    ' Optimize Macro Speed
    Application.ScreenUpdating = False
    Application.EnableEvents = False
    Application.Calculation = xlCalculationManual
    Application.DisplayAlerts = False
    
    x = Cells(Rows.Count, 5).End(xlUp).Row
    Set rng = Range("E2:E" & x)
    Set d = CreateObject("scripting.dictionary")
    For Each c In rng
        tmp = Trim(c.Value)
        If Len(tmp) > 0 Then d(tmp) = d(tmp) + 1
    Next c

    For Each k In d.keys
        Debug.Print k, d(k)
        
        Set ThisWb = ThisWorkbook
        Set wbTemp = Workbooks.Add
        
        On Error Resume Next
        For Each ws In wbTemp.Worksheets
            ws.Delete
        Next
        On Error GoTo 0
        
        For Each ws In ThisWb.Sheets
            ws.Copy After:=wbTemp.Sheets(1)
        Next
        
        wbTemp.Sheets(1).Delete
        
        For i = Cells(Rows.Count, 5).End(xlUp).Row To 2 Step -1
            If Cells(i, 5).Value2 <> k Then
                wbTemp.Rows(i).Delete
            End If
        Next i
    
        For i = Cells(Rows.Count, 5).End(xlUp).Row To 3 Step -1
            wbTemp.Range("O" & i).Formula = "=TEXT(M" & i & "-M" & i - 1 & ", ""h:mm:ss"")"
        Next i
        
        wbTemp.Sheets(1).Delete
        wbTemp.SaveAs "C:\Users\wg2017\Desktop\Product Data\" & k & ".xls", 51
        
        DoEvents
        wbTemp.Close SaveChanges:=True
        DoEvents
        
    Next k
    
    'Reset Macro Optimization Settings
ResetSettings:
    Application.EnableEvents = True
    Application.Calculation = xlCalculationAutomatic
    Application.ScreenUpdating = True
    Application.DisplayAlerts = True
    
End Sub
 

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
56,883
Office Version
  1. 365
Platform
  1. Windows
You need to specify which sheet in the new workbook you want to delete the rows from.
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
56,883
Office Version
  1. 365
Platform
  1. Windows
You can also replace this part of your code
VBA Code:
'        Set wbTemp = Workbooks.Add
'
'        On Error Resume Next
'        For Each ws In wbTemp.Worksheets
'            ws.Delete
'        Next
'        On Error GoTo 0
'
'        For Each ws In ThisWb.Sheets
'            ws.Copy After:=wbTemp.Sheets(1)
'        Next
'
'        wbTemp.Sheets(1).Delete
with
VBA Code:
        ThisWb.Sheets.Copy
        Set wbTemp = ActiveWorkbook
 

Watch MrExcel Video

Forum statistics

Threads
1,130,170
Messages
5,640,577
Members
417,151
Latest member
ChickenTenderer

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
Top