+ Reply to Thread
Results 1 to 3 of 3

Run-time error '13': Type mismatch - How to work with Cell.value

Hybrid View

  1. #1
    Registered User
    Join Date
    07-30-2014
    Location
    Frederick, Maryland
    MS-Off Ver
    Professional Plus 2010
    Posts
    12

    Run-time error '13': Type mismatch - How to work with Cell.value

    Hi, I am new to VBA but have some familiarity with programming.

    I am trying to average a set of values based on the date the values were entered for each unique date. I am keeping a count and running total as my program looks through the data for each date that is the same as the previous (then dividing the "running total" by "count") to get the average). I am then trying to output this info in 2 columns - date and average value.

    I think the main problem is that I am trying to store the running total in a cell and then do math on it (based on what I could find on the error code given and other forums). But I can't seem to store the cell value in a variable either so I don't know the best practice or work around for this. Full code below, but I think the main problem is these two lines:

    Cells(k, 12).Value = Cells(k, 12).Value + Cells(i, 6).Value
    ...
    Cells(k, 12).Value = Cells(k, 12).Value / count

    Where k and i are iterative variables for the row.

     Private Sub CommandButton1_Click()
    Dim i As Integer
    Dim k As Integer
    Dim count As Integer
    
    i = 2
    k = 2
    count = 0
    
    
    Do While Cells(i, 1).Value <> ""
        If count = 0 Then
            'initial value for a new date
            Cells(k, 11).Value = Cells(i, 1).Value 'Column 1=date stamps, column 6=value; column 11=date, column 12=average
            Cells(k, 12).Value = Cells(i, 6).Value
            count = 1
        Else
            i = i + 1
            'if date is different, begin adding values from column 6 and store them in column 12
            If Cells(i, 1).Value = Cells(k, 11).Value Then
                Cells(k, 12).Value = Cells(k, 12).Value + Cells(i, 6).Value
                count = count + 1
            Else
            'if date is same, calculate average, and increment the place to store date (for average)
                Cells(k, 12).Value = Cells(k, 12).Value / count
                count = 0
                k = k + 1
            End If
        End If
        
    Loop
                    
    End Sub
    Thanks!
    Last edited by beutlerm; 07-30-2014 at 01:19 PM. Reason: Solved

  2. #2
    Forum Expert
    Join Date
    02-11-2014
    Location
    New York
    MS-Off Ver
    Excel 365 (Windows)
    Posts
    6,258

    Re: Run-time error '13': Type mismatch - How to work with Cell.value

    Your code worked fine for me. What line produces the error? Could you upload a sample workbook that produces the error?
    Bernie Deitrick
    Excel MVP 2000-2010

  3. #3
    Registered User
    Join Date
    07-30-2014
    Location
    Frederick, Maryland
    MS-Off Ver
    Professional Plus 2010
    Posts
    12

    Re: Run-time error '13': Type mismatch - How to work with Cell.value

    Hm! ok I found the errors! The code does work fine!

    There were 3-4 asterisks within the "value" column causing the error! (among some 15,000 entries so I never saw them before). I had to do a sort function to see what non numeric numbers were within that column! Hate when I don't understand small mistakes! Well, that took care of it! Thanks!

+ 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. [SOLVED] Run-time error 13 type mismatch
    By mukeshbaviskar in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-29-2013, 12:02 AM
  2. VBA now getting Runtime Error 13 - Type Mismatch but used to work perfectly
    By Royzer in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-26-2012, 02:29 PM
  3. Run-Time error 13 type mismatch
    By zplugger in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 06-06-2012, 09:38 PM
  4. Run time error '13' - type mismatch
    By a8747 in forum Excel Programming / VBA / Macros
    Replies: 19
    Last Post: 05-19-2012, 09:41 PM
  5. [SOLVED] Run-time error '13': Type mismatch
    By btmtdk in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 02-26-2012, 06:32 PM

Tags for this Thread

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