+ Reply to Thread
Results 1 to 7 of 7

Summing all rows in a user inputted column

Hybrid View

  1. #1
    Registered User
    Join Date
    01-26-2009
    Location
    Kansas, USA
    MS-Off Ver
    Excel 2007
    Posts
    51

    Summing all rows in a user inputted column

    Hello,

    Originally I asked this on vbaexpress, but that website appears to have gone away

    I need to sum all the rows, except the header row, of a column inputted by the user. The column will be located in a worksheet dubbed SourceSheet.

    Here's a few pieces of code related to what I am trying to do. The header row is always row 1. So the rows I'm summing are always 2 - myrows.

    Let me know if I left anything out, or if you have any questions.

    Thanks in advance.

    Sub CreateMatrixVector()
        Dim SourceSheet As Worksheet
        Dim WeightingRange As String
        Dim MyRows As Long
        Dim MySum As Long
                        
    'Original worksheet
        Set SourceSheet = ActiveSheet
        
    'Prompt for Weighting Range
        WeightingRange = InputBox("Please enter the column letter of the column used for weighting")
      
    'Count the number of rows in the worksheet
        MyRows = SourceSheet.UsedRange.Rows.Count
    
    End Sub
    Last edited by NukedWhale; 02-02-2009 at 07:07 PM. Reason: Solved

  2. #2
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: Summing all rows in a user inputted column

    Hello NukedWhale,

    Here is the updated macro...
    Sub CreateMatrixVector()
    
        Dim MySum As Long
        Dim SumRng As Range
        Dim SourceSheet As Worksheet
        Dim WeightingRange As String
                        
    'Original worksheet
        Set SourceSheet = ActiveSheet
        
    'Prompt for Weighting Range
        WeightingRange = InputBox("Please enter the column letter of the column used for weighting")
      
    'Sum the cells in choosen column
        With SourceSheet
          SumRng = .Range(.Cells(2, WeightingRange), .Cells(.Rows.Count, WeightingRange).End(xlUp))
          MySum = WorksheetFunction.Sum(SumRng)
        End With
        
    End Sub
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  3. #3
    Registered User
    Join Date
    01-26-2009
    Location
    Kansas, USA
    MS-Off Ver
    Excel 2007
    Posts
    51

    Re: Summing all rows in a user inputted column

    Hi Leith,

    First I would like to say thanks for taking the time to help!

    Can you help me troubleshoot the error I'm getting when I incorporate your code?

    Run time error 91
    Object variable or With block variable not set

          SumRng = .Range(.Cells(2, WeightingRange), .Cells(.Rows.Count, WeightingRange).End(xlUp))

  4. #4
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: Summing all rows in a user inputted column

    Hello NukedWhale,

    Sorry about that, the Set statement is missing to assign the Range to the object variable. Change that line to this...
        Set SumRng = .Range(.Cells(2, WeightingRange), .Cells(.Rows.Count, WeightingRange).End(xlUp))

  5. #5
    Registered User
    Join Date
    01-26-2009
    Location
    Kansas, USA
    MS-Off Ver
    Excel 2007
    Posts
    51

    Re: Summing all rows in a user inputted column

    Thank you! Worked like a charm.

  6. #6
    Registered User
    Join Date
    09-11-2008
    Location
    CO
    MS-Off Ver
    Office 2007
    Posts
    40

    Re: Summing all rows in a user inputted column

    Wouldn't this work, just entering it into a cell & substituting A with the letter of the column you're working with?
    =SUM((A:A)-A1)

  7. #7
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: Summing all rows in a user inputted column

    Hello LostBoy,

    Your post does not comply with Rule 2 of our Forum RULES. Don't post a question in the thread of another member -- start your own thread. If you feel it's particularly relevant, provide a link to the other thread.

    Also the OP requested a VBA solution not a Worksheet Formula solution (Different Forum).

+ 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