+ Reply to Thread
Results 1 to 6 of 6

Numbers not recognized as such

Hybrid View

  1. #1
    Registered User
    Join Date
    10-01-2013
    Location
    Sweden
    MS-Off Ver
    Excel 2013
    Posts
    7

    Numbers not recognized as such

    Hello

    Here is my macro.. It processes data from a .csv imported.
    I managed to get the data I need from one sheet to the other. Now I would like to eliminate some rows (where the value of the transaction is positive).
    However, it looks like the values in the range "transactions" (and thus cell) are not recognized as numbers.
    Checking if they are positive (cell >= 0) gives a type error, and indeed it the "IsNumeric" gives a false (and the TypeName gives back a variant).
    The numbers are formatted something like -123,45, but I also tried using plain integers (1, 10, 100) and they are still not recognized as numeric.

    What is the problem?

    Private Sub CommandButton2_Click()
    'Create a new sheet named as the considered month
    Dim newsheet
    Set newsheet = Sheets.Add(After:=Sheets(Worksheets.Count))
    newsheet.Name = MonthName((Month(Now) - 1)) & " " & Year(Now)
    
    'Copies the content to the new sheet
    ActiveSheet.Range("A1:C3").Value = Worksheets(1).Range("A1:C3").Value
    ActiveSheet.Range("A2").Select
    ActiveSheet.Paste
    
    'Takes away the rows with positive transactions
    Dim transactions As Range
    Set transactions = Selection.Columns(3)
    transactions.Select
    transactions.NumberFormat = "0.00"
    For Each cell In transactions
    cell.Select
    If IsNumeric(cell.Value) Then ' <---    Here is the problem!
    MsgBox "whatever"
    Else
    MsgBox TypeName(cell.Value)
    'cell.EntireRow.Delete
    transactions.Select
    End If
    Next
    Last edited by d_abbatelli; 07-01-2014 at 08:53 AM.

  2. #2
    Forum Expert Jakobshavn's Avatar
    Join Date
    08-17-2012
    Location
    Lakehurst, NJ, USA
    MS-Off Ver
    Excel 2007
    Posts
    1,970

    Re: Numbers not recognized as such

    You may have blanks or other non-visible characters in the cells. This first step is to see what you have. Pick a cell and run:

    Sub WhatDoWeHave()
        txt = ActiveCell.Text
        l = Len(txt)
        msg = ""
        For i = 1 To l
            ch = Mid(txt, i, 1)
            msg = msg & ch & vbTab & Asc(ch) & vbCrLf
        Next i
        MsgBox msg
    End Sub
    Gary's Student

  3. #3
    Registered User
    Join Date
    10-01-2013
    Location
    Sweden
    MS-Off Ver
    Excel 2013
    Posts
    7

    Re: Numbers not recognized as such

    Quote Originally Posted by Jakobshavn View Post
    You may have blanks or other non-visible characters in the cells. This first step is to see what you have. Pick a cell and run:

    Sub WhatDoWeHave()
        txt = ActiveCell.Text
        l = Len(txt)
        msg = ""
        For i = 1 To l
            ch = Mid(txt, i, 1)
            msg = msg & ch & vbTab & Asc(ch) & vbCrLf
        Next i
        MsgBox msg
    End Sub
    Here is what I got, on a cell which contains only "10"

    1    49
    0    48
    On a cell that contains -178,3:

    -    45
    1    49
    7    55
    8    56
    ,    44
    3    51
    Doesn't seem to be any extra character...

  4. #4
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229

    Re: Numbers not recognized as such

    try
    For Each cell In transaction.Cells
    _
    ...How to Cross-post politely...
    ..Wrap code by selecting the code and clicking the # or read this. Thank you.

  5. #5
    Registered User
    Join Date
    10-01-2013
    Location
    Sweden
    MS-Off Ver
    Excel 2013
    Posts
    7

    Re: Numbers not recognized as such

    Quote Originally Posted by mikerickson View Post
    try
    For Each cell In transaction.Cells
    This worked!

    Thanks!

  6. #6
    Forum Expert
    Join Date
    05-30-2012
    Location
    The Netherlands
    MS-Off Ver
    Office 365
    Posts
    14,987

    Re: Numbers not recognized as such

    You could try this code.

    Sub TextRangeToNumbers_incolumn_A()
    'Made by Datsmart 14-07-2006 on mr Excel
        Dim rng As Range
        Dim c As Range
    'Change Column A referrence as needed
        Set rng = Range("A1:A" & Range("A1500").End(xlUp).Row)
        On Error Resume Next
    'Cycle through each item in Rng, Trim value then add zero
        For Each c In rng
            If c.Value <> "" Then
                Application.WorksheetFunction.Trim (c.Value)    'Trim removes any spaces
                c.Value = c.Value + 0   'Adding 0 changes item to number
            Else
            End If
        Next c
    End Sub
    Notice my main language is not English.

    I appreciate it, if you reply on my solution.

    If you are satisfied with the solution, please mark the question solved.

    You can add reputation by clicking on the star * add reputation.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 7
    Last Post: 07-10-2013, 09:45 PM
  2. [SOLVED] Numbers in cells not recognized when using InputBox to copy and paste rows
    By Nikolette in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 04-25-2013, 07:09 PM
  3. Data from Access to Excel (numbers not recognized)
    By excelcool in forum Excel General
    Replies: 0
    Last Post: 02-02-2011, 05:06 PM
  4. Value not recognized as value
    By Roadie in forum Excel General
    Replies: 2
    Last Post: 04-20-2005, 02:54 AM
  5. [SOLVED] Decimal numbers not recognized as numbers
    By Stein Kristiansen in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 02-13-2005, 06:06 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1