473,604 Members | 2,487 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Error: application defined or object defined error

126 New Member
Hi! I am a new beginner to MS Access VBA, I wrote the following VBA code in Access which deletes blank columns and rows in excel file. But, the code works fine for sometimes and sometimes it displays "applicatio n defined or object defined error".

And, when I step into the code, error is occuring at the following Bold line. Please help me with this problem. Thank you in advance.

Sub RunMacro()
On Error GoTo Err_RunMacro
Dim XL As Object
Set XL = CreateObject("E xcel.Applicatio n")
XL.Workbooks.Op en "C:\Test_file.x ls"

With XL.Application
Dim Col As Long, ColCnt As Long, Rng As Range, lastrow As Long, lastcol As Long
XL.ScreenUpdati ng = False
XL.Calculation = xlCalculationMa nual

On Error GoTo Exits:
lastcol = Range("A:A").Sp ecialCells(xlLa stCell).Column
Range(Cells(1, 1), Cells(lastcol, 1)).SpecialCell s(xlCellTypeBla nks).EntireColu mn.Delete

lastrow = Range("A:A").Sp ecialCells(xlLa stCell).Row
Range(Cells(1, 1), Cells(lastrow, 1)).SpecialCell s(xlCellTypeBla nks).EntireRow. Delete

Exits:
XL.ScreenUpdati ng = True
XL.Calculation = xlCalculationAu tomatic
If Err.Description = "" Then
'MsgBox "" & lastcol
Else
MsgBox Err.Description
'MsgBox "" & lastcol
End If
End With

XL.Workbooks("T est_file.xls"). SaveAs FileName:="C:\E xportFile\Test_ file.xls"
XL.Quit
XL.Workbooks.Cl ose
Set XL = Nothing
Exit_RunMacro:
Exit Sub
Err_RunMacro:
MsgBox Err.Description
End Sub
Jul 10 '08 #1
7 13446
Stewart Ross
2,545 Recognized Expert Moderator Specialist
Hi. The problem is that you are using implicit references to worksheet ranges, which works fine in Excel itself (as the code is running within a specific workbook) but not when running VBA code using Excel as an automation server.

You will need to qualify all such implicit references to the active worksheet or activeworkbook explictly, using the Excel object or a sub-object (a range, for example) defined from it.

There is more than one line in your code which has such implicit references to the current active worksheet. To refer to these explicitly it is easiest to define a range object, set it to the relevant range, then use it as shown below. (I have removed the With statement which was not providing much in the way of simplification of your code, and adjusted the Excel object references in the first lines.)

Expand|Select|Wrap|Line Numbers
  1. Sub RunMacro()
  2. On Error GoTo Err_RunMacro
  3.     Dim XL As Excel.Application
  4.     Dim xlRange as Range
  5.     Set XL = New Excel.Application 
  6.     XL.Workbooks.Open "C:\Test_file.xls"
  7.  
  8.     Dim Col As Long, ColCnt As Long
  9.     Dim Rng As Range
  10.     Dim lastrow As Long, lastcol As Long
  11.  
  12.     XL.ScreenUpdating = False
  13.     XL.Calculation = xlCalculationManual
  14.  
  15. On Error GoTo Exits
  16.     set xlRange = .ActiveSheet.Range("A:A").SpecialCells(xlLastCell)
  17.     lastcol = xlRange.Column
  18.     XL.Activesheet.Range(Cells(1, 1), Cells(lastcol, 1)).SpecialCells(xlCellTypeBlanks).EntireColumn.Delete
  19.  
  20.     lastrow = xlRange.Row
  21.     XL.Activesheet.Range(Cells(1, 1), Cells(lastrow, 1)).SpecialCells(xlCellTypeBlanks).EntireRow.Delete
  22.  
  23. Exits:
  24.     XL.ScreenUpdating = True
  25.     XL.Calculation = xlCalculationAutomatic
  26.     If Err.Description = "" Then
  27.         'MsgBox "" & lastcol
  28.     Else
  29.         MsgBox Err.Description
  30.         'MsgBox "" & lastcol
  31.     End If
  32.  
  33.     XL.Workbooks("Test_file.xls").SaveAs 
  34.     FileName:="C:\ExportFile\Test_file.xls"
  35.     XL.Quit
  36.     XL.Workbooks.Close
  37.     Set XL = Nothing
  38. Exit_RunMacro:
  39.     Exit Sub
  40. Err_RunMacro:
  41.     MsgBox Err.Description
  42. End Sub
