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
7 13446
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.) - Sub RunMacro()
-
On Error GoTo Err_RunMacro
-
Dim XL As Excel.Application
-
Dim xlRange as Range
-
Set XL = New Excel.Application
-
XL.Workbooks.Open "C:\Test_file.xls"
-
-
Dim Col As Long, ColCnt As Long
-
Dim Rng As Range
-
Dim lastrow As Long, lastcol As Long
-
-
XL.ScreenUpdating = False
-
XL.Calculation = xlCalculationManual
-
-
On Error GoTo Exits
-
set xlRange = .ActiveSheet.Range("A:A").SpecialCells(xlLastCell)
-
lastcol = xlRange.Column
-
XL.Activesheet.Range(Cells(1, 1), Cells(lastcol, 1)).SpecialCells(xlCellTypeBlanks).EntireColumn.Delete
-
-
lastrow = xlRange.Row
-
XL.Activesheet.Range(Cells(1, 1), Cells(lastrow, 1)).SpecialCells(xlCellTypeBlanks).EntireRow.Delete
-
-
Exits:
-
XL.ScreenUpdating = True
-
XL.Calculation = xlCalculationAutomatic
-
If Err.Description = "" Then
-
'MsgBox "" & lastcol
-
Else
-
MsgBox Err.Description
-
'MsgBox "" & lastcol
-
End If
-
-
XL.Workbooks("Test_file.xls").SaveAs
-
FileName:="C:\ExportFile\Test_file.xls"
-
XL.Quit
-
XL.Workbooks.Close
-
Set XL = Nothing
-
Exit_RunMacro:
-
Exit Sub
-
Err_RunMacro:
-
MsgBox Err.Description
-
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
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.) - Sub RunMacro()
-
On Error GoTo Err_RunMacro
-
Dim XL As Excel.Application
-
Dim xlRange as Range
-
Set XL = New Excel.Application
-
XL.Workbooks.Open "C:\Test_file.xls"
-
-
Dim Col As Long, ColCnt As Long
-
Dim Rng As Range
-
Dim lastrow As Long, lastcol As Long
-
-
XL.ScreenUpdating = False
-
XL.Calculation = xlCalculationManual
-
-
On Error GoTo Exits
-
set xlRange = .ActiveSheet.Range("A:A").SpecialCells(xlLastCell)
-
lastcol = xlRange.Column
-
XL.Activesheet.Range(Cells(1, 1), Cells(lastcol, 1)).SpecialCells(xlCellTypeBlanks).EntireColumn.Delete
-
-
lastrow = xlRange.Row
-
XL.Activesheet.Range(Cells(1, 1), Cells(lastrow, 1)).SpecialCells(xlCellTypeBlanks).EntireRow.Delete
-
-
Exits:
-
XL.ScreenUpdating = True
-
XL.Calculation = xlCalculationAutomatic
-
If Err.Description = "" Then
-
'MsgBox "" & lastcol
-
Else
-
MsgBox Err.Description
-
'MsgBox "" & lastcol
-
End If
-
-
XL.Workbooks("Test_file.xls").SaveAs
-
FileName:="C:\ExportFile\Test_file.xls"
-
XL.Quit
-
XL.Workbooks.Close
-
Set XL = Nothing
-
Exit_RunMacro:
-
Exit Sub
-
Err_RunMacro:
-
MsgBox Err.Description
-
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.
Ahh, one of the WITH elements that I overlooked... change it to XL.Activesheet. ... (rest as before)
-Stewart
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. - Sub RunMacro()
-
On Error GoTo Err_RunMacro
-
Dim XL As Excel.Application
-
Dim xlRange As Range
-
Set XL = New Excel.Application
-
XL.Workbooks.Open "C:\Test_file3.xls"
-
-
Dim lastrow As Long, lastcol As Long
-
XL.ScreenUpdating = False
-
XL.Calculation = xlCalculationManual
-
-
On Error GoTo Exits
-
Set xlRange = XL.ActiveSheet.Range("A:A").SpecialCells(xlLastCell)
-
lastcol = xlRange.Column
-
XL.ActiveSheet.Range(Cells(1, 1), Cells(lastcol, 1)).SpecialCells(xlCellTypeBlanks).EntireColumn.Delete
-
-
lastrow = xlRange.Row
-
XL.ActiveSheet.Range(Cells(1, 1), Cells(lastrow, 1)).SpecialCells(xlCellTypeBlanks).EntireRow.Delete
-
-
Exits:
-
XL.ScreenUpdating = True
-
XL.Calculation = xlCalculationAutomatic
-
If Err.Description = "" Then
-
'MsgBox "" & lastcol
-
Else
-
MsgBox Err.Description
-
'MsgBox "" & lastcol
-
End If
-
-
XL.Workbooks("Test_file3.xls").SaveAs FileName:="C:\ExportFile\Test_file3.xls"
-
XL.Quit
-
XL.Workbooks.Close
-
Set XL = Nothing
-
Exit_RunMacro:
-
Exit Sub
-
Err_RunMacro:
-
MsgBox Err.Description
-
End Sub
By the way, I am using MS Access 2000 format.
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: - With XL.ActiveSheet
-
.Range(.Cells(1, 1), .Cells(1, lastcol)).SpecialCells(xlCellTypeBlanks).EntireColumn.Delete
-
-
lastrow = xlRange.Row
-
.Range(.Cells(1, 1), .Cells(lastrow, 1)).SpecialCells(xlCellTypeBlanks).EntireRow.Delete
-
End With
-
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: - With XL.ActiveSheet
-
.Range(.Cells(1, 1), .Cells(1, lastcol)).SpecialCells(xlCellTypeBlanks).EntireColumn.Delete
-
-
lastrow = xlRange.Row
-
.Range(.Cells(1, 1), .Cells(lastrow, 1)).SpecialCells(xlCellTypeBlanks).EntireRow.Delete
-
End With
-
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. - Sub RunMacro()
-
On Error GoTo Err_RunMacro
-
Dim XL As Excel.Application
-
Dim xlRange As Range
-
Set XL = New Excel.Application
-
XL.Workbooks.Open "C:\Test_file1.xls"
-
-
Dim lastrow As Long, lastcol As Long
-
XL.ScreenUpdating = False
-
XL.Calculation = xlCalculationManual
-
-
With XL.ActiveSheet
-
On Error GoTo Exits
-
Set xlRange = .Range("A:A").SpecialCells(xlLastCell)
-
lastcol = xlRange.Column
-
.Range(.Cells(1, 1), .Cells(1, lastcol)).SpecialCells(xlCellTypeBlanks).EntireColumn.Delete
-
lastrow = xlRange.Row
-
.Range(.Cells(1, 1), .Cells(lastrow, 1)).SpecialCells(xlCellTypeBlanks).EntireRow.Delete
-
-
Exits:
-
XL.ScreenUpdating = True
-
XL.Calculation = xlCalculationAutomatic
-
If Err.Description = "" Then
-
'MsgBox "" & lastcol
-
Else
-
MsgBox Err.Description
-
MsgBox "" & lastcol
-
End If
-
End With
-
-
XL.Workbooks("Test_file1.xls").SaveAs FileName:="C:\ExportFile\Test_file1.xls"
-
XL.Quit
-
XL.Workbooks.Close
-
Set XL = Nothing
-
Exit_RunMacro:
-
Exit Sub
-
Err_RunMacro:
-
MsgBox Err.Description
-
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): - .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. - .Range(.Cells(1, 1), .Cells(1, lastcol)).SpecialCells(xlCellTypeBlanks).EntireColumn.Delete
Thanks a loooooooot for your help. Thank You very much :). Your forum rocks!
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
Sign in to post your reply or Sign up for a free account.
Similar topics |
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.
|
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...
|
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. ...
|
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.
|
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.
| |
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...
|
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
|
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...
|
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...
|
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,...
|
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...
| |
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...
|
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...
|
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...
|
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();...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |