PC Review


Reply
Thread Tools Rate Thread

Change Number to Text , Case error when cell is String and format isGeneral

 
 
moonhk
Guest
Posts: n/a
 
      19th Feb 2010
Hi All

How to check the cell is text or number ?
When Cell is string, Case Excel close and recoveing.

Sub FormatNum2str()
'~~ 2010/02/18
Dim iRows As Long
Dim iCols As Long
Dim ir, ic As Long
Dim iNum As Long
Dim strVal As String
If Application.Selection Is Nothing Then
MsgBox "No Open Worksheet", vbCritical
Exit Sub
End If

iRows = Application.Selection.Rows.Count
iCols = Application.Selection.Columns.Count


For ic = 1 To iCols
For ir = 1 To iRows
With Application.Selection.Item(ir, ic)
If .NumberFormatLocal = "General" Then
strVal = str(.Value)
.NumberFormatLocal = "@" '~~ Format as a text
.Value = strVal
End If
End With
Next ir
Next ic

End Sub
 
Reply With Quote
 
 
 
 
OssieMac
Guest
Posts: n/a
 
      19th Feb 2010
Change following line

strVal = Str(.Value)

to this
strVal = Format(.Value, "0")

You can use any valid number format between the double quotes.

--
Regards,

OssieMac


"moonhk" wrote:

> Hi All
>
> How to check the cell is text or number ?
> When Cell is string, Case Excel close and recoveing.
>
> Sub FormatNum2str()
> '~~ 2010/02/18
> Dim iRows As Long
> Dim iCols As Long
> Dim ir, ic As Long
> Dim iNum As Long
> Dim strVal As String
> If Application.Selection Is Nothing Then
> MsgBox "No Open Worksheet", vbCritical
> Exit Sub
> End If
>
> iRows = Application.Selection.Rows.Count
> iCols = Application.Selection.Columns.Count
>
>
> For ic = 1 To iCols
> For ir = 1 To iRows
> With Application.Selection.Item(ir, ic)
> If .NumberFormatLocal = "General" Then
> strVal = str(.Value)
> .NumberFormatLocal = "@" '~~ Format as a text
> .Value = strVal
> End If
> End With
> Next ir
> Next ic
>
> End Sub
> .
>

 
Reply With Quote
 
moonhk
Guest
Posts: n/a
 
      19th Feb 2010
On 2月19日, 下午12時56分, OssieMac <Ossie...@discussions.microsoft.com>
wrote:
> Change following line
>
> strVal = Str(.Value)
>
> to this
> strVal = Format(.Value, "0")
>
> You can use any valid number format between the double quotes.
>
> --
> Regards,
>
> OssieMac
>
>
>
> "moonhk" wrote:
> > Hi All

>
> > How to check the cell is text or number ?
> > When Cell is string, Case Excel close and recoveing.

>
> > Sub FormatNum2str()
> > '~~ 2010/02/18
> > Dim iRows As Long
> > Dim iCols As Long
> > Dim ir, ic As Long
> > Dim iNum As Long
> > Dim strVal As String
> > If Application.Selection Is Nothing Then
> > * * MsgBox "No Open Worksheet", vbCritical
> > * * Exit Sub
> > End If

>
> > iRows = Application.Selection.Rows.Count
> > iCols = Application.Selection.Columns.Count

>
> > For ic = 1 To iCols
> > * * For ir = 1 To iRows
> > * * * * With Application.Selection.Item(ir, ic)
> > * * * * * * If .NumberFormatLocal = "General" Then
> > * * * * * * * * strVal = str(..Value)
> > * * * * * * * * .NumberFormatLocal = "@" '~~ Format as a text
> > * * * * * * * * .Value = strVal
> > * * * * * * *End If
> > * * * * End With
> > * * Next ir
> > Next ic

>
> > End Sub
> > .- 隱藏被引用文* -

>
> - 顯示被引用文* -


Thank It works.


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Extract number from text string based on number's format? MeatLightning Microsoft Excel Misc 16 19th Nov 2008 01:08 AM
Change 3 letter text string to a number string Pete Microsoft Excel Misc 3 31st Dec 2007 07:47 PM
word match in string text in cell, color format cell =?Utf-8?B?anBtYWhvbnk=?= Microsoft Excel Misc 1 31st Oct 2007 03:56 PM
Excel should let me change case in the cell the text is in. =?Utf-8?B?ZHdt?= Microsoft Excel Worksheet Functions 1 27th Sep 2005 08:49 PM
Change case for all text in cell range R. Lehr Microsoft Excel Misc 5 28th Jan 2004 05:20 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 11:50 PM.