Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 184
Default VBA Import Convert Data Type from Type 1 to Type 2

I would like to fix the data type in my initial Import step
with VBA in order to have all data type for that field as type 2.
But I read in another post from Tom Ogilvy : "In general, you can't
force a change in type in VBA the way you can in Excel..."
I could accept that if the conversion was applied within the Cell
within the same Sheet,
but I am reading data in workbook A, converting and writing it in
workbook B. What is the work around solution ?
Thank you for your help,
J.P.



  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 9,101
Default VBA Import Convert Data Type from Type 1 to Type 2

Type 2 is a number format, Correct?

1) In VBA you can delare the variable as a number format (Integer, Singel,
Double.
2) In VBA use Val(MyNumber) to convert to a number
3) Use copy statement to copy from oneworkbook to another

Workbooks("A").Sheets("Sheet1").Range(B4").copy _
destination:=Workbooks("B").Sheets("Sheet2").Range ("D5")
4) format workbook B top the format you are looking for


Workbooks("A").Sheets("Sheet1").Range(B4").copy _
destination:=Workbooks("B").Sheets("Sheet2").Range ("D5")
Workbooks("B").Sheets("Sheet2").Range("D5").number format = "#0.00"



"u473" wrote:

I would like to fix the data type in my initial Import step
with VBA in order to have all data type for that field as type 2.
But I read in another post from Tom Ogilvy : "In general, you can't
force a change in type in VBA the way you can in Excel..."
I could accept that if the conversion was applied within the Cell
within the same Sheet,
but I am reading data in workbook A, converting and writing it in
workbook B. What is the work around solution ?
Thank you for your help,
J.P.




  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 184
Default VBA Import Convert Data Type from Type 1 to Type 2

Thank you for your answer, but Data Type 1 is Numeric, Type 2 is
String.
The field I need to import has a mix of Type 1 and Type 2.
I need to have them all converted to Type 2.
My current syntax attempts with cString(MyNumber) or NumberFormat=
"@"
did not produce satisfactory results.
I am still searching.
Thank you for your help,
J.P.

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 9,101
Default VBA Import Convert Data Type from Type 1 to Type 2

Th eproblem with cstring is it adds an extra space in front of the number as
a place holder for a positive or neagive sign. Excel will automatically
convert a number string to a real number if the cells is formated as General
or Number format. You need to format the cell to text (numberformat = "@")
before you put the number into the cell to stop the number from changing.
You should also use variables in VBS declared as strings if you are not
directly copying the number from one workbook to the other.

I've had to in the past specifically move numbers in excel to a string
variable in VBA and then write to excel to prevent any changes of the numbers.

"u473" wrote:

Thank you for your answer, but Data Type 1 is Numeric, Type 2 is
String.
The field I need to import has a mix of Type 1 and Type 2.
I need to have them all converted to Type 2.
My current syntax attempts with cString(MyNumber) or NumberFormat=
"@"
did not produce satisfactory results.
I am still searching.
Thank you for your help,
J.P.


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
Convert numeric field from type 1 to type 2 u473 Excel Programming 2 October 20th 08 02:06 PM
how to convert date type to text type Steffen Excel Discussion (Misc queries) 3 July 17th 07 11:32 AM
Type Mismatch: array or user defined type expected ExcelMonkey Excel Programming 4 July 6th 06 03:40 PM
Help: Compile error: type mismatch: array or user defined type expected lvcha.gouqizi Excel Programming 1 October 31st 05 08:20 PM
Adding new 'Type' to Format->Number->Time->Type Chip Pearson Excel Discussion (Misc queries) 5 September 26th 05 08:45 PM


All times are GMT +1. The time now is 06:49 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"