Question

I have an Excel file with Zeros (0 = No) and Ones (1 = Yes) in columns L thru V. Depending on what their answers are in those columns, I want to put some text in column Z. For instance:

0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 

If all of those cells are 0, put "Beginner" in Z

1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0

would be "Learner" -- all the way through:

1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1

would be "Expert".

I tried different variations of IF-THEN-ELSE and the SELECT CASE function but I can't seem to get anything to work right. I know that I'm going to have to loop through the rows (there are about 500) but how do I tell Excel what I want?

Forgot to mention, data could also look like this

0 | 0 | 0 | 0 | 0 | 0 | 0 | 1 | 1 | 1 | 1

I've got about 50 different scenarios that show up.

Was it helpful?

Solution

You could concatenate the answers into a string and choose your output based on that.

For r = 1 To 500
Answers = ""

For c = 12 To 22
    Answers = Answers & Cells(r, c)
Next c

Select Case Answers
    Case "00000000000"
        Cells(r, 26) = "Beginner"
    Case "10000000000"
        Cells(r, 26) = "Learner"
    Case "11110000000"
        Cells(r, 26) = "Sophomore"
    Case "11111111111"
        Cells(r, 26) = "Expert"
    Case Else
        Cells(r, 26) = "<something else>"
End Select

Next r

Edit: Depending on how complex your rules are, you may be better off using if-then-elseif-else, because then you can use the Like operator, which lets you use wildcards.

If Answers = "00000000000" Then
    output = "Beginner"
ElseIf Answers = "11111111111" Then
    output = "Expert"
ElseIf Answers Like "1111*" Then
    output = "Sophomore"
Else
    output = "other"
End If

OTHER TIPS

Use SUM to sum up all the rows. Then post your answer based on the range the sum fits in.

Use the CHOOSE function on the SUM, which will return a number between 1-11 that will pick from your list of names. Fill in the numbers below for your categories:

=CHOOSE(SUM(L2:V2), "Beginner", "Learner", "3", "4", "5", "6", "7", "8", "9", "10", "Expert")

Then copy the formula down through all of the rows.

If the total sum of 1s is what matters then you could just do:

For Each rw in Sheets("Sheet1").UsedRange.Rows
  Select Case Application.Sum(rw.Columns("L:V"))
    Case 0
      rs.Cells(1,26) = "Beginner"
    Case 1
      rs.Cells(1,26) = "Learner"
    Case 11
      rs.Cells(1,26) = "Expert"
  End Select
Next rw

If 1s in specific places matter, then this code will work:

Sub FillZ()

Dim wRange As Range
Dim resultArray As Variant
Dim i As Integer

Set wRange = Sheets("Sheet1").Range("A1:Z" & Range("A65536").End(xlUp).Row)

resultArray = SpecialConcatenate(workingRange)

For i = 1 To wRange.Rows.Count
  Select Case resultArray(i)
    Case "00000000000"
      wRange(i, 26) = "Beginner"
    Case "10000000000"
      wRange(i, 26) = "Learner"
    Case "11111111111"
      wRange(i, 26) = "Expert"
  End Select
Next i

End Sub

Function SpecialConcatenate(wRange As Range) As Variant

Dim j As Integer, k As Integer
Dim resultArray() As Variant

ReDim resultArray(1 To wRange.Rows.Count)

For j = 1 To wRange.Rows.Count
  resultArray(j) = ""
  For k = 1 To 11
    resultArray(j) = resultArray(j) & wRange.Cells(j, k + 11).Value
  Next k
Next j
SpecialConcatenate = resultArray

End Function
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top