Run-time error '1004'

waldymar

Board Regular
Joined
Feb 19, 2009
Messages
238
Hi All,
I have the following code
Code:
Sub Automatization()


Dim twb As Workbook, wb As Workbook, new_sheet_counter As Integer


Application.ScreenUpdating = False
    Set twb = ThisWorkbook
    


    new_sheet_counter = Application.SheetsInNewWorkbook
    Set wb = Workbooks.Add
'----- Copy relevant worksheets to new book -----
    twb.Sheets(Array("DashboardTotal", "DashboardMT", "DashboardPT")).Copy After:=wb.Sheets(new_sheet_counter)
'----- Remove original worksheets and set others to values-only -----
    Application.DisplayAlerts = False
    With wb
        For counter = 1 To new_sheet_counter
            .Sheets("Sheet" & counter).Delete
        Next
        For Each ws In wb.Worksheets
            ws.Cells.Copy
            ws.Cells.PasteSpecial (xlPasteValues)
            Application.CutCopyMode = False
        Next
        wb.Sheets("DashboardTotal").Select
    End With
'----- Set up variables for saving report -----
    reportingdate = Format(Date, "dd-mmm-yyyy")
    reportingtime = Format(Time, "hh-mm")
    outputlocation = coding.Range("b1")
    If Right(outputlocation, 1) <> "\" Then outputlocation = outputlocation & "\"
    vsion = 1
'----- Save new workbook with version control -----
    wb.SaveAs Filename:=outputlocation & "Daily Report-" & reportingdate & "-" & reportingtime & ".xlsx"
    wb.Close False
    Application.DisplayAlerts = True
Application.ScreenUpdating = True
    
End Sub

The code stops working on this line:
Code:
wb.SaveAs Filename:=outputlocation & "Daily Report-" & reportingdate & "-" & reportingtime & ".xlsx"

Can anyone spot a bug?

Thanks
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
Have you checked the valeus of outputlocation, reportingdate and reportingtime?
 
Upvote 0
It probably won't make any difference but I'd always stick 'Option Explicit' at the top and explicitly declare those three variables so you can be certain you're passing text strings.

Code:
Option Explicit

Sub Automatization()

Dim twb As Workbook, wb As Workbook, new_sheet_counter As Integer
Dim reportingdate, reportingtime, outputlocation as String

Application.ScreenUpdating = False
    Set twb = ThisWorkbook
...

Regards
Adam
 
Upvote 0
Adam, you are right, I added Dim properties!
Personally, I have concerns about this line:
Code:
outputlocation = coding.Range("b1")
Maybe it doesn't understand in which workbook shall it look for "coding" worksheet?! Any ideas?
 
Upvote 0
Adam, you are right, I added Dim properties!
Personally, I have concerns about this line:
Code:
outputlocation = coding.Range("b1")
Maybe it doesn't understand in which workbook shall it look for "coding" worksheet?! Any ideas?
I thought the variables all had the correct values? Which workbook should the code be looking at.<br><br>Is there any other code?<br><br> PS If you want to dim all the variables as string you need to use something like this.
Code:
Dim reportingdate As String, reportingtime As String, outputlocation As String
 
Upvote 0
I thought the variables all had the correct values? Which workbook should the code be looking at.

Is there any other code?

PS If you want to dim all the variables as string you need to use something like this.
Code:
Dim reportingdate As String, reportingtime As String, outputlocation As String

There is no other code.
Worksheet 'coding' refers to the current workbook (not the added one).
Code:
Set twb = ThisWorkbook

Maybe later, when a new workbook is added it becomes 'ThisWorkbook'?
Thanks for correcting string properties
 
Upvote 0
I meant worksheet, not workbook. Sorry.
 
Upvote 0

Forum statistics

Threads
1,225,156
Messages
6,183,223
Members
453,152
Latest member
ChrisMd

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