+ Reply to Thread
Results 1 to 8 of 8

How can I perform a conditional test based on the sum a row of cells using VBA

  1. #1
    Registered User
    Join Date
    05-05-2009
    Location
    Australia
    MS-Off Ver
    Excel 2010
    Posts
    68

    Question How can I perform a conditional test based on the sum a row of cells using VBA

    Hi

    There is no doubt an exceedingly simple answer to this question however using VBA script to perform basic mathematical calculations is not something I am familiar with.

    In the past I have used VBA script to change column with size based on cell values as per follows:

    Please Login or Register  to view this content.
    Now I wish to change row size in a similar fashion only the criteria will be a maths function rather than a string in a single cell. The maths function will simply be a sum of cells in a row. I will need to test whether or not the row ads up to a number greater than zero.

    To illustrate what I am trying to achieve I have (poorly) written out a rough attempt at the code below:

    Please Login or Register  to view this content.
    I am unsure how the syntax works for this, I considered defining the range as a string first so I could test the value of a single variable (by defined name) rather than directly referencing the row of cells in the formula. As this code needs to be repeated many time for many rows the simplest solution would be b

    An assistance greatly appreciated.

    Michael.

  2. #2
    Forum Guru Izandol's Avatar
    Join Date
    03-29-2012
    Location
    *
    MS-Off Ver
    Excel 20(03|10|13)
    Posts
    2,581

    Re: How can I perform a conditional test based on the sum a row of cells using VBA

    It is very close:
    Please Login or Register  to view this content.
    or
    Please Login or Register  to view this content.
    • Please remember to mark threads Solved with Thread Tools link at top of page.
    • Please use code tags when posting code: [code]Place your code here[/code]
    • Please read Forum Rules

  3. #3
    Registered User
    Join Date
    05-05-2009
    Location
    Australia
    MS-Off Ver
    Excel 2010
    Posts
    68

    Re: How can I perform a conditional test based on the sum a row of cells using VBA

    Much appreciate the reply although unfortunately I cant seem to get either of these to work

    Application code seems to cause:

    Run-time error '13':

    Type mismatch


    Worksheetfunction code seems to cause:

    Run-time error '438':
    Object doesn't support this property or method

    Maybe I should add that the code is within an option Explicit Worksheet Activate format as follows:

    Please Login or Register  to view this content.
    Last edited by mick86; 02-06-2014 at 07:53 PM.

  4. #4
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: How can I perform a conditional test based on the sum a row of cells using VBA

    Maybe:

    Option Explicit

    Private Sub Worksheet_Activate()

    If WorksheetFunction.Sum(Range("C6:Q6")) < 0 Then 'Week 1
    Rows("6").RowHeight = 0
    Else
    Rows("6").RowHeight = 20
    End If
    End Sub
    If I've helped you, please consider adding to my reputation - just click on the liitle star at the left.

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~(Pride has no aftertaste.)

    You can't do one thing. XLAdept

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~aka Orrin

  5. #5
    Forum Guru Izandol's Avatar
    Join Date
    03-29-2012
    Location
    *
    MS-Off Ver
    Excel 20(03|10|13)
    Posts
    2,581

    Re: How can I perform a conditional test based on the sum a row of cells using VBA

    I am sorry - I omitted the Range("c6:q6")
    You omitted the .Sum

  6. #6
    Forum Guru Izandol's Avatar
    Join Date
    03-29-2012
    Location
    *
    MS-Off Ver
    Excel 20(03|10|13)
    Posts
    2,581

    Re: How can I perform a conditional test based on the sum a row of cells using VBA

    Also you may use
    Please Login or Register  to view this content.

  7. #7
    Registered User
    Join Date
    05-05-2009
    Location
    Australia
    MS-Off Ver
    Excel 2010
    Posts
    68

    Re: How can I perform a conditional test based on the sum a row of cells using VBA

    Quote Originally Posted by xladept View Post
    Maybe:

    Option Explicit

    Private Sub Worksheet_Activate()

    If WorksheetFunction.Sum(Range("C6:Q6")) < 0 Then 'Week 1
    Rows("6").RowHeight = 0
    Else
    Rows("6").RowHeight = 20
    End If
    End Sub
    Thank you, this does indeed work

    I should add that for the exact purpose I wanted it for I changed the code to <=0, I should have typed it this way in my first example.

    Please Login or Register  to view this content.

  8. #8
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: How can I perform a conditional test based on the sum a row of cells using VBA

    You're welcome! And, thanks for the rep

+ 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] Test if a cell is within a name range and return a text value based on the test
    By DraconR in forum Excel Formulas & Functions
    Replies: 15
    Last Post: 06-24-2013, 02:46 AM
  2. Replies: 10
    Last Post: 06-12-2013, 12:12 PM
  3. Conditional Formatting of one cell based on Test of another
    By KStrong in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 04-12-2013, 01:36 PM
  4. Insert cells based on test
    By jdh in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 12-17-2007, 07:58 PM
  5. Replies: 1
    Last Post: 02-24-2005, 07:26 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