+ Reply to Thread
Results 1 to 9 of 9

Using IsNumeric with If and For functions

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    12-02-2010
    Location
    Boise, Idaho
    MS-Off Ver
    Excel 2013, 2016
    Posts
    100

    Using IsNumeric with If and For functions

    Hello All,

    I'm sure this is very simple but i'm writing to try and further my knowledge. I've found a way to do this with a sumif() formula in the actual worksheet but i like to keep everything in VBA.

    Anyway, I'm trying to sum a column of values, but some of the cells in that column have words and sentences instead of numbers, so i need to sum only the numerical values in the column.

    What i've come up with so far is

    Sub sum()
    
    Dim total As Boolean
    Dim value As Integer
    
    For Job = 1 To 360
    
    value = Worksheets("2012's").Cells(4, 1 + Job)
    
        If IsNumeric("value") = True Then
    
        total = total + value
    
        End If
    
    Next Job
    
    Worksheets("2012's").Cells(4, 365) = total
    
    End Sub

    Any help would be greatly appreciated.

    Thank you all,

    DarkF1ame
    Last edited by DarkF1ame; 05-16-2012 at 03:52 PM.

  2. #2
    Valued Forum Contributor
    Join Date
    05-07-2012
    Location
    USA
    MS-Off Ver
    Excel 2007
    Posts
    354

    Re: Using IsNumeric with If and For functions

    replace two lines:

    value = Worksheets("2012's").Cells(4, 1 + Job).value
    
        If worksheetfunction.IsNumeric(value) = True Then
    Regards,
    Vandan

  3. #3
    Forum Contributor
    Join Date
    12-02-2010
    Location
    Boise, Idaho
    MS-Off Ver
    Excel 2013, 2016
    Posts
    100

    Re: Using IsNumeric with If and For functions

    Thank you,

    I'm still getting a "runtime-error '13' Type Mismatch.

    I don't know why

  4. #4
    Valued Forum Contributor
    Join Date
    05-07-2012
    Location
    USA
    MS-Off Ver
    Excel 2007
    Posts
    354

    Re: Using IsNumeric with If and For functions

    I apologize. I didn't check my code before sending.
    change
    If worksheetfunction.IsNumeric(value) = True Then
    to
    If WorksheetFunction.IsNumber(value) = True Then

  5. #5
    Forum Contributor
    Join Date
    12-02-2010
    Location
    Boise, Idaho
    MS-Off Ver
    Excel 2013, 2016
    Posts
    100

    Re: Using IsNumeric with If and For functions

    ^Tim^ I'm trying to make sure the values that return are only numbers, so i don't know how to do that in VBA.

    Ok here is the whole thing and I still am not getting it to work.



    Jobs.xls

  6. #6
    Valued Forum Contributor
    Join Date
    05-14-2012
    Location
    Unknown
    MS-Off Ver
    Unseen
    Posts
    429

    Re: Using IsNumeric with If and For functions

    For simplicity, you could use the SUMIF that you have already established.

    Use Application.Worksheetfunction.Sumif(Range("...."),...

    For example:
    Application.WorksheetFunction.SumIf(Range("B19:B23"), Range("B19"), Range("C19:C23"))
    This is equivalent to: =SUMIF(B19:B23,B19,C19:C23)


    TIM

  7. #7
    Valued Forum Contributor
    Join Date
    05-07-2012
    Location
    USA
    MS-Off Ver
    Excel 2007
    Posts
    354

    Re: Using IsNumeric with If and For functions

    try this...

    Option Explicit
    Private Sub Worksheet_activate()
    
    Dim lTotal As Long
    Dim iJob As Integer
    Dim vValue As Variant
    
    For iJob = 1 To 360
    
         vValue = Worksheets("2012's").Cells(1 + iJob, 4).value
    
        lTotal = lTotal + val(vValue)
    
    Next iJob
    
    Worksheets("2012's").Cells(365, 4) = lTotal
    
    End Sub

  8. #8
    Valued Forum Contributor
    Join Date
    05-14-2012
    Location
    Unknown
    MS-Off Ver
    Unseen
    Posts
    429

    Re: Using IsNumeric with If and For functions

    You said you had managed to put together a SUMIF formula in the worksheet. I have given you an example of translating that into VBA. I'm sure that would be better than looping through a long list.

    What is the SUMIF formula that you are currently using?

    TIM

  9. #9
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: Using IsNumeric with If and For functions

    The formula in E367 could be replaced with just =SUM(E2:E361)

    Sum ignores text and numbers stored as text.
    Last edited by shg; 05-16-2012 at 08:39 AM.
    Entia non sunt multiplicanda sine necessitate

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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