Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5
Default Type mismatch !

If I've got the following code in vba

var1 as string
var1 = Cells(1, 1).Value


If the cell in question contains the value "#N/A", then I get a type
mismatch error on the line var1 = Cells(1, 1).Value.

However, no error if the value in the cell in "N/A"

What can I do to get around this, I need var1 to contain the value in
the cell, no matter what value it is.

Also, I wonder why VBA doesn't tell you which line it was executing
when it comes across a type mismatch, I had to find out by stepping
through the code !

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,302
Default Type mismatch !

Hi Roshintosh,

Try:

Dim var1 As Variant


---
Regards,
Norman



"Roshintosh" wrote in message
ups.com...
If I've got the following code in vba

var1 as string
var1 = Cells(1, 1).Value


If the cell in question contains the value "#N/A", then I get a type
mismatch error on the line var1 = Cells(1, 1).Value.

However, no error if the value in the cell in "N/A"

What can I do to get around this, I need var1 to contain the value in
the cell, no matter what value it is.

Also, I wonder why VBA doesn't tell you which line it was executing
when it comes across a type mismatch, I had to find out by stepping
through the code !



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5
Default Type mismatch !

Cheers Norman,

Declaring the variable as variant makes no difference unfortunately.

If the value in the cell is #N/A, then the statement

dim var1 as variant
var1 = Cells(1, 1).Value

gives a type mismatch. (run time error 13)

I'm totally confused !

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 789
Default Type mismatch !


Hi

Try
var1 as string
var1 = Cells(1, 1).Text

I'd have thought the Variant declaration would do it, but it seems not.
Anyone out there know why not? I couldn't find the type of an error in
the Errors collection

You could also do
var1 as Variant
var1 = Cstr(Cells(1,1).Value)

but this returns
Error 2042

regards
Paul

  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,302
Default Type mismatch !

Hi Roshintosh,

Declaring the variable as variant makes no difference unfortunately.


If the variable is declared as a string, the macro will break with a
run-time 13 type mismatch code.

I tried:

Sub ABC()
Dim var1 As Variant
var1 = Cells(1, 1).Value
Debug.Print "var1", var1
Debug.Print "#N/A error number", CVErr(xlErrNA)
End
End Sub

This produced the following in the imediate window:

var1 Error 2042
#N/A error number Error 2042

Thus, the variant variable correctly holds the cell's error value.

---
Regards,
Norman



"Roshintosh" wrote in message
oups.com...
Cheers Norman,

Declaring the variable as variant makes no difference unfortunately.

If the value in the cell is #N/A, then the statement

dim var1 as variant
var1 = Cells(1, 1).Value

gives a type mismatch. (run time error 13)

I'm totally confused !





  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Type mismatch !


Hello Roshintosh & Paul,

Worksheet cells are all variants. When an error is found, Excel set
the cell to an Error type variant. This can be verified 2 ways. Firs
by using IsError() and second by using VarType(). IsError will retur
True if the cell is an Error variant type and VarType returns 10 if th
cell is an Error variant type.

To answer your question about getting a cell value, if there is a
error Excel will only return the Error, not the value. So, your bes
bet is test for an error and handle it.

The help files tell you how to raise an error in cell, but not how t
convert an Error type back to something useful like an Error Number
Here's how to do that...

Sub ErrorTest()

Dim Ret
Dim ErrNum

Ret = Range("A1").Value
If IsError(Ret) Then
E = CInt(Ret)
End If


End Sub

Hope this helps you some.

Sincerely,
Leith Ros

--
Leith Ros
-----------------------------------------------------------------------
Leith Ross's Profile: http://www.excelforum.com/member.php...fo&userid=1846
View this thread: http://www.excelforum.com/showthread.php?threadid=48380

  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default Type mismatch !

dim Var1 as Variant
if iserror(cells(1,1).value) then
var1 = cells(1,1).text
else
var1 = cells(1,1).value
end if




Roshintosh wrote:

If I've got the following code in vba

var1 as string
var1 = Cells(1, 1).Value

If the cell in question contains the value "#N/A", then I get a type
mismatch error on the line var1 = Cells(1, 1).Value.

However, no error if the value in the cell in "N/A"

What can I do to get around this, I need var1 to contain the value in
the cell, no matter what value it is.

Also, I wonder why VBA doesn't tell you which line it was executing
when it comes across a type mismatch, I had to find out by stepping
through the code !


--

Dave Peterson
  #8   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5
Default Type mismatch !

Thanks Dave

I just didn't notice the .text property !

I'm new to VBA

I spent a good while today putting in unnecessary error handling for
using the .value property
Now I can take it out !

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Help: Compile error: type mismatch: array or user defined type expected lvcha.gouqizi Excel Programming 1 October 31st 05 08:20 PM
type mismatch? Gixxer_J_97[_2_] Excel Programming 4 April 1st 05 03:20 AM
type mismatch here! J_J[_2_] Excel Programming 2 March 14th 05 03:14 PM
Type Mismatch Jack Schitt Excel Programming 2 September 3rd 04 11:55 AM
Type Mismatch Phil Hageman[_3_] Excel Programming 2 January 9th 04 06:11 PM


All times are GMT +1. The time now is 01:04 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"