ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Runtime Error 1004 - File could not be accessed (https://www.excelbanter.com/excel-programming/386496-runtime-error-1004-file-could-not-accessed.html)

Carlee

Runtime Error 1004 - File could not be accessed
 
Hi there, i am getting a runtime error 1004...that the file could not be
accessed. The code it below, and the line in question is:

sbFile.SaveAs Filename:=WshShell.SpecialFolders("Desktop") & "\Copreco
Reading.xlsx """

================================================
Sub ExportCoprecoReadingData()
Const sourceSheet = "Site Reading Log" ' source
Dim sourceBook As String
Dim destBook As String
Dim sourceRange As Range
Dim destRange As Range

Application.ScreenUpdating = False
sourceBook = ThisWorkbook.Name
'get the new workbook object
Set sbFile = Workbooks.Add ' create new book
destBook = ActiveWorkbook.Name
Windows(sourceBook).Activate
Worksheets(sourceSheet).Select

Set sourceRange = ActiveSheet.Rows("1:1")
Set destRange = Workbooks(destBook).Worksheets("Sheet1").Rows("1:1 ")
destRange.Value = sourceRange.Value

'change "A" to column with data in last row
Range("A" & Rows.Count).End(xlUp).Select
Set sourceRange = ActiveSheet.Rows(ActiveCell.Row & ":" & ActiveCell.Row)
Set destRange = Workbooks(destBook).Worksheets("Sheet1").Rows("2:2 ")
destRange.Value = sourceRange.Value
Set sourceRange = Nothing
Set destRange = Nothing

'get the desktop path, save and close new workbook
Set WshShell = CreateObject("WScript.Shell")
sbFile.SaveAs Filename:=WshShell.SpecialFolders("Desktop") & "\Copreco
Reading.xlsx """
sbFile.Close

Set WshShell = Nothing
Set sbFile = Nothing

End Sub

joel

Runtime Error 1004 - File could not be accessed
 
It failed the second time for me because the item was already on my desktop.
The 1st time it ran perfectly.

"Carlee" wrote:

Hi there, i am getting a runtime error 1004...that the file could not be
accessed. The code it below, and the line in question is:

sbFile.SaveAs Filename:=WshShell.SpecialFolders("Desktop") & "\Copreco
Reading.xlsx """

================================================
Sub ExportCoprecoReadingData()
Const sourceSheet = "Site Reading Log" ' source
Dim sourceBook As String
Dim destBook As String
Dim sourceRange As Range
Dim destRange As Range

Application.ScreenUpdating = False
sourceBook = ThisWorkbook.Name
'get the new workbook object
Set sbFile = Workbooks.Add ' create new book
destBook = ActiveWorkbook.Name
Windows(sourceBook).Activate
Worksheets(sourceSheet).Select

Set sourceRange = ActiveSheet.Rows("1:1")
Set destRange = Workbooks(destBook).Worksheets("Sheet1").Rows("1:1 ")
destRange.Value = sourceRange.Value

'change "A" to column with data in last row
Range("A" & Rows.Count).End(xlUp).Select
Set sourceRange = ActiveSheet.Rows(ActiveCell.Row & ":" & ActiveCell.Row)
Set destRange = Workbooks(destBook).Worksheets("Sheet1").Rows("2:2 ")
destRange.Value = sourceRange.Value
Set sourceRange = Nothing
Set destRange = Nothing

'get the desktop path, save and close new workbook
Set WshShell = CreateObject("WScript.Shell")
sbFile.SaveAs Filename:=WshShell.SpecialFolders("Desktop") & "\Copreco
Reading.xlsx """
sbFile.Close

Set WshShell = Nothing
Set sbFile = Nothing

End Sub



All times are GMT +1. The time now is 06:36 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com