Hello everyone, the following code is working fine for the first time when I run it and giving " Run-time error '13': Type mismatch" at line #15 during the second time and so on....
The code is working sometimes and sometimes not!
Please kindly let me know what to do?
Thanks in advance. - Sub RunMacro()
-
Dim xlApp As Excel.Application
-
Dim xlWB As Excel.Workbook
-
Dim xlWS As Excel.Worksheet
-
Dim LastUsedRow As Long
-
Set xlApp = New Excel.Application
-
-
With xlApp
-
.Visible = True
-
.Interactive = True
-
Set xlWB = .Workbooks.Open("C:\ExtractPolicyList.xls", , False)
-
Set xlWS = xlWB.Worksheets("SelectPolicies")
-
-
If .WorksheetFunction.CountA(xlWS.Cells) > 0 Then
-
LastUsedRow = xlWS.Cells.Find(What:="*", After:=[A1], _
-
SearchOrder:=xlByRows, _
-
SearchDirection:=xlPrevious).Row
-
MsgBox "" & LastUsedRow
-
End If
-
End With
-
End Sub
4 5121
Hi JFKjr. Not sure why this fails on second and subsequent passes. I suspect that the use of [A1] as a cell reference is part of the issue. However, there is no need to do a search at all to find the last row (and the last column).
The following two functions - part of an automated Excel query transfer module I developed - return the last row and column of object objExcel (which is global in scope within the class module concerned). - Function LastRow(Optional worksheetname As String = "") As Long
-
' Returns the last row number in the active sheet,
-
' or the specified worksheet if a worksheet name is provided
-
Dim rngLast As Range
-
If worksheetname = "" Then
-
Set rngLast = objExcel.ActiveSheet.Range("A1").SpecialCells(xlLastCell)
-
Else
-
Set rngLast = objExcel.Worksheets(worksheetname).Range("A1").SpecialCells(xlLastCell)
-
End If
-
LastRow = rngLast.Row
-
End Function
-
-
Function LastColumn(Optional worksheetname As String = "") As Long
-
' Returns the last row number in the active sheet,
-
' or the specified worksheet if a worksheet name is provided
-
Dim rngLast As Range
-
If worksheetname = "" Then
-
Set rngLast = objExcel.ActiveSheet.Range("A1").SpecialCells(xlLastCell)
-
Else
-
Set rngLast = objExcel.Worksheets(worksheetname).Range("A1").SpecialCells(xlLastCell)
-
End If
-
LastColumn = rngLast.Column
-
End Function
Feel free to extract/adapt the code as necessary.
-Stewart
I think your syntax is incorrect, I am referring to Line # 15. - Dim xlApp As Excel.Application
-
Dim xlWB As Excel.Workbook
-
Dim xlWS As Excel.Worksheet
-
Dim LastUsedRow As Long
-
Dim c
-
Set xlApp = New Excel.Application
-
-
With xlApp
-
.Visible = True
-
.Interactive = True
-
Set xlWB = .Workbooks.Open("C:\ExtractPolicyList.xls", , False)
-
Set xlWS = xlWB.Worksheets("SelectPolicies")
-
-
If .WorksheetFunction.CountA(xlWS.Cells) > 0 Then
-
LastUsedRow = xlWS.Cells.Find(What:="*", After:=xlWS.Range("A1"), _
-
SearchOrder:=xlByRows, _
-
SearchDirection:=xlPrevious).Row
-
MsgBox "" & LastUsedRow
-
End If
-
End With
I think your syntax is incorrect, I am referring to Line #15. - Dim xlApp As Excel.Application
-
Dim xlWB As Excel.Workbook
-
Dim xlWS As Excel.Worksheet
-
Dim LastUsedRow As Long
-
Dim c
-
Set xlApp = New Excel.Application
-
-
With xlApp
-
.Visible = True
-
.Interactive = True
-
Set xlWB = .Workbooks.Open("C:\ExtractPolicyList.xls", , False)
-
Set xlWS = xlWB.Worksheets("SelectPolicies")
-
-
If .WorksheetFunction.CountA(xlWS.Cells) > 0 Then
-
LastUsedRow = xlWS.Cells.Find(What:="*", After:=xlWS.Range("A1"), _
-
SearchOrder:=xlByRows, _
-
SearchDirection:=xlPrevious).Row
-
MsgBox "" & LastUsedRow
-
End If
-
End With
Hello Stewart and ADezii, I solved the issue by changing the 15 line above to the following and it worked. - LastUsedRow = .Cells.Find(what:="*", after:=.Cells(1, 1), lookat:=xlPart, searchorder:=xlByRows, SearchDirection:=xlPrevious).Row
Thanks anyway though!
NeoPa 32,556
Expert Mod 16PB
Indeed. This is essentially what ADezii suggested in the previous post (although alternative wording was used).
When dealing with Application Automation in Office, I always recommend that the code for the foreign application be developed natively first (in this case within Excel) and only when it is working correctly port it across to the other application (in this case Access).
This can reduce the number of headaches considerably. Don't forget that some of the references to internal objects need to be modified for when they are referenced as a foreign application though.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Ed |
last post by:
Hello,
I took a course in asp about 2 years ago and I was
practicing with IIS 5.0. Then I put it down for a while.
Now trying to get back to it. I can't run asp files from
subdirectories of...
|
by: Jenna Olson |
last post by:
Hi all-
I've never seen this particular issue addressed, but was wondering if
there's anything to support one way or another. Say I have a class:
class ManipulateData
{
public:...
|
by: mg |
last post by:
How can I run an .exe using C# from within the code behind of a WebForm app?
|
by: billr |
last post by:
I have developed a small API for taking care of a lot of boiler plate stuff
in a multi formed windows application, for example setting up a messaging
thread framework.
New Forms, in the...
|
by: Bob Day |
last post by:
Using vs2003, vb.net
I start a thread, giving it a name before start. Code snippet:
'give each thread a unique name (for later identification)
Trunk_Thread.Name = "Trunk_0_Thread"
' allow...
|
by: Brett Wesoloski |
last post by:
I am new to VS2005.
I changed my program.cs file to be a different form I am working on. But
when I go to run the application it still brings up the form that was
originally declared as new. ...
|
by: Lee Crabtree |
last post by:
I remember when I was first getting into .NET Forms programming that
there was a rather emphatic rule about not constructing a form before
calling Application.Run with it. So this:
...
|
by: David Thielen |
last post by:
Hi;
In our setup program how do I determine if I need to run "aspnet_regiis –i"
and if so, is there an API I can calll rather than finding that program on
the user's disk and calling it?
--...
|
by: traceable1 |
last post by:
Is there a way I can set up a SQL script to run when the instance
starts up?
SQL Server 2005 SP2
thanks!
|
by: mxdevit |
last post by:
Task: run application from ASP.NET
for example,
you have a button on ASP.NET page, when press this button - one
application is invoked.
the code to run application (for example, notepad) is...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
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,...
|
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...
| |