Run Time Error when Selecting Cell VBA

Cassie_H

New Member
Joined
Dec 29, 2011
Messages
9
Hello all,

Merry Christmas and Happy New years!
I am having an issue with VBA Scripting. This is what I've done:

Sub Create_Dynamic_Forecast()
'
' Create_Dynamic_Forecast
Application.ScreenUpdating = True
'ActiveWorkbook.Save
Sheets.Add After:=Sheets(Sheets.Count)
ActiveSheet.Name = "Dynamic Forecast"
Sheets("Emergency Proforma").Select
Cells.Select
Range("B1").Activate
Selection.Copy
Sheets("Dynamic Forecast").Select
ActiveSheet.Paste
ActiveWindow.DisplayGridlines = False
ActiveSheet.Shapes("Button 1").Select
Application.CutCopyMode = False
Selection.Delete
Application.CutCopyMode = False
Range("C31").Select
Selection.ClearContents
Range("D2:K2").Select
ActiveCell.FormulaR1C1 = "30 Day Dynamic Forecast"


Application.ScreenUpdating = True
MsgBox ("Dynamic Forecast saved to C:\Temp")

End Sub

It's working until Range("C31").Select and then it bombs out with:

"Run-time error '1004': Application-defined or object-defined error"

I understand that the error relates to forms, controls, and so on but not why its happening. I just want to clear the contents of the cell on the new sheet.

Believe it or not, i'm not very good at VBA! :eek:
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
Hi Cassie and Welcome to the Board
Try
Code:
Sub Create_Dynamic_Forecast()
'
' Create_Dynamic_Forecast
Application.ScreenUpdating = False 'should false at beginning of code
ActiveWorkbook.Save
Sheets.Add After:=Sheets(Sheets.Count)
ActiveSheet.Name = "Dynamic Forecast"
Sheets("Emergency Proforma").Range("B1").Copy Destination:=Sheets("Dynamic Forecast").Range("B1")
ActiveWindow.DisplayGridlines = False
ActiveSheet.Shapes("Button 1").Delete
Application.CutCopyMode = False
Sheets("Dynamic Forecast").Range("C31").ClearContents
Range("D2:K2").Value = "30 Day Dynamic Forecast"
Application.ScreenUpdating = True
MsgBox ("Dynamic Forecast saved to C:\Temp")
End Sub
 
Upvote 0
Thank you so much for replying, but it's still not working! I tried to cut and paste your response and I got

"Run-time error '5':
Invalid procedure call or argument"

So then I stole the last bit of yours and made this:

Sub Create_Dynamic_Forecast()
'
' Create_Dynamic_Forecast
Application.ScreenUpdating = True

'ActiveWorkbook.Save
Sheets.Add After:=Sheets(Sheets.Count)
ActiveSheet.Name = "Dynamic Forecast"
Sheets("Emergency Proforma").Select
Cells.Select
Range("B1").Activate
Selection.Copy
Sheets("Dynamic Forecast").Select
ActiveSheet.Paste
ActiveWindow.DisplayGridlines = False
ActiveSheet.Shapes("Button 1").Select
Application.CutCopyMode = False
Selection.Delete
Application.CutCopyMode = False
Sheets("Dynamic Forecast").Range("C31").ClearContents
Range("D2:K2").Value = "30 Day Dynamic Forecast"



Application.ScreenUpdating = True
MsgBox ("Dynamic Forecast saved to C:\Temp")


End Sub


And I got my 1004 error again. I might be a bit thick!
 
Upvote 0
Try that, note to change the file path and name to suit
Code:
Sub Create_Dynamic_Forecast()
'
' Create_Dynamic_Forecast
Application.ScreenUpdating = False 'should false at beginning of code
ActiveWorkbook.SaveCopyAs Filename:="C:\Test\CopyBook.xls" ' Change path and name to suit
Sheets.Add After:=Sheets(Sheets.Count)
ActiveSheet.Name = "Dynamic Forecast"
Sheets("Emergency Proforma").Range("B1").Copy Destination:=Sheets("Dynamic Forecast").Range("B1")
ActiveWindow.DisplayGridlines = False
ActiveSheet.Shapes("Button 1").Delete
Application.CutCopyMode = False
Sheets("Dynamic Forecast").Range("C31").ClearContents
Range("D2:K2").Value = "30 Day Dynamic Forecast"
Application.ScreenUpdating = True
MsgBox ("Dynamic Forecast saved to C:\Temp")
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,417
Messages
6,124,777
Members
449,187
Latest member
hermansoa

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