check data

jagguy

New Member
Joined
Jul 20, 2007
Messages
5
Hi,

I want to create a macro that can check each the data of each cell to make sure it is a number before summing them .
eg sum cells a1-a20 and check if each cell has a number in it.

I know how to create a if statement but not to check on all cells with a macro.
 

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
Welcome to the Board!

You could do something like this:

Code:
Sub test()
dim total as double
for i = 1 to 20
If isnumeric(cells(i,1).value) then
total = total + cells(i,1).value
end if
next i

msgbox("The total is " & total)
end sub
 
Upvote 0
This function will signal if all the cells in the range contain numbers.
Code:
Function allAreNumbers(inRay As Range)
    Dim cellCount As Long
    cellCount = inRay.Cells.Count
    On Error Resume Next
    cellCount = cellCount - inRay.SpecialCells(xlCellTypeConstants, 1).Cells.Count
    cellCount = cellCount - inRay.SpecialCells(xlCellTypeFormulas, 1).Cells.Count
    On Error GoTo 0
    allAreNumbers = (cellCount = 0)
End Function
 
Upvote 0
Welcome to the Board!

Why a macro? SUM will ignore text and only operate on numbers.

I.E.
Book3
ABCD
11
22
33
4a
5c
6b
74
85
96
10e
11f
12g
1321
Sheet1


But here's some code that will do the same thing in the manner you asked (but I don't understand why):<font face=Tahoma><SPAN style="color:#00007F">Sub</SPAN> foo()
    <SPAN style="color:#00007F">Dim</SPAN> c<SPAN style="color:#00007F">As</SPAN> Range
    <SPAN style="color:#00007F">Dim</SPAN> i
        i = 0
            <SPAN style="color:#00007F">For</SPAN><SPAN style="color:#00007F">Each</SPAN> c<SPAN style="color:#00007F">In</SPAN> ActiveSheet.Range([A1], Cells(Rows.Count, "A").End(xlUp))
                <SPAN style="color:#00007F">If</SPAN> IsNumeric(c)<SPAN style="color:#00007F">Then</SPAN> i = i + c.Value
            <SPAN style="color:#00007F">Next</SPAN> c
        MsgBox i<SPAN style="color:#00007F">End</SPAN><SPAN style="color:#00007F">Sub</SPAN></FONT>

Hope that helps,

Smitty
 
Upvote 0
Hi,

Thanks for the replies.

What about also using data validation tool? Is there a problem with this?

So I can say only enter in numbers or just strings with no numbers and display an error message if i do?.
 
Upvote 0
Date?
Code:
Sub test()
Dim r As Range, myTotal As Double
For Each r In Selection
     If (IsNumeric(r.Value)) * (Not IsDate(r.Value)) Then myTotal = myTotal + r.Value
Next
MsgBox myTotal
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,585
Messages
6,120,394
Members
448,957
Latest member
Hat4Life

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