It is helpful if you use the code tags to enclose your code samples - it makes seeing the structure of your code much easier.

-Stewart
Jul 10 '08 #2
JFKJr
126 New Member
Hi. The problem is that you are using implicit references to worksheet ranges, which works fine in Excel itself (as the code is running within a specific workbook) but not when running VBA code using Excel as an automation server.

You will need to qualify all such implicit references to the active worksheet or activeworkbook explictly, using the Excel object or a sub-object (a range, for example) defined from it.

There is more than one line in your code which has such implicit references to the current active worksheet. To refer to these explicitly it is easiest to define a range object, set it to the relevant range, then use it as shown below. (I have removed the With statement which was not providing much in the way of simplification of your code, and adjusted the Excel object references in the first lines.)

Expand|Select|Wrap|Line Numbers
  1. Sub RunMacro()
  2. On Error GoTo Err_RunMacro
  3.     Dim XL As Excel.Application
  4.     Dim xlRange as Range
  5.     Set XL = New Excel.Application 
  6.     XL.Workbooks.Open "C:\Test_file.xls"
  7.  
  8.     Dim Col As Long, ColCnt As Long
  9.     Dim Rng As Range
  10.     Dim lastrow As Long, lastcol As Long
  11.  
  12.     XL.ScreenUpdating = False
  13.     XL.Calculation = xlCalculationManual
  14.  
  15. On Error GoTo Exits
  16.     set xlRange = .ActiveSheet.Range("A:A").SpecialCells(xlLastCell)
  17.     lastcol = xlRange.Column
  18.     XL.Activesheet.Range(Cells(1, 1), Cells(lastcol, 1)).SpecialCells(xlCellTypeBlanks).EntireColumn.Delete
  19.  
  20.     lastrow = xlRange.Row
  21.     XL.Activesheet.Range(Cells(1, 1), Cells(lastrow, 1)).SpecialCells(xlCellTypeBlanks).EntireRow.Delete
  22.  
  23. Exits:
  24.     XL.ScreenUpdating = True
  25.     XL.Calculation = xlCalculationAutomatic
  26.     If Err.Description = "" Then
  27.         'MsgBox "" & lastcol
  28.     Else
  29.         MsgBox Err.Description
  30.         'MsgBox "" & lastcol
  31.     End If
  32.  
  33.     XL.Workbooks("Test_file.xls").SaveAs 
  34.     FileName:="C:\ExportFile\Test_file.xls"
  35.     XL.Quit
  36.     XL.Workbooks.Close
  37.     Set XL = Nothing
  38. Exit_RunMacro:
  39.     Exit Sub
  40. Err_RunMacro:
  41.     MsgBox Err.Description
  42. End Sub
It is helpful if you use the code tags to enclose your code samples - it makes seeing the structure of your code much easier.

-Stewart
Hello Stewart, thanks a lot for the reply

I have used the above modified code as it is, but it is giving me "Compile error: Invalid or unqualified reference". When I step into the code, the error is occurring at ".ActiveShe et" on line number 16.

Please let me know what to do. Thanks.
Jul 10 '08 #3
Stewart Ross
2,545 Recognized Expert Moderator Specialist
Ahh, one of the WITH elements that I overlooked... change it to XL.Activesheet. ... (rest as before)

-Stewart
Jul 10 '08 #4
JFKJr
126 New Member
Ahh, one of the WITH elements that I overlooked... change it to XL.Activesheet. ... (rest as before)

-Stewart

Hello Stewart, the error is repeating again

When I run the following code, it worked fine for the first time and during the second time it is generating "Error application defined or object defined error ".

But, this time when I step into the code, it is entering into "Exits" case after 15th line below.
Please help me what to do. Thanks.

