View Single Post
  #3   Report Post  
Posted to microsoft.public.excel.worksheet.functions
Toppers
 
Posts: n/a
Default Select a range of non-adjacent cells in Excel?

.... typo ....


=Sum_FS(A1:F1,"F")

"Toppers" wrote:



Try this UDF:

=sum_FS("A1:F1","F")

HTH

Function Sum_FS(ByRef rng As Range, ByVal Comparator As String) As Double
ncol = rng.Count
sumf = 0
For col = 2 To ncol
If rng(1, col - 1) = Comparator Then
sumf = sumf + rng(1, col)
End If
Next col
Sum_Fs = sumf
End Function

"hodgsonk" wrote:

I have a series of data values in non-adjacent columns in an excel spreadsheet.
In the following example, assume the | (vertical bar) refers to the start of
a new cell:

F|45|A|30|F|15|F|10

Using the SUMIF formula, I want to add all numbers which are preceded by a
cell containing the letter F.

SUMIF(A1:G1,"F",B1:H1)

What I need to do is specify a range of every second cell in the row
(starting with Cell A1) for validating they equal F, and a range of the
alternating cells (starting in column B1) for the range containing the data
to add. How can I specify these ranges (I can't name each cell individually
as I have more than 30 cells to add up in my real life situation and the IF
function allows selection of no more than 30 values)?