Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 110
Default a string to an integer

I have a string:

string(3) = "543"

I try to change it to an integer:

int = cint(string(3))

It gives me a 'Type Mismatch' Error.

Maybe there may be spaces before of after the numbers but does that matter?

How do I change from a string to an integer?
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 523
Default a string to an integer


Spaces do matter.

Try

int = cint(trim(string(3))

"Philosophaie" wrote:

I have a string:

string(3) = "543"

I try to change it to an integer:

int = cint(string(3))

It gives me a 'Type Mismatch' Error.

Maybe there may be spaces before of after the numbers but does that matter?

How do I change from a string to an integer?

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 110
Default a string to an integer

There were no spaces. I checked. Why else am I getting 'Type Mismatch' Error
and not letting me use cint to change from a string?
  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 523
Default a string to an integer

I've just done this:

Sub test()

Dim s As String
s = "543"

Dim i As Integer
i = CInt(Trim(s))

MsgBox i

End Sub


and it works. Have you ot an apostrophe/single quote ' preceeding the 5?


"Philosophaie" wrote:

There were no spaces. I checked. Why else am I getting 'Type Mismatch' Error
and not letting me use cint to change from a string?

  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default a string to an integer

Don't use String and Int as variable names.

String is a VBA function and data type.
Int is a VBA function.


Dim myString(1 To 3) As String
Dim myInt As Long
myString(3) = "543"
myInt = CInt(myString(3))

Personally, I wouldn't use "as integer" or cInt(). I'd use "As long" and
clng().

Philosophaie wrote:

I have a string:

string(3) = "543"

I try to change it to an integer:

int = cint(string(3))

It gives me a 'Type Mismatch' Error.

Maybe there may be spaces before of after the numbers but does that matter?

How do I change from a string to an integer?


--

Dave Peterson


  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 110
Default a string to an integer

I am using what is already in the cell from "Left and Right" operations to a
bunch of similar data strings. I have ran my own trimming operation thru

if left(a,1)="-" then a=Right(a,2)

but it will still not let me take the cint or cdbl of any of the data.

So the quote sign is irrelevant.
  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 523
Default a string to an integer

Ok... another approach - try

msgbox len(str) * " """ & str & """"

which should display

3 "543"

to make sure there are no extra characters in there.

Sam


"Philosophaie" wrote:

I am using what is already in the cell from "Left and Right" operations to a
bunch of similar data strings. I have ran my own trimming operation thru

if left(a,1)="-" then a=Right(a,2)

but it will still not let me take the cint or cdbl of any of the data.

So the quote sign is irrelevant.

  #8   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 110
Default a string to an integer

I typed verbatum:

msgbox len(str) * " """ & str & """"

even that gave me a 'Type Mismatch error'


"Sam Wilson" wrote:

Ok... another approach - try

msgbox len(str) * " """ & str & """"

which should display

3 "543"

to make sure there are no extra characters in there.

Sam


"Philosophaie" wrote:

I am using what is already in the cell from "Left and Right" operations to a
bunch of similar data strings. I have ran my own trimming operation thru

if left(a,1)="-" then a=Right(a,2)

but it will still not let me take the cint or cdbl of any of the data.

So the quote sign is irrelevant.

  #9   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 523
Default a string to an integer

msgbox len(str) & " """ & str & """"

Sorry, my mistake. Change str for whatever string it is you're trying to
convert.

Sam



"Philosophaie" wrote:

I typed verbatum:

msgbox len(str) * " """ & str & """"

even that gave me a 'Type Mismatch error'


"Sam Wilson" wrote:

Ok... another approach - try

msgbox len(str) * " """ & str & """"

which should display

3 "543"

to make sure there are no extra characters in there.

Sam


"Philosophaie" wrote:

I am using what is already in the cell from "Left and Right" operations to a
bunch of similar data strings. I have ran my own trimming operation thru

if left(a,1)="-" then a=Right(a,2)

but it will still not let me take the cint or cdbl of any of the data.

So the quote sign is irrelevant.

  #10   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 110
Default a string to an integer

they all had len=2 and nothing looked out of the ordinary.

"Sam Wilson" wrote:

msgbox len(str) & " """ & str & """"

Sorry, my mistake. Change str for whatever string it is you're trying to
convert.




  #11   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 703
Default a string to an integer

Also String is an intrinsic function in VB!

Use Option Explicit at the top of your module and make sure you dim all of
your variables using names that are not intrinsic functions.



"Philosophaie" wrote:

I have a string:

string(3) = "543"

I try to change it to an integer:

int = cint(string(3))

It gives me a 'Type Mismatch' Error.

Maybe there may be spaces before of after the numbers but does that matter?

How do I change from a string to an integer?

  #12   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 703
Default a string to an integer

Type mismatch probably comes from the fact you are using "int"

int = CInt()

Int is an intrinsic function in VB. You shouldn't name a variable "int"

As for the other problem use Mid not Right to get the remaining portion of
the string

if left(a,1)="-" then a=mid(a,2)

"Philosophaie" wrote:

I have a string:

string(3) = "543"

I try to change it to an integer:

int = cint(string(3))

It gives me a 'Type Mismatch' Error.

Maybe there may be spaces before of after the numbers but does that matter?

How do I change from a string to an integer?

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
String to Rounded Integer StumpedAgain Excel Programming 3 June 11th 08 08:08 PM
convert string to integer eugene Excel Programming 7 February 20th 07 07:00 PM
Varaibles as both integer and String RominallL Excel Programming 5 January 31st 07 06:33 PM
String or integer? JustinP Excel Programming 2 September 5th 06 06:56 AM
Compare string with integer hurriance[_8_] Excel Programming 2 June 26th 06 09:14 AM


All times are GMT +1. The time now is 09:31 PM.

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

About Us

"It's about Microsoft Excel"