Run Time Error

billandrew

Well-known Member
Joined
Mar 9, 2014
Messages
743
Afternoon

Is there a work around to the below seems to work well in other scripts.

The error occurs at the ThisWorkbook.Activate

Thanks All...

Code:
Dim filepath As StringSet wb2 = Workbooks.Add
ThisWorkbook.Activate
ActiveSheet.Copy before:=wb2.Sheets(1)
wb2.Activate
filepath = CreateObject("WScript.Shell").SpecialFolders("Desktop") & Application.PathSeparator
wb2.SaveAs filepath & "Results" & " " & Format(Now, "mm-dd-yy hhmm") & ".xlsx", FileFormat:=51
 

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
Why do you need that line of code ??
Code:
ThisWorkbook.Activate
Are there a number of workbook open ??
Have you tried removing it ?
What are you actually trying to do with the code ??
 
Upvote 0
Michael

Trying to add workbook and save to Desktop.

I've changed to this, however it seems to save to downloads folder not the desktop.

Thanks for any help...

Code:
Dim wb As WorkbookSet wb = Workbooks.Add
ThisWorkbook.Activate
ActiveSheet.Copy before:=wb.Sheets(1)
Dim WSHShell As Object
Dim DesktopPath As String
Set WSHShell = CreateObject("WScript.Shell")
DesktopPath = WSHShell.specialfolders("Desktop")
ActiveWorkbook.SaveAs "Results" & " " & Format(Now, "mm-dd-yy hhmm") & ".xlsx", FileFormat:=51
Set WSHShell = Nothing
 
Upvote 0
You haven't included DesktopPath in your SaveAs line.
 
Upvote 0
Update:

In the OP I may have not stated what I was attempting to attain. Save a worksheet ["Results"] from the current workbook to a new workbook and save to desktop. This script does what I need. Hopefully it helps others.

Thanks to DanteAmor, Michael & Mark for your assistance.



Code:
Dim Filepath As String    Sheets("Results").Copy
    Filepath = CreateObject("WScript.Shell").SpecialFolders("Desktop") & Application.PathSeparator
    ActiveWorkbook.SaveAs filename:=Filepath & "Results" & " " & Format(Now, "mm-dd-yy hhmm") & ".xlsx", FileFormat:=51
 
Upvote 0
Update:

In the OP I may have not stated what I was attempting to attain. Save a worksheet ["Results"] from the current workbook to a new workbook and save to desktop. This script does what I need. Hopefully it helps others.

Thanks to DanteAmor, Michael & Mark for your assistance.

I did not do much, but thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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