Quantcast
Channel: Excel Help Forum - Excel Programming / VBA / Macros
Viewing all articles
Browse latest Browse all 50038

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

$
0
0
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:

Code:

If Range("A1").Value = "Some Criteria" Then
 Columns("A").ColumnWidth = 0
 Else
 Columns("Q").ColumnWidth = 20
 End If

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:

Code:

If Sum("A1:E1") < 0 Then
 Rows("1").RowHeight = 0
 Else
 Rows("1").RowHeight = 20
 End If

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.

Viewing all articles
Browse latest Browse all 50038

Trending Articles