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

M

moonhk

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
 
O

OssieMac

Change following line

strVal = Str(.Value)

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

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

moonhk

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










- 顯示被引用文字 -

Thank It works.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top