Text to numbers

opislak

Board Regular
Joined
Feb 28, 2017
Messages
68
Hi all,

Problem: make vba turn numbers stored as text into numbers, after the cells have been formatted as standard.

(sorry I cannot upload a mini sheet, for company policy prohibits installing software. I give screen shots hereby).

In the screen shots, you will find:
- in column A: the percentages I get from another application. There, they are stored as text
- in column B: a simple sum, so you see the errors immediately

This is a piece of my code:
VBA Code:
Sub txt2nbr()

    Cells.ClearFormats

    Set txt2nr = Range("A1:A12")
    txt2nr.TextToColumns DataType:=xlDelimited, _
        TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True, _
        Semicolon:=False, Comma:=True, Space:=False, Other:=False, FieldInfo _
        :=Array(1, 1), TrailingMinusNumbers:=True

End Sub

The problem occurs when the code is executed twice (or more) (that's how it goes in real life, and not always with new data, so it should be possible to execute more than once).
The code works fine, if "Cells.ClearFormats" is skipped. However, I need to clear the format of the cells first. Leaving this part out is not an option.

As I am a European, my Excel is set with a decimal comma, not a point.

What happens:
- at first run: everything seems OK (80%, left aligned is turned into 80%, right aligned)
- at second run: Cells.ClearFormats turns 80% into 0,8 (zero comma eight) --> result is OK
- but then the formula turns 0,8 (zero comma eight) into 0.8 (zero point eight) --> result is that everything but 100% (1) becomes text again.

I already tried Comma:=False and Comma:=True, but with the same result.
Thanks for any input!
Patrick.
 

Attachments

  • Capture.JPG
    Capture.JPG
    60.4 KB · Views: 18

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
I can't see anything in your screen captures, see if this fixes it.

VBA Code:
Sub txt2nbr()
Dim txt2nr as Range
Set txt2nr = Range("A1:A12")
With txt2nr
    .ClearFormats
    Range("XFD1048576").Copy
    .PasteSpecial Paste:=xlPasteAll, Operation:=xlAdd
End With
Application.CutCopyMode = False
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,104
Messages
6,170,125
Members
452,303
Latest member
c4cstore

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top