Expand|Select|Wrap|Line Numbers
  1. Sub RunMacro()
  2. On Error GoTo Err_RunMacro
  3.     Dim XL As Excel.Application
  4.     Dim xlRange As Range
  5.     Set XL = New Excel.Application
  6.     XL.Workbooks.Open "C:\Test_file3.xls"
  7.  
  8.     Dim lastrow As Long, lastcol As Long
  9.     XL.ScreenUpdating = False
  10.     XL.Calculation = xlCalculationManual
  11.  
  12. On Error GoTo Exits
  13.     Set xlRange = XL.ActiveSheet.Range("A:A").SpecialCells(xlLastCell)
  14.     lastcol = xlRange.Column
  15.     XL.ActiveSheet.Range(Cells(1, 1), Cells(lastcol, 1)).SpecialCells(xlCellTypeBlanks).EntireColumn.Delete
  16.  
  17.     lastrow = xlRange.Row
  18.     XL.ActiveSheet.Range(Cells(1, 1), Cells(lastrow, 1)).SpecialCells(xlCellTypeBlanks).EntireRow.Delete
  19.  
  20. Exits:
  21.     XL.ScreenUpdating = True
  22.     XL.Calculation = xlCalculationAutomatic
  23.     If Err.Description = "" Then
  24.         'MsgBox "" & lastcol
  25.     Else
  26.         MsgBox Err.Description
  27.         'MsgBox "" & lastcol
  28.     End If
  29.  
  30.     XL.Workbooks("Test_file3.xls").SaveAs FileName:="C:\ExportFile\Test_file3.xls"
  31.     XL.Quit
  32.     XL.Workbooks.Close
  33.     Set XL = Nothing
  34. Exit_RunMacro:
  35.     Exit Sub
  36. Err_RunMacro:
  37.     MsgBox Err.Description
  38. End Sub
By the way, I am using MS Access 2000 format.
Jul 10 '08 #5
Stewart Ross
2,545 Recognized Expert Moderator Specialist
HI. Its a domino chain of errors caused by implicit references. This time the Cells references are causing the failure, because there is no explicit range object for the cells object to refer to. (As a side note you have reversed the row and column references in the second cells part of line 15).

The easiest way to resolve this is to surround that part of the code with a WITH and use the '.' property to refer to the active sheet for the Cells object as part of the WITH:
Expand|Select|Wrap|Line Numbers
  1. With XL.ActiveSheet
  2.     .Range(.Cells(1, 1), .Cells(1, lastcol)).SpecialCells(xlCellTypeBlanks).EntireColumn.Delete
  3.  
  4. lastrow = xlRange.Row
  5. .Range(.Cells(1, 1), .Cells(lastrow, 1)).SpecialCells(xlCellTypeBlanks).EntireRow.Delete
  6. End With 
  7.  
Jul 10 '08 #6
JFKJr
126 New Member
HI. Its a domino chain of errors caused by implicit references. This time the Cells references are causing the failure, because there is no explicit range object for the cells object to refer to. (As a side note you have reversed the row and column references in the second cells part of line 15).

The easiest way to resolve this is to surround that part of the code with a WITH and use the '.' property to refer to the active sheet for the Cells object as part of the WITH:
Expand|Select|Wrap|Line Numbers
  1. With XL.ActiveSheet
  2.     .Range(.Cells(1, 1), .Cells(1, lastcol)).SpecialCells(xlCellTypeBlanks).EntireColumn.Delete
  3.  
  4. lastrow = xlRange.Row
  5. .Range(.Cells(1, 1), .Cells(lastrow, 1)).SpecialCells(xlCellTypeBlanks).EntireRow.Delete
  6. End With 
  7.  
Sorry to bother you once again!

But, when I run the following code, it is generating "Object Required" error at 17th line below and entering into "Exits" case.

Expand|Select|Wrap|Line Numbers
  1. Sub RunMacro()
  2. On Error GoTo Err_RunMacro
  3.     Dim XL As Excel.Application
  4.     Dim xlRange As Range
  5.     Set XL = New Excel.Application
  6.     XL.Workbooks.Open "C:\Test_file1.xls"
  7.  
  8.     Dim lastrow As Long, lastcol As Long
  9.     XL.ScreenUpdating = False
  10.     XL.Calculation = xlCalculationManual
  11.  
  12.     With XL.ActiveSheet
  13.     On Error GoTo Exits
  14.     Set xlRange = .Range("A:A").SpecialCells(xlLastCell)
  15.     lastcol = xlRange.Column
  16.     .Range(.Cells(1, 1), .Cells(1, lastcol)).SpecialCells(xlCellTypeBlanks).EntireColumn.Delete
  17.     lastrow = xlRange.Row
  18.     .Range(.Cells(1, 1), .Cells(lastrow, 1)).SpecialCells(xlCellTypeBlanks).EntireRow.Delete
  19.  
  20. Exits:
  21.     XL.ScreenUpdating = True
  22.     XL.Calculation = xlCalculationAutomatic
  23.     If Err.Description = "" Then
  24.         'MsgBox "" & lastcol
  25.     Else
  26.         MsgBox Err.Description
  27.         MsgBox "" & lastcol
  28.     End If
  29.     End With
  30.  
  31.     XL.Workbooks("Test_file1.xls").SaveAs FileName:="C:\ExportFile\Test_file1.xls"
  32.     XL.Quit
  33.     XL.Workbooks.Close
  34.     Set XL = Nothing
  35. Exit_RunMacro:
  36.     Exit Sub
  37. Err_RunMacro:
  38.     MsgBox Err.Description
  39. End Sub
