Convert Text into numbers with vba (1 Viewer)

Jorispk

New member
Local time
Today, 01:13
Joined
Nov 30, 2011
Messages
6
Goodafternoon,

I have written a vba code that copies a list from one workbook to another workbook. This all works perfectly.

But the list contains numbers that are written as text values. (The list is big most of the times over 1000 rows), I need the text values converted into numbers.

The numbers(Displayed as text) give an error message:

Number is stored as text


I can choose to convert the text to numbers this is exactly what I need vba to do!


My code:
Code:
Private Sub CMD_GetWeight_Click()
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Dim wbk As Workbook
Dim wbk2 As Workbook
Dim strFirstFile As String
Dim strSecondFile As String
strFirstFile = Me.L_ListLocation.Caption
strSecondFile = "test.xls"
Set wbk = Workbooks.Open(strFirstFile)
With wbk.Sheets("Sheet1")
    Range("A9:Q65536").Copy
End With
Set wbk2 = Workbooks.Open(strSecondFile)
With wbk2.Sheets("List")
    Range("A2:Q65536").PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:= _
        False, Transpose:=False
    Range("J:J").Select
    For Each cell In Selection
        cell.Value = Val(cell.Value)
    Next
    Selection.NumberFormat = "General"
End With
Application.DisplayAlerts = True
Application.ScreenUpdating = True

Note: This works, but there is one problem and another thing I would like to fix.

1. The column J:J contains text values like 1 X 30 or 1 + 1/2 and text like 3. When Vba converts this to general numbers 1 X 30 becomes 1, 1 + 1/2 becomes 1 and 3 becomes 3. But 1 X 30 needs to be 1 X 30 and 1 + 1/2 to be 1 + 1/2 after converting.

When you convert yourself trough the error message it stays the same.. I don't get why this is changing all my numbers.


2. My code takes every cell apart and fills 0 in the empty cells, it would be better if it just leaves the cells empty. (This proces is time consuming)

Thank you very much!

Hope someone have an answer
 

Brianwarnock

Retired
Local time
Today, 09:13
Joined
Jun 2, 2003
Messages
12,701
A couple of points
1 Your code is behaving as one would expect Val to behave
2
When you convert yourself trough the error message it stays the same

cells containing 1*3 or 1+1/2 are text and would not flag up as numbers stored as text

3 you always convert the whole column even if there are only a 1000 rows, this is unnecessary as you can look for the last row

4 I am a little rusty now but I believe the use of Selection slows down code as it causes unecessary reference to the worksheet.


I use *1 to convert numbers stored as text to numbers, but if there are side issues such as you have then you need to filter the data, so sticking to converting the whole column my code would be something like

Code:
For Each c In Range("J:J")
       If c = "" Or c = " " Then GoTo nextc
       nc = Val(c)
       If Len(nc) < Len(c) Or nc = 0 Then GoTo nextc
        c.Value = c.Value * 1
        c.NumberFormat = "general"
nextc:
Next c


Brian
 

Brianwarnock

Retired
Local time
Today, 09:13
Joined
Jun 2, 2003
Messages
12,701
erm :eek: :eek:

now that i'm sitting at my old steam driven desktop instead of on the sofa with my wife's Ipad I cannot believe what I wroye yesterday, mind you it did work, :cool:, but try

Code:
For Each c In Range("J:J")
    If c = "" Then GoTo nextc
    If IsNumeric(c) Then
        c.Value = c.Value * 1
        c.NumberFormat = "general"
    End If
    
nextc:
Next c

Brian
 

Jorispk

New member
Local time
Today, 01:13
Joined
Nov 30, 2011
Messages
6
Thank you very much Brian

This code works perfectly, Ty!
 

Rx_

Nothing In Moderation
Local time
Today, 02:13
Joined
Oct 22, 2009
Messages
2,803
Great Post! After starting here and looking at well over a dozen other sites, I am back with another option to share.

Using the CopyFromRecordset method from Access to an Excel automation, the Date Diff converted to number. Here is the catch. The function for the Date2Diff must return a Variant inorder to preserve either a number or a null. Using CopyFromRecordset method converts the vairant numbers to a Text and the Nulls come to Excel blank (as I wanted).

If either one of the two dates are missing, a blank is preferrable in Excel instead of a Zero. The loop you described was OK, but my reports may have 15,000 records and 8 columns that need this conversion.
Plus, during some looping - the blank can be converted to a 0 in Excel.
A second loop to check for zero can be required.

After searching the afternoon and looking at a dozen ways to deal with this, I found the following:

ObjXL.Range("F6:F11132") = ObjXL.Range("F6:F11132").Value

This is the exact equal to selecting a range with the Warning icon and selecting Convert Text into Numbers on the Excel interface.
All the blanks stay blank, all the text turn to numbers. No loops (hooray!)
By saving the recordset count the F11132 can be substituted with a variable "F6:F" & RecordCount

What is the big deal about displaying a Number instead of a Text in Excel?
When a filter is added on the header and the formula is added above the header: = AGGREGATE(4,3,F6:F11132)
The formula won't read text. Once converted to a number, the formula will work - and the formula will ignore the hidden rows during a filter.
 

Attachments

  • change excel text to number.jpg
    change excel text to number.jpg
    57.3 KB · Views: 1,419
Last edited:

Users who are viewing this thread

Top Bottom