Go Back  Xtreme Visual Basic Talk > Legacy Visual Basic (VB 4/5/6) > VBA / Office Integration > Excel > Runtime error 1004


Reply
 
Thread Tools Display Modes
  #1  
Old 06-22-2012, 04:04 AM
justinng justinng is offline
Newcomer
 
Join Date: Jun 2009
Posts: 12
Default Runtime error 1004


Hi all,

I have the runtime error 1004 when I am trying to run the macro in excel 2010. The marco works perfectly fine if I am using excel 2003 but not any excel newer than 2003.

The error message says Microsoft Excel cannot access to the path. But I am puzzled.

The macro looks like this:

Quote:
Option Explicit

Sub importdata()

Dim rngFullName As Range
Dim wbOpen As Workbook
Dim wsOpenPL As Worksheet
Dim wsOpenBS As Worksheet
Dim wbData As Workbook
Dim wsDataCtrl As Worksheet
Dim wsDataMP As Worksheet
Dim wsDataBS As Worksheet
Dim wsDataPL As Worksheet
Dim rngDataPL As Range
Dim rngDataBS As Range
Dim rngCell As Range
Dim strControl As String
Dim lngNext As Long
Dim rngFound As Range
Dim sb As Class1, i As Long

Application.ScreenUpdating = False

strControl = "Failed"
'setting objects on ThisWorkbook and ThisWorkbook.ActiveSheet
Set wbData = ThisWorkbook
Set wsDataCtrl = wbData.Sheets("Control")
Set wsDataMP = wbData.Sheets("Main_Page")
Set wsDataPL = wbData.Sheets("Original_CPL")
Set wsDataBS = wbData.Sheets("Original_CBS")
Set sb = New Class1 ' create a new progress bar

Set rngFullName = wsDataCtrl.Range("H5")
'if the workbboks always are located in the same directory, use
' ThisWorkbook.Path & Application.PathSeparator & Filename & ".xls"

On Error GoTo ErrorRoutine
'open workbook with data and setting objects to both Workbook and Worksheet
Set wbOpen = Workbooks.Open(rngFullName, UpdateLinks:=False)
Set wsOpenPL = wbOpen.Sheets("REPORT-PL")
Set wsOpenBS = wbOpen.Sheets("REPORT-BS")

'indicating the rows for data delivered by VLookup
Set rngDataPL = wsDataPL.Range("A13:A421").SpecialCells(xlCellTypeConstants, 23)
Set rngDataBS = wsDataBS.Range("A13:A421").SpecialCells(xlCellTypeConstants, 23)
Thank you

Best Regards,
Justin
Reply With Quote
  #2  
Old 06-22-2012, 08:23 PM
hDC_0 hDC_0 is offline
Contributor

* Expert *
 
Join Date: Feb 2004
Posts: 522
Default excel 2010 vba error 1004

Quote:
Originally Posted by justinng
I have the runtime error 1004 when I am trying to run the macro in excel 2010.
The error message says Microsoft Excel cannot access to the path. But I am puzzled.
This "Explanation of Trappable Errors in Visual Basic for Apps" MSDN page
offers the following description for Excel error 1004:
Quote:
[Method name] method of [object] class failed (Error 1004)

An external error occurred, such as a failure to read or write from a file.
The method cannot be used on the object.
Possible reasons include the following:

An argument contains a value that isn't valid.
A common cause of this problem is an attempt to access an object that doesn't exist
[for example, you tried to use Workbooks(5) when there were only three workbooks open].
The method cannot be used in the applied context.
For example, some Range object methods require that the range contain data; if the range doesn't contain data, the method fails.

An external error occurred, such as a failure to read or write from a file.
Looking at this code line:
Code:
' ThisWorkbook.Path & Application.PathSeparator & Filename & ".xls"
..brought to mind this StackOverFlow thread which had as an answer:
Quote:
The error is because your file extension (xls) doesn't match your file type (OpenXMLWorkbookMacroEnabled).
You would need the xlsm extension.
If your work environment utilizes SharePoint, I also found this page which recommended
this hotfix for Excel 2007 (which may also work for Excel 2010).

One of the other differences between excel 2003 and excel 2007/2010 is something call "workbook protection"
which can result in an error 1004.
This page talks about removing workbook protection.

If workbook protection proves to be the sticking point
there is also code in the above linked to thread that shows
how to use "ActiveSheet.Unprotect" in code, as follows:

If you want the worksheets to generally remain protected during use,
you could modify the code to unprotect the sheet before taking action
in code to cells/charts/other objects on it
and then re-apply the protection when that is all done. Like this:

Code:
ActiveSheet.Unprotect
ActiveSheet.ChartObjects("Chart 1").Activate
ActiveChart.ChartArea.Select
ActiveChart.Axes(xlCategory).Select
With ActiveChart.Axes(xlCategory)
If Range("J41") = "TIP" Then
.MinimumScale = 0.7
.MaximumScale = 1.05
Else
.MinimumScale = 0.15
.MaximumScale = 0.71
End If
End With
ActiveSheet.Protect
If the sheets are protected with a password, you have to provide that with the statements like this:
Code:
ActiveSheet.Unprotect password:="sheet password"
and
Code:
ActiveSheet.Protect password:="sheet password"
Naturally you'd replace sheet password with the actual password for the sheet.

Last edited by hDC_0; 06-22-2012 at 08:55 PM.
Reply With Quote
  #3  
Old 06-22-2012, 08:32 PM
Kluz's Avatar
Kluz Kluz is offline
Sapience.Aquire

Super Moderator
* Expert *
 
Join Date: Oct 2003
Location: Lake Bluff, Ill., U.S.
Posts: 3,306
Default

Is the workbook that you are trying to open a *.xls or an *.xlsx, *.xlsm?
Let is know on what line the error occurs, otherwise we have to resort to guessing.
__________________
No the other right mouse click
Reply With Quote
  #4  
Old 06-26-2012, 12:59 AM
justinng justinng is offline
Newcomer
 
Join Date: Jun 2009
Posts: 12
Default

Thanks for your replies.

I have tried adding the code Const sFILTER As String = "*.xlsm, *.xlsm"but to no avail.
Reply With Quote
  #5  
Old 06-26-2012, 01:07 AM
justinng justinng is offline
Newcomer
 
Join Date: Jun 2009
Posts: 12
Default

Thanks for your replies.

I have tried adding the code Const sFILTER As String = "*.xlsm, *.xlsm"but to no avail. I have attached the screen shot of the error massage. I guess the error was due to the file extension. Those files that I will be importing is 2003 version. How do I overcome this version so that excel 2010 will be able to read and extract the file from the path is indicated.

The file path that I pre-defined is without file extension as I understand for excel 2003, we do not have to define the file extension.

I really appreciate your help. I have not able to run the macro since I am using the latest excel version. I need to do manual copy and paste the data from 40 over workbooks into this master workbook.

Thank you
Attached Files
File Type: pdf Screen shot for the error 1004.pdf (533.4 KB, 3 views)
Reply With Quote
  #6  
Old 06-26-2012, 08:09 PM
Kluz's Avatar
Kluz Kluz is offline
Sapience.Aquire

Super Moderator
* Expert *
 
Join Date: Oct 2003
Location: Lake Bluff, Ill., U.S.
Posts: 3,306
Default

The file extension still exists even if it is not displayed by Windows. You can Google on how to display them on your operation system version. For instance, an *.csv file can be opened by Excel and, depending upon your set-up even displays an Excel logo in the file manager/explorer window.
__________________
No the other right mouse click
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
 
 
-->