Results 1 to 4 of 4

Thread: an extreme newbie vba question

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Apr 2002
    Posts
    27

    an extreme newbie vba question

    How do I get a the sum of a range of cells to display in a msgbox when the user clicks on a command button? In excel, its using the SUM function, but VBA doesnt have that. For instance, i want the sum of cells J10 to :J30....

  2. #2
    Addicted Member
    Join Date
    Jan 2005
    Posts
    138

    Re: an extreme newbie vba question

    Set myRange = Worksheets("Sheet1").Range("A1:C10")
    answer = Application.WorksheetFunction.Sum(myRange)
    MsgBox answer

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Apr 2002
    Posts
    27

    Re: an extreme newbie vba question

    I keep getting an error saying "Subscript out of range". any ideas?

  4. #4
    Addicted Member
    Join Date
    Jan 2005
    Posts
    138

    Re: an extreme newbie vba question

    try this:


    VB Code:
    1. Option Explicit
    2.  
    3. Sub test()
    4. Dim myRange As Range
    5. Dim answer As String
    6.  
    7. Set myRange = Worksheets("Sheet1").Range("A1:C10")
    8. answer = Application.WorksheetFunction.Sum(myRange)
    9. MsgBox "The sum is " & Format(answer, "currency")
    10. End Sub

Posting Permissions

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



Click Here to Expand Forum to Full Width