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!
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