473,406 Members | 2,956 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Run-time error '13': Type mismatch

126 100+
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.

Expand|Select|Wrap|Line Numbers
  1. Sub RunMacro()
  2. Dim xlApp As Excel.Application
  3. Dim xlWB As Excel.Workbook
  4. Dim xlWS As Excel.Worksheet
  5. Dim LastUsedRow As Long
  6. Set xlApp = New Excel.Application
  7.  
  8. With xlApp
  9. .Visible = True
  10. .Interactive = True
  11. Set xlWB = .Workbooks.Open("C:\ExtractPolicyList.xls", , False)
  12. Set xlWS = xlWB.Worksheets("SelectPolicies")
  13.  
  14. If .WorksheetFunction.CountA(xlWS.Cells) > 0 Then
  15.   LastUsedRow = xlWS.Cells.Find(What:="*", After:=[A1], _
  16.                 SearchOrder:=xlByRows, _
  17.                 SearchDirection:=xlPrevious).Row
  18.   MsgBox "" & LastUsedRow
  19. End If
  20. End With
  21. End Sub
Jul 29 '08 #1
4 5121
Stewart Ross
2,545 Expert Mod 2GB
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).

Expand|Select|Wrap|Line Numbers
  1. Function LastRow(Optional worksheetname As String = "") As Long
  2.     ' Returns the last row number in the active sheet,
  3.     ' or the specified worksheet if a worksheet name is provided
  4.     Dim rngLast As Range
  5.     If worksheetname = "" Then
  6.         Set rngLast = objExcel.ActiveSheet.Range("A1").SpecialCells(xlLastCell)
  7.     Else
  8.         Set rngLast = objExcel.Worksheets(worksheetname).Range("A1").SpecialCells(xlLastCell)
  9.     End If
  10.     LastRow = rngLast.Row
  11. End Function
  12.  
  13. Function LastColumn(Optional worksheetname As String = "") As Long
  14.     ' Returns the last row number in the active sheet,
  15.     ' or the specified worksheet if a worksheet name is provided
  16.     Dim rngLast As Range
  17.     If worksheetname = "" Then
  18.         Set rngLast = objExcel.ActiveSheet.Range("A1").SpecialCells(xlLastCell)
  19.     Else
  20.         Set rngLast = objExcel.Worksheets(worksheetname).Range("A1").SpecialCells(xlLastCell)
  21.     End If
  22.     LastColumn = rngLast.Column
  23. End Function
Feel free to extract/adapt the code as necessary.

-Stewart
Jul 29 '08 #2
ADezii
8,834 Expert 8TB
I think your syntax is incorrect, I am referring to Line #15.
Expand|Select|Wrap|Line Numbers
  1. Dim xlApp As Excel.Application
  2. Dim xlWB As Excel.Workbook
  3. Dim xlWS As Excel.Worksheet
  4. Dim LastUsedRow As Long
  5. Dim c
  6. Set xlApp = New Excel.Application
  7.  
  8. With xlApp
  9.   .Visible = True
  10.   .Interactive = True
  11.   Set xlWB = .Workbooks.Open("C:\ExtractPolicyList.xls", , False)
  12.   Set xlWS = xlWB.Worksheets("SelectPolicies")
  13.  
  14.   If .WorksheetFunction.CountA(xlWS.Cells) > 0 Then
  15.     LastUsedRow = xlWS.Cells.Find(What:="*", After:=xlWS.Range("A1"), _
  16.                   SearchOrder:=xlByRows, _
  17.                   SearchDirection:=xlPrevious).Row
  18.     MsgBox "" & LastUsedRow
  19.   End If
  20. End With
Jul 30 '08 #3
JFKJr
126 100+
I think your syntax is incorrect, I am referring to Line #15.
Expand|Select|Wrap|Line Numbers
  1. Dim xlApp As Excel.Application
  2. Dim xlWB As Excel.Workbook
  3. Dim xlWS As Excel.Worksheet
  4. Dim LastUsedRow As Long
  5. Dim c
  6. Set xlApp = New Excel.Application
  7.  
  8. With xlApp
  9.   .Visible = True
  10.   .Interactive = True
  11.   Set xlWB = .Workbooks.Open("C:\ExtractPolicyList.xls", , False)
  12.   Set xlWS = xlWB.Worksheets("SelectPolicies")
  13.  
  14.   If .WorksheetFunction.CountA(xlWS.Cells) > 0 Then
  15.     LastUsedRow = xlWS.Cells.Find(What:="*", After:=xlWS.Range("A1"), _
  16.                   SearchOrder:=xlByRows, _
  17.                   SearchDirection:=xlPrevious).Row
  18.     MsgBox "" & LastUsedRow
  19.   End If
  20. End With
Hello Stewart and ADezii, I solved the issue by changing the 15 line above to the following and it worked.
Expand|Select|Wrap|Line Numbers
  1. LastUsedRow = .Cells.Find(what:="*", after:=.Cells(1, 1), lookat:=xlPart, searchorder:=xlByRows, SearchDirection:=xlPrevious).Row
Thanks anyway though!
Jul 30 '08 #4
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.
Aug 5 '08 #5

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

Similar topics

4
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...
2
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:...
15
by: mg | last post by:
How can I run an .exe using C# from within the code behind of a WebForm app?
6
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...
13
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...
9
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. ...
7
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: ...
8
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? --...
3
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!
7
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
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
0
BarryA
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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,...
0
jinu1996
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...
0
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...
0
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,...
0
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...

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.