I have this macro that loops through the sheets in a workbook and uses Paste Special | Values to
1. paste the values
2. delete the button that ran the macro
3. save the workbook with a new name.
Sometimes the macro doesn't complete. It runs and I can see that the formulas have been converted to values, but the button hasn't been deleted and the workbook hasn't been saved with the new name. I don't see any error when the macro doesn't complete, and if I click the button again, the macro completes all 3 steps.
Here is the macro:
Sub Paste_Values()
Dim Sht As Worksheet
Dim ThisSheet As Worksheet
Dim ws As Worksheet
Application.ScreenUpdating = False
Set ThisSheet = ActiveSheet
'Loop through all cells
For Each Sht In ThisWorkbook.Worksheets
With Sht
.Activate
End With
Cells.Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Next Sht
'End of loop
'Delete the Store Sheet if it exists
For Each ws In ThisWorkbook.Worksheets
If ws.Name = "Store" Then
Application.DisplayAlerts = False
ws.Delete
Application.DisplayAlerts = True
Exit Sub
End If
Next ws
'Application.ScreenUpdating = True
ThisSheet.Select
'Finish on the sheet we started
Set ThisSheet = Nothing
'Delete the button on the workbook copy
Range("A1").Select
ActiveSheet.Shapes("Button 1").Select
Selection.Cut
Application.ScreenUpdating = True
'Save the workbook
MyName = ActiveWorkbook.Name
ActiveWorkbook.SaveAs Filename:="Values_" & MyName
End Sub
I'm not sure how to fix this. Any help would be much appreciated!
Susan Gross
1. paste the values
2. delete the button that ran the macro
3. save the workbook with a new name.
Sometimes the macro doesn't complete. It runs and I can see that the formulas have been converted to values, but the button hasn't been deleted and the workbook hasn't been saved with the new name. I don't see any error when the macro doesn't complete, and if I click the button again, the macro completes all 3 steps.
Here is the macro:
Sub Paste_Values()
Dim Sht As Worksheet
Dim ThisSheet As Worksheet
Dim ws As Worksheet
Application.ScreenUpdating = False
Set ThisSheet = ActiveSheet
'Loop through all cells
For Each Sht In ThisWorkbook.Worksheets
With Sht
.Activate
End With
Cells.Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Next Sht
'End of loop
'Delete the Store Sheet if it exists
For Each ws In ThisWorkbook.Worksheets
If ws.Name = "Store" Then
Application.DisplayAlerts = False
ws.Delete
Application.DisplayAlerts = True
Exit Sub
End If
Next ws
'Application.ScreenUpdating = True
ThisSheet.Select
'Finish on the sheet we started
Set ThisSheet = Nothing
'Delete the button on the workbook copy
Range("A1").Select
ActiveSheet.Shapes("Button 1").Select
Selection.Cut
Application.ScreenUpdating = True
'Save the workbook
MyName = ActiveWorkbook.Name
ActiveWorkbook.SaveAs Filename:="Values_" & MyName
End Sub
I'm not sure how to fix this. Any help would be much appreciated!
Susan Gross