Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2
Default How do I change the datatype of a cell?

I need to change the datatype of all cells in a column to text. I have the
following vbscript:

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As
Boolean)
Call UpdateDatatypes
End Sub

Private Sub Workbook_Open()
Call UpdateDatatypes
End Sub

Private Sub UpdateDatatypes()
Dim myCell As Excel.Range
Dim myRng As Excel.Range
Dim wks As Excel.Worksheet

For Each wks In Me.Worksheets
Set myRng = wks.UsedRange.Cells

For Each myCell In myRng.Cells
If (Not Intersect(myCell, wks.Range("h:n")) Is Nothing) Then
myCell.Value = CStr(Replace(myCell.Value, ",", "."))
End If
If (Not Intersect(myCell, wks.Range("a:b")) Is Nothing) Then
myCell.Value = CStr(Replace(myCell.Value, ",", "."))
End If
Next
Next
End Sub

Now, any cells that have only digits in them will be converted to a number
type by Excel.
If I use:

myCell.Value = "A" & CStr(Replace(myCell.Value, ",", "."))

then cells will have a text type after the script is run. How do I keep
excel from changing the
type of the cells with only digits in them to number?

I need this because the MS JET OleDb provider for excel is stupid and will
only allow
reading of a cell if it has the same type as the the cell in same column in
the previous row
(if not it returns an empty string).

Casper


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default How do I change the datatype of a cell?

Private Sub UpdateDatatypes()
Dim myCell As Excel.Range
Dim myRng As Excel.Range
Dim wks As Excel.Worksheet

For Each wks In ThisWorkbook.Worksheets
Set myRng = Intersect(wks.UsedRange, wks.Range("A:B,H:N"))

For Each myCell In myRng
myCell.NumberFormat = "@"
myCell.Value = "'" & CStr(Replace(myCell.Text, ",", "."))
Next
Next
End Sub

Assuming you still want to replace any comma with a period/fullstop

--
Regards,
Tom Ogilvy


"Casper Hornstrup" wrote in message
...
I need to change the datatype of all cells in a column to text. I have the
following vbscript:

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As
Boolean)
Call UpdateDatatypes
End Sub

Private Sub Workbook_Open()
Call UpdateDatatypes
End Sub

Private Sub UpdateDatatypes()
Dim myCell As Excel.Range
Dim myRng As Excel.Range
Dim wks As Excel.Worksheet

For Each wks In Me.Worksheets
Set myRng = wks.UsedRange.Cells

For Each myCell In myRng.Cells
If (Not Intersect(myCell, wks.Range("h:n")) Is Nothing) Then
myCell.Value = CStr(Replace(myCell.Value, ",", "."))
End If
If (Not Intersect(myCell, wks.Range("a:b")) Is Nothing) Then
myCell.Value = CStr(Replace(myCell.Value, ",", "."))
End If
Next
Next
End Sub

Now, any cells that have only digits in them will be converted to a number
type by Excel.
If I use:

myCell.Value = "A" & CStr(Replace(myCell.Value, ",", "."))

then cells will have a text type after the script is run. How do I keep
excel from changing the
type of the cells with only digits in them to number?

I need this because the MS JET OleDb provider for excel is stupid and will
only allow
reading of a cell if it has the same type as the the cell in same column

in
the previous row
(if not it returns an empty string).

Casper




  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 6
Default How do I change the datatype of a cell?

In the following macro to each value of the defined cells
is "'" added. Due to this it gets a string.

Sub MakeString()
x = 4
For i = 1 To x
Cells(i, 1).Value = "'" & Cells(i, 1).Value
Next
End Sub

Regards
Klaus


-----Original Message-----
I need to change the datatype of all cells in a column to

text. I have the
following vbscript:

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As

Boolean, Cancel As
Boolean)
Call UpdateDatatypes
End Sub

Private Sub Workbook_Open()
Call UpdateDatatypes
End Sub

Private Sub UpdateDatatypes()
Dim myCell As Excel.Range
Dim myRng As Excel.Range
Dim wks As Excel.Worksheet

For Each wks In Me.Worksheets
Set myRng = wks.UsedRange.Cells

For Each myCell In myRng.Cells
If (Not Intersect(myCell, wks.Range("h:n"))

Is Nothing) Then
myCell.Value = CStr(Replace

(myCell.Value, ",", "."))
End If
If (Not Intersect(myCell, wks.Range("a:b"))

Is Nothing) Then
myCell.Value = CStr(Replace

(myCell.Value, ",", "."))
End If
Next
Next
End Sub

Now, any cells that have only digits in them will be

converted to a number
type by Excel.
If I use:

myCell.Value = "A" & CStr(Replace(myCell.Value, ",", "."))

then cells will have a text type after the script is run.

How do I keep
excel from changing the
type of the cells with only digits in them to number?

I need this because the MS JET OleDb provider for excel

is stupid and will
only allow
reading of a cell if it has the same type as the the cell

in same column in
the previous row
(if not it returns an empty string).