Hello Stewart!

I got the code working, I just reversed the row and column references in the second cell part of line 15 as following (as we did before):

Expand|Select|Wrap|Line Numbers
  1. .Range(.Cells(1, 1), .Cells(lastcol, 1)).SpecialCells(xlCellTypeBlanks).EntireColumn.Delete
Because with the following line all columns in the excel file are getting deleted and there are no rows left.

Expand|Select|Wrap|Line Numbers
  1. .Range(.Cells(1, 1), .Cells(1, lastcol)).SpecialCells(xlCellTypeBlanks).EntireColumn.Delete
Thanks a loooooooot for your help. Thank You very much :). Your forum rocks!
Jul 10 '08 #7
Stewart Ross
2,545 Recognized Expert Moderator Specialist
Hi JFKjr. Thanks for your kind words. I am very glad you got your code to work out for you, and pleased to have been able to help.

Welcome to Bytes!

Regards

Stewart
Jul 10 '08 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

2
13111
by: RU | last post by:
Hi, I am working on a porting project to port C/C++ application from unixware C++, AT&T Standard components to g++ with STL on Linux. This application has been working properly on Unixware/C++/AT&T componets environment. I have been able to compile all modules after making necessary changes in LINUX/gcc/STL environment. We have two templates defined XList and XMap.
6
4738
by: beav At wn DOT com DOT au | last post by:
Students at the school where I work are getting the error "Application-defined or object-defined error." when trying to document their Access databases. If you open the documenter window, select a table and click OK, it works. If you click options and then click OK the above error message comes up. When logged in as administrator, the error does not happen. I assume that it is therefore a permissions problem. I have used google, and...
9
5293
by: Tyler | last post by:
I am attempting to extend a legacy VB6 application by making it use a .NET component written in C# exposed through COM interop. Everything appeared to be going well (VB application creates the .NET component instead of the legacy VB6 component and invokes some methods successfully) until I hit a snag. For one method that is being invoked by the VB6 application, the method takes 2 parameters (string values) and a custom interface. ...
0
23472
by: HKSHK | last post by:
This list compares the error codes used in VB.NET 2003 with those used in VB6. Error Codes: ============ 3: This Error number is obsolete and no longer used. (Formerly: Return without GoSub) 5: Procedure call or argument is not valid. 6: Overflow. 7: Out of memory.
3
1983
by: MLH | last post by:
If Err.Number = 2001 Then I have the above line in a procedure I downloaded. I don't know what error that is exactly.
4
2170
by: David Lozzi | last post by:
Howdy, I found a nice little book called ASP.NET 2.0 Cookbook by Michael A Kittel and Geoffrey LeBlond. Anyway, they have some instructions on how to setup application level error handling. Most of my functions have try..catch to email me about an error, then I want the application level to fire off to send the user to a custom page AND log it in the app log. When an error occurs on my page, i get a server error Server Error in...
8
12469
by: PW | last post by:
Hi, There is code in Alison Balter's excellant "Mastering Access 2003" to create a list of error codes and descriptions but it only generates error messages 3 through 94. Is there a website with a list of all of the error messages (with descriptions?). Thanks, -paul
2
4768
by: jr | last post by:
Hello everyone! I am new to this sort of postings so I'm not even sure if this is the correct place to start. Anyway, I would realy appreciate any help. I have a some VBA code that works quite happily in a number of machines and for different users. Recently we had to open a new account for a user in one of the machines and when the code runs it shows a run-time error '3075'. Nonetheless if I switch users in the same machine and run the...
8
11816
by: ConfusedMay | last post by:
Hi I have an error message in access 97 database and I was hoping that someone can help me with this (i've been searching the answer but seems like I couldn't find the right answer). When I clicked the open word document button on one of the form, I got "User defined type not defined" error message. Here is the code that connected to the button Private Sub openWordButton_Click() <------ highlighted in yellow Dim objWord As...
0
7929
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8419
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8280
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6739
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
5882
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5441
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3907
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
3955
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1526
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.