Wrox Programmer Forums
|
Excel VBA Discuss using VBA for Excel programming.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Excel VBA section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old March 3rd, 2008, 05:44 PM
Authorized User
 
Join Date: Mar 2008
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Default VBA function help

Hello,
I want to create a function in vba for excel that uses the equivalent of sumif() function. Here is what I have as an example:

- - 1 - 2 - 3 - 4 -5
1 - A - A - C - D - F
2 - B - B - B - A - A
3 - C - B - A - B - A
4 - D - A - B - D - D
5 - A - C - B - A - D


What I have here are column (1-5) and rows (1-5) in excel that with letter grades. The value of the letter grades are A = 4, B = 3, C = 2, D = 1, F = 0. So i want to get a sum of these values accros for each row. How would I go about making this in vba.
Also, just to make life harder, lets say column 2 had different values for the letter grades such as (A = 12, B = 9, C = 6, D = 3, F = 0) but all the other columns were as before. How would i sum this up. Any help would be great. I am new to vba. Thanks in advance


Carbon_13
__________________
Carbon_13
 
Old March 4th, 2008, 09:49 PM
Authorized User
 
Join Date: Nov 2007
Posts: 48
Thanks: 0
Thanked 4 Times in 4 Posts
Default

carbon_13,

Please TEST this FIRST in a COPY of your workbook (always make a backup copy before trying new code, you never know what you might lose).

Press and hold down the 'ALT' key, and press the 'F11' key.

Insert a Module in your VBAProject, Microsoft Excel Objects

Copy the below code, and paste it into the Module1.

Code:
Option Explicit
Function SumColumns(r As Range) As Long
'
' Function created 03/04/2008 by stanleydgromjr
'
' for carbon_13
' http://p2p.wrox.com/post.asp?method=...90&FORUM_ID=72
'
' Formula in cell F1 (copied down):
' =SumColumns($A1:$E1)
'
'
    Dim c
    Dim lngLoopCtr As Long
    Dim lngTotal As Long
    lngTotal = 0
    lngLoopCtr = 1
    For Each c In r.Cells
        Select Case lngLoopCtr
            'value of the letter grades are A = 4, B = 3, C = 2, D = 1, F = 0
            Case 1, 3, 4, 5
                Select Case c
                    Case "A"
                        lngTotal = lngTotal + 4
                        lngLoopCtr = lngLoopCtr + 1
                    Case "B"
                        lngTotal = lngTotal + 3
                        lngLoopCtr = lngLoopCtr + 1
                    Case "C"
                        lngTotal = lngTotal + 2
                        lngLoopCtr = lngLoopCtr + 1
                    Case "D"
                        lngTotal = lngTotal + 1
                        lngLoopCtr = lngLoopCtr + 1
                    Case "F"
                        lngTotal = lngTotal + 0
                        lngLoopCtr = lngLoopCtr + 1
                End Select
            'column 2 had different values for the letter grades such as (A = 12, B = 9, C = 6, D = 3, F = 0)
            Case 2
                Select Case c
                    Case "A"
                        lngTotal = lngTotal + 12
                        lngLoopCtr = lngLoopCtr + 1
                    Case "B"
                        lngTotal = lngTotal + 9
                        lngLoopCtr = lngLoopCtr + 1
                    Case "C"
                        lngTotal = lngTotal + 6
                        lngLoopCtr = lngLoopCtr + 1
                    Case "D"
                        lngTotal = lngTotal + 3
                        lngLoopCtr = lngLoopCtr + 1
                    Case "F"
                        lngTotal = lngTotal + 0
                        lngLoopCtr = lngLoopCtr + 1
                End Select
        End Select
    Next c
    SumColumns = lngTotal
End Function

With your data beginning in row 1, cell A1, enter the following formula in cell F1 (then copy it down):
=SumColumns($A1:$E1)


Have a great day,
Stan





stanleydgromjr

Windows Vista Business and Excel 2003, 2007.
 
Old March 5th, 2008, 05:17 PM
Authorized User
 
Join Date: Mar 2008
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thank you so much for the help. The code was just perfect. I must say you are pretty good with vba. Just wanted to say thanks again.


Carbon_13





Similar Threads
Thread Thread Starter Forum Replies Last Post
"Wait" function in VBA wscheiman Access VBA 15 December 18th, 2010 05:16 PM
MS Access VBA Dateddiff Function SKZ Access VBA 5 November 18th, 2008 08:38 AM
Excel 2003 VBA Function RollingWoodFarm Excel VBA 15 August 2nd, 2006 04:24 PM
Create text pop-up in VBA function jimmy_chu Excel VBA 0 December 10th, 2003 12:15 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.