当多个单元格都为真时,如何填充单元格

时间:2011-05-12 19:02:22

标签: excel vba excel-2010

我有一个带有Zeros(0 = No)和Ones(1 = Yes)的Excel文件,在L至V列中。根据他们在这些列中的答案,我想在Z列中放入一些文本。例如:

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

如果所有这些单元格都为0,则将“初学者”放在Z

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

将成为“学习者” - 一路走来:

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

将是“专家”。

我尝试了IF-THEN-ELSE和SELECT CASE功能的不同变体,但我似乎无法正常工作。我知道我将不得不循环遍历行(大约有500行)但是如何告诉Excel我想要什么?

忘记提及,数据也可能看起来像这样

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

我有大约50种不同的场景出现。

4 个答案:

答案 0 :(得分:1)

您可以将答案连接成一个字符串,然后根据它选择输出。

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

编辑: 根据规则的复杂程度,最好使用if-then-elseif-else,因为这样你就可以使用Like运算符,它允许你使用通配符。

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

答案 1 :(得分:0)

使用SUM汇总所有行。然后根据总和适合的范围发布您的答案。

答案 2 :(得分:0)

使用CHOOSE上的SUM功能,该功能将返回1-11之间的数字,该数字将从您的名字列表中选择。为您的类别填写以下数字:

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

然后将公式复制到所有行。

答案 3 :(得分:0)

如果1的总和是重要的,那么你可以这样做:

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

如果特定位置的1s很重要,那么此代码将起作用:

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