Casper


.

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 114
Default How do I change the datatype of a cell?

Hi Casper,

Use numberformat to change cell format.

mycell.Value = CStr(Replace(mycell.Value, ",", ""))
mycell.NumberFormat = "@" 'Change to Text Format


Regards,
Shah Shailesh
http://members.lycos.co.uk/shahweb/
(Excel Add-ins)


*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2
Default How do I change the datatype of a cell?

Thanks Klaus. This worked beatifully.

Also thanks to you all for the NumberFormat solution.
I'm sure I'm going to need that sometime.

"Klaus" wrote in message
...
In the following macro to each value of the defined cells
is "'" added. Due to this it gets a string.

Sub MakeString()
x = 4
For i = 1 To x
Cells(i, 1).Value = "'" & Cells(i, 1).Value
Next
End Sub

Regards
Klaus


-----Original Message-----
I need to change the datatype of all cells in a column to

text. I have the
following vbscript:

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As

Boolean, Cancel As
Boolean)
Call UpdateDatatypes
End Sub

Private Sub Workbook_Open()
Call UpdateDatatypes
End Sub

Private Sub UpdateDatatypes()
Dim myCell As Excel.Range
Dim myRng As Excel.Range
Dim wks As Excel.Worksheet

For Each wks In Me.Worksheets
Set myRng = wks.UsedRange.Cells

For Each myCell In myRng.Cells
If (Not Intersect(myCell, wks.Range("h:n"))

Is Nothing) Then
myCell.Value = CStr(Replace

(myCell.Value, ",", "."))
End If
If (Not Intersect(myCell, wks.Range("a:b"))

Is Nothing) Then
myCell.Value = CStr(Replace

(myCell.Value, ",", "."))
End If
Next
Next
End Sub

Now, any cells that have only digits in them will be

converted to a number
type by Excel.
If I use:

myCell.Value = "A" & CStr(Replace(myCell.Value, ",", "."))

then cells will have a text type after the script is run.

How do I keep
excel from changing the
type of the cells with only digits in them to number?

I need this because the MS JET OleDb provider for excel

is stupid and will
only allow
reading of a cell if it has the same type as the the cell

in same column in
the previous row
(if not it returns an empty string).

Casper


.





  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default How do I change the datatype of a cell?

Note that my code
myCell.Value = "'" & CStr(Replace(myCell.Text, ",", "."))
also included the single quote

just for interest.

--
Regards,
Tom Ogilvy



"Casper Hornstrup" wrote in message
...
Thanks Klaus. This worked beatifully.

Also thanks to you all for the NumberFormat solution.
I'm sure I'm going to need that sometime.

"Klaus" wrote in message
...
In the following macro to each value of the defined cells
is "'" added. Due to this it gets a string.

Sub MakeString()
x = 4
For i = 1 To x
Cells(i, 1).Value = "'" & Cells(i, 1).Value
Next
End Sub

Regards
Klaus


-----Original Message-----
I need to change the datatype of all cells in a column to

text. I have the
following vbscript:

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As

Boolean, Cancel As
Boolean)
Call UpdateDatatypes
End Sub

Private Sub Workbook_Open()
Call UpdateDatatypes
End Sub

Private Sub UpdateDatatypes()
Dim myCell As Excel.Range
Dim myRng As Excel.Range
Dim wks As Excel.Worksheet

For Each wks In Me.Worksheets
Set myRng = wks.UsedRange.Cells

For Each myCell In myRng.Cells
If (Not Intersect(myCell, wks.Range("h:n"))

Is Nothing) Then
myCell.Value = CStr(Replace

(myCell.Value, ",", "."))
End If
If (Not Intersect(myCell, wks.Range("a:b"))

Is Nothing) Then
myCell.Value = CStr(Replace

(myCell.Value, ",", "."))
End If
Next
Next
End Sub

Now, any cells that have only digits in them will be

converted to a number
type by Excel.
If I use:

myCell.Value = "A" & CStr(Replace(myCell.Value, ",", "."))

then cells will have a text type after the script is run.

How do I keep
excel from changing the
type of the cells with only digits in them to number?

I need this because the MS JET OleDb provider for excel

is stupid and will
only allow
reading of a cell if it has the same type as the the cell

in same column in
the previous row
(if not it returns an empty string).

Casper


.





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
Importing data. Problem with datatype in column Rolf Barbakken[_2_] Excel Discussion (Misc queries) 4 February 27th 09 06:46 AM
How do I validate the datatype of a cell's value in Excel 2007? SwordAngel Excel Discussion (Misc queries) 0 February 19th 09 09:36 AM
making copied cells change with change in original cell Jennifer Mcdermeit Excel Worksheet Functions 2 July 20th 06 04:58 PM
Save as a dbf file changes datatype araki Excel Discussion (Misc queries) 0 November 18th 05 10:42 PM
Vlookup datatype(?) issue ars Excel Worksheet Functions 1 January 25th 05 09:11 AM


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