Run time error 1004<p>Hi, I am a newbies in VBA. With the code attached, I have encountered the run time error 1004 with message Application defined or object defined error. Appreciate your helps. Thanks alot</p> <p align=left> </p> <p align=left>Sub Copy_To_Workbooks()<br>    Dim CalcMode As Long<br>    Dim ws1 As Worksheet<br>    Dim ws2 As Worksheet<br>    Dim WSNew As Worksheet<br>    Dim rng As Range<br>    Dim cell As Range<br>    Dim Lrow As Long<br>    Dim foldername As String<br>    Dim MyPath As String<br>    Dim FieldNum As Integer<br>    Dim FileExtStr As String<br>    Dim FileFormatNum As Long<br>    Dim NewFn As String<br>    NewFn = Format(Sheets(&quot;Rpt_BkgTrend&quot;).Range(&quot;C2&quot;), &quot;yymmdd&quot;)<br>    <br>    Dim ws3 As Worksheet<br>    Dim ws4 As Worksheet<br>    Dim WsNew1 As Worksheet<br>    Dim rng1 As Range<br>    Dim rng2 As Range<br>    Dim wb As Workbook<br>    <br>    <br>    'Name of the sheet with your data<br>    Set ws1 = Sheets(&quot;Rpt_BkgTrend&quot;)  '&lt;&lt;&lt; Change<br>    Set ws3 = Sheets(&quot;Rpt_DepMth&quot;)<br>    <br>   <br>    'Determine the Excel version and file extension/format<br>    If Val(Application.Version) &lt; 12 Then<br>        'You use Excel 97-2003<br>        FileExtStr = &quot;.xls&quot;: FileFormatNum = -4143<br>    Else<br>        'You use Excel 2007<br>        If ws1.Parent.FileFormat = 56 Then<br>            FileExtStr = &quot;.xls&quot;: FileFormatNum = 56<br>        Else<br>            FileExtStr = &quot;.xlsx&quot;: FileFormatNum = 51<br>        End If<br>    End If</p> <p align=left>    'Set filter range : A9 is the top left cell of your filter range and<br>    'the header of the first column, V is the last column in the filter range<br>    Set rng = ws1.Range(&quot;A9:V&quot; &amp; Rows.Count)<br>    Set rng1 = ws3.Range(&quot;A9:AE&quot; &amp; Rows.Count)<br>      <br>    <br>    'Set Field number of the filter column<br>    'Field:=1 is column A, 2 = column B, ......<br>    FieldNum = 1</p> <p align=left>    With Application<br>        CalcMode = .Calculation<br>        .Calculation = xlCalculationManual<br>        .ScreenUpdating = False<br>    End With</p> <p align=left>    ' Add worksheet to copy/Paste the unique list<br>    Set ws2 = Worksheets.Add<br>    </p> <p align=left>    MyPath = &quot;C:\Documents and Settings\jc\Desktop\Working&quot;</p> <p align=left>    'Add a slash at the end if the user forget it<br>    If Right(MyPath, 1) &lt;&gt; &quot;\&quot; Then<br>        MyPath = MyPath &amp; &quot;\&quot;<br>    End If</p> <p align=left>    'Create folder for the new files<br>    foldername = MyPath &amp; Format(Now, &quot;yyyy-mm-dd hh-mm-ss&quot;) &amp; &quot;\&quot;<br>    MkDir foldername</p> <p align=left>    With ws2<br>        'first we copy the Unique data from the filter field to ws2<br>        rng.Columns(FieldNum).AdvancedFilter _<br>                Action:=xlFilterCopy, _<br>                CopyToRange:=.Range(&quot;A1&quot;), Unique:=True<br>        rng1.Columns(FieldNum).AdvancedFilter _<br>                Action:=xlFilterCopy, _<br>                CopyToRange:=.Range(&quot;A1000&quot;), Unique:=True<br>         <br>        'Replace value<br>        Cells.Replace What:=&quot;Market&quot;, Replacement:=&quot;&quot;, LookAt:=xlPart, _<br>        SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _<br>        ReplaceFormat:=False<br>        <br>        'Sort data<br>        ws2.Range(&quot;A1:A2000&quot;).Sort _<br>        Key1:=ws2.Range(&quot;A1&quot;)<br>        <br>        <br>        Set rng2 = ws2.Range(&quot;A1:A&quot; &amp; Rows.Count)<br>        rng2.Columns(FieldNum).AdvancedFilter _<br>                Action:=xlFilterCopy, _<br>                CopyToRange:=.Range(&quot;B1&quot;), Unique:=True<br>                <br>        Set rng3 = ws2.Range(&quot;B2:B&quot; &amp; Rows.Count)<br>        rng3.Columns(FieldNum).AdvancedFilter _<br>                Action:=xlFilterCopy, _<br>                CopyToRange:=.Range(&quot;C1&quot;), Unique:=True<br>        <br>        <br>        'loop through the unique list in ws2 and filter/copy to a new workbook<br>        Lrow = .Cells(Rows.Count, &quot;C&quot;).End(xlUp).Row<br>        For Each cell In .Range(&quot;C2:C&quot; &amp; Lrow)</p> <p align=left>            'Add new workbook with 2 sheets<br>             <br>            Set wb = Workbooks.Add(1)<br>        <br>            Set WSNew = wb.Worksheets.Add<br>            WSNew.Name = &quot;Rpt_BkgTrend_Market&quot;<br>            Set WsNew1 = wb.Sheets(&quot;Sheet1&quot;)<br>            WsNew1.Name = &quot;Rpt_DepMth_Market&quot;<br>            <br>                <br>            'Firstly, remove the AutoFilter<br>            ws1.AutoFilterMode = False<br>            ws3.AutoFilterMode = False<br>            </p> <p align=left>            'Filter the range<br>            rng.AutoFilter Field:=FieldNum, Criteria1:=&quot;=&quot; &amp; cell.Value<br>            rng1.AutoFilter Field:=FieldNum, Criteria1:=&quot;=&quot; &amp; cell.Value<br>            <br>            'Copy the Header 1 to 8<br>            ws1.Rows(&quot;1:8&quot;).Copy<br>            With WSNew.Range(&quot;A1&quot;)<br>                .PasteSpecial Paste:=8<br>                .PasteSpecial xlPasteValues<br>                .PasteSpecial xlPasteFormats<br>            End With<br>            <br>            ws1.AutoFilter.Range.Copy<br>            With WSNew.Range(&quot;A9&quot;)<br>                ' Paste:=8 will copy the columnwidth in Excel 2000 and higher<br>                .PasteSpecial Paste:=8<br>                .PasteSpecial xlPasteValues<br>                .PasteSpecial xlPasteFormats<br>                Application.CutCopyMode = False<br>                .Select<br>            End With<br>            <br>            Application.CutCopyMode = False<br>              <br>            <br>            ws3.Activate<br>            <br>            'Copy the Header 1 to 8<br>            ws3.Rows(&quot;1:8&quot;).Copy<br>            With WsNew1.Range(&quot;A1&quot;)<br>                .PasteSpecial Paste:=8<br>                .PasteSpecial xlPasteValues<br>                .PasteSpecial xlPasteFormats<br>            End With<br>            <br>            ws3.AutoFilter.Range.Copy<br>            With WsNew1.Range(&quot;A9&quot;)<br>                ' Paste:=8 will copy the columnwidth in Excel 2000 and higher<br>                .PasteSpecial Paste:=8<br>                .PasteSpecial xlPasteValues<br>                .PasteSpecial xlPasteFormats<br>                Application.CutCopyMode = False<br>                .Select<br>            End With<br>            <br>            Application.CutCopyMode = False<br>            </p> <p align=left>           <br>            <br>           'Save the file in the new folder and close it<br>           'WSNew.Parent.SaveAs foldername &amp; NewFn &amp; &quot;_&quot; _<br>            '     &amp; cell.Value &amp; FileExtStr, FileFormatNum<br>           'WSNew.Parent.Close False</p> <p align=left>            wb.SaveAs foldername &amp; NewFn &amp; &quot;_&quot; _<br>                 &amp; cell.Value &amp; FileExtStr, FileFormatNum<br>            <br>                        <br>                        <br>            'Close AutoFilter<br>           ws1.AutoFilterMode = False<br>           ws3.AutoFilterMode = False</p> <p align=left>        Next cell</p> <p align=left>        'Delete the ws2 sheet<br>        On Error Resume Next<br>        Application.DisplayAlerts = False<br>        .Delete<br>        Application.DisplayAlerts = True<br>        On Error GoTo 0</p> <p align=left>    End With</p> <p align=left><br>    MsgBox &quot;Look in &quot; &amp; foldername &amp; &quot; for the files&quot;</p> <p align=left>    With Application<br>        .ScreenUpdating = True<br>        .Calculation = CalcMode<br>    End With</p> <p align=left>End Sub</p> <p align=left><br> </p> <p align=left><font face=Arial size=2></font> </p>© 2009 Microsoft Corporation. All rights reserved.Wed, 10 Dec 2008 23:25:30 Z64a1df78-2a10-4fb4-823c-04565867bb7dhttp://social.msdn.microsoft.com/Forums/en-US/isvvba/thread/64a1df78-2a10-4fb4-823c-04565867bb7d#64a1df78-2a10-4fb4-823c-04565867bb7dhttp://social.msdn.microsoft.com/Forums/en-US/isvvba/thread/64a1df78-2a10-4fb4-823c-04565867bb7d#64a1df78-2a10-4fb4-823c-04565867bb7dJC88http://social.msdn.microsoft.com/Profile/en-US/?user=JC88Run time error 1004<p>Hi, I am a newbies in VBA. With the code attached, I have encountered the run time error 1004 with message Application defined or object defined error. Appreciate your helps. Thanks alot</p> <p align=left> </p> <p align=left>Sub Copy_To_Workbooks()<br>    Dim CalcMode As Long<br>    Dim ws1 As Worksheet<br>    Dim ws2 As Worksheet<br>    Dim WSNew As Worksheet<br>    Dim rng As Range<br>    Dim cell As Range<br>    Dim Lrow As Long<br>    Dim foldername As String<br>    Dim MyPath As String<br>    Dim FieldNum As Integer<br>    Dim FileExtStr As String<br>    Dim FileFormatNum As Long<br>    Dim NewFn As String<br>    NewFn = Format(Sheets(&quot;Rpt_BkgTrend&quot;).Range(&quot;C2&quot;), &quot;yymmdd&quot;)<br>    <br>    Dim ws3 As Worksheet<br>    Dim ws4 As Worksheet<br>    Dim WsNew1 As Worksheet<br>    Dim rng1 As Range<br>    Dim rng2 As Range<br>    Dim wb As Workbook<br>    <br>    <br>    'Name of the sheet with your data<br>    Set ws1 = Sheets(&quot;Rpt_BkgTrend&quot;)  '&lt;&lt;&lt; Change<br>    Set ws3 = Sheets(&quot;Rpt_DepMth&quot;)<br>    <br>   <br>    'Determine the Excel version and file extension/format<br>    If Val(Application.Version) &lt; 12 Then<br>        'You use Excel 97-2003<br>        FileExtStr = &quot;.xls&quot;: FileFormatNum = -4143<br>    Else<br>        'You use Excel 2007<br>        If ws1.Parent.FileFormat = 56 Then<br>            FileExtStr = &quot;.xls&quot;: FileFormatNum = 56<br>        Else<br>            FileExtStr = &quot;.xlsx&quot;: FileFormatNum = 51<br>        End If<br>    End If</p> <p align=left>    'Set filter range : A9 is the top left cell of your filter range and<br>    'the header of the first column, V is the last column in the filter range<br>    Set rng = ws1.Range(&quot;A9:V&quot; &amp; Rows.Count)<br>    Set rng1 = ws3.Range(&quot;A9:AE&quot; &amp; Rows.Count)<br>      <br>    <br>    'Set Field number of the filter column<br>    'Field:=1 is column A, 2 = column B, ......<br>    FieldNum = 1</p> <p align=left>    With Application<br>        CalcMode = .Calculation<br>        .Calculation = xlCalculationManual<br>        .ScreenUpdating = False<br>    End With</p> <p align=left>    ' Add worksheet to copy/Paste the unique list<br>    Set ws2 = Worksheets.Add<br>    </p> <p align=left>    MyPath = &quot;C:\Documents and Settings\jc\Desktop\Working&quot;</p> <p align=left>    'Add a slash at the end if the user forget it<br>    If Right(MyPath, 1) &lt;&gt; &quot;\&quot; Then<br>        MyPath = MyPath &amp; &quot;\&quot;<br>    End If</p> <p align=left>    'Create folder for the new files<br>    foldername = MyPath &amp; Format(Now, &quot;yyyy-mm-dd hh-mm-ss&quot;) &amp; &quot;\&quot;<br>    MkDir foldername</p> <p align=left>    With ws2<br>        'first we copy the Unique data from the filter field to ws2<br>        rng.Columns(FieldNum).AdvancedFilter _<br>                Action:=xlFilterCopy, _<br>                CopyToRange:=.Range(&quot;A1&quot;), Unique:=True<br>        rng1.Columns(FieldNum).AdvancedFilter _<br>                Action:=xlFilterCopy, _<br>                CopyToRange:=.Range(&quot;A1000&quot;), Unique:=True<br>         <br>        'Replace value<br>        Cells.Replace What:=&quot;Market&quot;, Replacement:=&quot;&quot;, LookAt:=xlPart, _<br>        SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _<br>        ReplaceFormat:=False<br>        <br>        'Sort data<br>        ws2.Range(&quot;A1:A2000&quot;).Sort _<br>        Key1:=ws2.Range(&quot;A1&quot;)<br>        <br>        <br>        Set rng2 = ws2.Range(&quot;A1:A&quot; &amp; Rows.Count)<br>        rng2.Columns(FieldNum).AdvancedFilter _<br>                Action:=xlFilterCopy, _<br>                CopyToRange:=.Range(&quot;B1&quot;), Unique:=True<br>                <br>        Set rng3 = ws2.Range(&quot;B2:B&quot; &amp; Rows.Count)<br>        rng3.Columns(FieldNum).AdvancedFilter _<br>                Action:=xlFilterCopy, _<br>                CopyToRange:=.Range(&quot;C1&quot;), Unique:=True<br>        <br>        <br>        'loop through the unique list in ws2 and filter/copy to a new workbook<br>        Lrow = .Cells(Rows.Count, &quot;C&quot;).End(xlUp).Row<br>        For Each cell In .Range(&quot;C2:C&quot; &amp; Lrow)</p> <p align=left>            'Add new workbook with 2 sheets<br>             <br>            Set wb = Workbooks.Add(1)<br>        <br>            Set WSNew = wb.Worksheets.Add<br>            WSNew.Name = &quot;Rpt_BkgTrend_Market&quot;<br>            Set WsNew1 = wb.Sheets(&quot;Sheet1&quot;)<br>            WsNew1.Name = &quot;Rpt_DepMth_Market&quot;<br>            <br>                <br>            'Firstly, remove the AutoFilter<br>            ws1.AutoFilterMode = False<br>            ws3.AutoFilterMode = False<br>            </p> <p align=left>            'Filter the range<br>            rng.AutoFilter Field:=FieldNum, Criteria1:=&quot;=&quot; &amp; cell.Value<br>            rng1.AutoFilter Field:=FieldNum, Criteria1:=&quot;=&quot; &amp; cell.Value<br>            <br>            'Copy the Header 1 to 8<br>            ws1.Rows(&quot;1:8&quot;).Copy<br>            With WSNew.Range(&quot;A1&quot;)<br>                .PasteSpecial Paste:=8<br>                .PasteSpecial xlPasteValues<br>                .PasteSpecial xlPasteFormats<br>            End With<br>            <br>            ws1.AutoFilter.Range.Copy<br>            With WSNew.Range(&quot;A9&quot;)<br>                ' Paste:=8 will copy the columnwidth in Excel 2000 and higher<br>                .PasteSpecial Paste:=8<br>                .PasteSpecial xlPasteValues<br>                .PasteSpecial xlPasteFormats<br>                Application.CutCopyMode = False<br>                .Select<br>            End With<br>            <br>            Application.CutCopyMode = False<br>              <br>            <br>            ws3.Activate<br>            <br>            'Copy the Header 1 to 8<br>            ws3.Rows(&quot;1:8&quot;).Copy<br>            With WsNew1.Range(&quot;A1&quot;)<br>                .PasteSpecial Paste:=8<br>                .PasteSpecial xlPasteValues<br>                .PasteSpecial xlPasteFormats<br>            End With<br>            <br>            ws3.AutoFilter.Range.Copy<br>            With WsNew1.Range(&quot;A9&quot;)<br>                ' Paste:=8 will copy the columnwidth in Excel 2000 and higher<br>                .PasteSpecial Paste:=8<br>                .PasteSpecial xlPasteValues<br>                .PasteSpecial xlPasteFormats<br>                Application.CutCopyMode = False<br>                .Select<br>            End With<br>            <br>            Application.CutCopyMode = False<br>            </p> <p align=left>           <br>            <br>           'Save the file in the new folder and close it<br>           'WSNew.Parent.SaveAs foldername &amp; NewFn &amp; &quot;_&quot; _<br>            '     &amp; cell.Value &amp; FileExtStr, FileFormatNum<br>           'WSNew.Parent.Close False</p> <p align=left>            wb.SaveAs foldername &amp; NewFn &amp; &quot;_&quot; _<br>                 &amp; cell.Value &amp; FileExtStr, FileFormatNum<br>            <br>                        <br>                        <br>            'Close AutoFilter<br>           ws1.AutoFilterMode = False<br>           ws3.AutoFilterMode = False</p> <p align=left>        Next cell</p> <p align=left>        'Delete the ws2 sheet<br>        On Error Resume Next<br>        Application.DisplayAlerts = False<br>        .Delete<br>        Application.DisplayAlerts = True<br>        On Error GoTo 0</p> <p align=left>    End With</p> <p align=left><br>    MsgBox &quot;Look in &quot; &amp; foldername &amp; &quot; for the files&quot;</p> <p align=left>    With Application<br>        .ScreenUpdating = True<br>        .Calculation = CalcMode<br>    End With</p> <p align=left>End Sub</p> <p align=left><br> </p> <p align=left><font face=Arial size=2></font> </p>Fri, 08 Feb 2008 05:03:48 Z2008-02-08T05:03:48Zhttp://social.msdn.microsoft.com/Forums/en-US/isvvba/thread/64a1df78-2a10-4fb4-823c-04565867bb7d#34b67b45-5f9d-490d-bba2-5cc798ba0a27http://social.msdn.microsoft.com/Forums/en-US/isvvba/thread/64a1df78-2a10-4fb4-823c-04565867bb7d#34b67b45-5f9d-490d-bba2-5cc798ba0a27ADGhttp://social.msdn.microsoft.com/Profile/en-US/?user=ADGRun time error 1004<p align=left><font face=Arial size=2>Can you let us know which line gives you the error.</font></p>Fri, 08 Feb 2008 08:31:00 Z2008-02-08T08:31:00Zhttp://social.msdn.microsoft.com/Forums/en-US/isvvba/thread/64a1df78-2a10-4fb4-823c-04565867bb7d#d3d33011-1cfc-48e7-aa3c-c7a33798269bhttp://social.msdn.microsoft.com/Forums/en-US/isvvba/thread/64a1df78-2a10-4fb4-823c-04565867bb7d#d3d33011-1cfc-48e7-aa3c-c7a33798269bDonna Evanshttp://social.msdn.microsoft.com/Profile/en-US/?user=Donna%20EvansRun time error 1004I am having the same problem with a macro in the workbook. I have included the code with error in code block. <p align=left><font face=Arial size=2></font> </p> <p align=left>Sub Save_New_Excel_File(Folder_Name As String, File_Name As String)<br>    Dim Vendor As String<br>    Dim Temp_Folder As String<br>    Dim TheDate<br>    Dim TheTime, TheHour<br>    Dim WB As Workbooks<br>    Set WB = Application.Workbooks<br>    <br>    Vendor = Left(File_Name, Len(File_Name) - 4)<br>    <br>    ' Get the date and time to add to the XLS filename<br>    TheHour = Hour(Time)<br>    If TheHour &gt; 13 Then<br>        TheHour = TheHour - 12<br>    End If<br>    <br>    TheTime = TheHour &amp; &quot;-&quot; &amp; Minute(Time)<br>    TheDate = &quot;-&quot; &amp; Month(Date) &amp; &quot;-&quot; &amp; Day(Date) &amp; &quot;-&quot; &amp; TheTime<br>       <br> <div class=codeseg> <div class=codecontent> <div class=codesniptitle><span style="width:100%">Code Snippet</span></div>    WB.Open FileLocation &amp; &quot;\Master.xls&quot;<br> <p align=left> </p></div></div> <p align=left> </p>    WB.Application.DisplayAlerts = False<br>    Worksheets(&quot;PPI 2&quot;).Visible = False<br>    <br>    If INI_Exists Then<br>        ActiveWorkbook.SaveCopyAs Folder_Name &amp; Vendor &amp; TheDate &amp; &quot;.xls&quot;<br>    Else<br>        ' Example = <a title="file://\\server\Global-backup\">\\server\Global-backup\</a> Docs<br>        Temp_Folder = &quot;<a title="file://\\server\Global-backup\">\\server\Global-backup\</a>&quot; &amp; UserName &amp; &quot;'s Docs\Board Orders\&quot;<br>        'Temp_Folder = &quot;<a title="file://server1/C-Drive/Layouts/">\\server1\C-Drive\Layouts\</a>&quot; &amp; UserName &amp; &quot;'s Docs\&quot;<br>        ActiveWorkbook.SaveCopyAs Temp_Folder &amp; Vendor &amp; TheDate &amp; &quot;.xls&quot;<br>    End If<br>    <br>    WB(&quot;MASTER.XLS&quot;).Close SaveChanges = False<br>    <br>    If INI_Exists Then<br>        WB.Open Folder_Name &amp; Vendor &amp; TheDate &amp; &quot;.xls&quot;<br>    Else<br>        WB.Open Temp_Folder &amp; Vendor &amp; TheDate &amp; &quot;.xls&quot;<br>    End If<br>    <br>    Enter_Data_Cells Vendor, Folder_Name<br>    <br>    WB(Vendor &amp; TheDate &amp; &quot;.xls&quot;).Save<br>    'WB(Vendor &amp; &quot;.xls&quot;).Close<br>    <br>End Sub <p></p>Fri, 15 Aug 2008 20:29:14 Z2008-08-15T20:29:14Zhttp://social.msdn.microsoft.com/Forums/en-US/isvvba/thread/64a1df78-2a10-4fb4-823c-04565867bb7d#1a827717-28b3-4191-8de5-3312e2c58766http://social.msdn.microsoft.com/Forums/en-US/isvvba/thread/64a1df78-2a10-4fb4-823c-04565867bb7d#1a827717-28b3-4191-8de5-3312e2c58766Shasurhttp://social.msdn.microsoft.com/Profile/en-US/?user=ShasurRun time error 1004<p align=left><font face=Arial size=2>Hi </font></p> <p align=left> </p> <p align=left>One possibility is that the file is not available in required location. Vatriable 'FileLocation ' in the code does not have any value!</p> <p align=left> </p> <p align=left> <div class=codeseg> <div class=codecontent> <div class=codesniptitle><span style="width:100%">Code Snippet</span></div> <p align=left>If Len(Dir(FileLocation &amp; &quot;\Master.xls&quot;)) &lt;&gt; 0 Then<br>       WB.Open FileLocation &amp; &quot;\Master.xls&quot;<br>   Else<br>       MsgBox &quot;File Not Available in required location!&quot;<br>       Exit Sub<br>   End If</p> <p align=left> </p></div></div> <p align=left> </p> <p>You can use the above code to check for the same</p> <p align=left> </p> <p align=left>(<a title="http://vbadud.blogspot.com/2008/05/excel-vba-1004-file-could-not-be.html" href="http://vbadud.blogspot.com/2008/05/excel-vba-1004-file-could-not-be.html">http://vbadud.blogspot.com/2008/05/excel-vba-1004-file-could-not-be.html</a>)</p> <p align=left> </p> <p align=left>Cheers</p> <p align=left>Shasur</p>Sat, 16 Aug 2008 07:43:39 Z2008-08-16T07:43:39Z