how to convert a range from numeric to text type? but quickly

L

lauralucas

hello

I'm currently using this macro for converting a column that has mixed
types (text and numeric) and change it to being text.

Sub AddSpace()

Dim r As Range
Dim Wks As Worksheet
Dim cell As Object
Dim found As Boolean

For IndexSheet = 1 To Sheets.Count
Set Wks = Sheets(IndexSheet)
With Wks
.Activate

'find the column that has to be changed
found = False
For Each r In .Range("1:1")
If r.Value2 = "Values" Or r.Value2 = "Value" Then
r.Activate
found = True
Exit For
End If
Next
If found = True Then

Range(Selection, Selection.End(xlDown)).Select 'this is the
'column to change


Selection.NumberFormat = "@" 'not there yet, we have to
'add and remove a space
'from each cell to achieve desired result

For Each cell In Selection 'this is the slow part,
'this is too slow for 39800 rows
cell.Value = " " & cell.Value
cell.Value = Right(cell.Value, Len(cell.Value) - 1)
Next
End If
End With

Next
ActiveWorkbook.Save
End Sub
 
T

Tom Ogilvy

Sub AddSpace()

Dim r As Range
Dim Wks As Worksheet
Dim cell As Object
Dim found As Boolean
Dim v as Variant

For IndexSheet = 1 To Sheets.Count
Set Wks = Sheets(IndexSheet)
With Wks
.Activate

'find the column that has to be changed
found = False
For Each r In .Range("1:1")
If r.Value2 = "Values" Or r.Value2 = "Value" Then
r.Activate
found = True
Exit For
End If
Next
If found = True Then

Range(Selection, Selection.End(xlDown)).Select 'this is the
'column to change


Selection.NumberFormat = "@" 'not there yet, we have to
'add and remove a space
'from each cell to achieve desired result
v = Selection
for i = 1 to ubound(v)
if isnumeric(v) then
v(i,1) = "'" & v(i,1)
end if
next i
Selection.Value = v
End If
End With

Next
ActiveWorkbook.Save
End Sub
 
L

lauralucas

sorry, that didn't work, in excel application, a green triangle should
appear with the error/warning "Number stored as text"
That is the only way to make sure the number is really of TEXT type,
and running your macro this warning didn't appear...
 
T

Tom Ogilvy

mea culpa, I had a typo:

Sub AddSpace()

Dim r As Range
Dim Wks As Worksheet
Dim cell As Object
Dim found As Boolean
Dim v as Variant

For IndexSheet = 1 To Sheets.Count
Set Wks = Sheets(IndexSheet)
With Wks
.Activate

'find the column that has to be changed
found = False
For Each r In .Range("1:1")
If r.Value2 = "Values" Or r.Value2 = "Value" Then
r.Activate
found = True
Exit For
End If
Next
If found = True Then

Range(Selection, Selection.End(xlDown)).Select 'this is the
'column to change


Selection.NumberFormat = "@" 'not there yet, we have to
'add and remove a space
'from each cell to achieve desired result
v = Selection
for i = 1 to ubound(v)
if isnumeric(v(i,1)) then
v(i,1) = "'" & v(i,1)
end if
next i
Selection.Value = v
End If
End With

Next
ActiveWorkbook.Save
End Sub
 

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