当数组个数小于3个时,输出数组内所有值?
如果C>90,则存入数组arr中;
1、arr只有3个值或更少时,输出文字“姓名1成绩为XX,姓名2成绩为XX,姓名3成绩为XX"
(XX为对应学生的成绩)
2、如果arr有3个值以上,则输出文字:X个学生的成绩在XX-XX之间;(X为arr数组内数字个数,XX为成绩)
求VBA代码 展开
第一种情况:
第二种情况:
代码文本:
Function 数组(a As Range)
Dim arr(1 To 100), brr(1 To 100)
For i = 2 To a.Cells(65536, 1).End(3).Row
If a.Cells(i, 2) > 90 Then
n = n + 1
arr(n) = a.Cells(i, 1)
brr(n) = a.Cells(i, 2)
End If
Next
If n < 4 Then
For j = 1 To n
数组 = 数组 & "," & arr(j) & "成绩为" & brr(j)
Next
数组 = Mid(数组, 2)
Else
数组 = n & "个学生的成绩在" & Application.Min(brr) & "到" & Application.Max(brr) & "之间"
End If
End Function
如果有其他科目成绩新增,如成绩分成了语文,数学,等科目;
需要改动哪些呢,能不能麻烦举个例子?
这个具体得看你的排版方式,在哪个位置,是否固定,需不需要代码判断。
VBA我不玩的,但是你描述的情况应该可以用数组公式实现的:
=IF(COUNTIF(C:C,">90")<4,TEXTJOIN(",",TRUE,IF(C:C>90,A:A&B:B&C:C,"")),COUNTIF(C:C,">90")&"个学生的成绩在90以上")
或者是:
=IF(COUNTIFS(C:C,">90",C:C,"<=100")<4,TEXTJOIN(",",TRUE,IF((C:C>90)*(C:C<=100),A:A&B:B&C:C,"")),COUNTIFS(C:C,">90",C:C,"<=100")&"个学生的成绩在90-100之间")
是数组公式!!!必须CTRL+SHIFT+ENTER这3个键同按结束,否则公式会无效哦!!
Dim arr(), i&, r&, n As Byte, j As Byte, X%, Xmin As Byte, Xmax As Byte, a$
r = Cells(Rows.Count, 1).End(3).Row
Xmin = 90 '设置成绩下限
Xmax = 120 '设置满成绩
X = Application.CountIf(Range("c2:c" & r), ">" & Xmin)
If X > 3 Then
a = X & "个学生成绩在" & Xmin & "-" & Xmax & "之间"
ElseIf X = 0 Then
a = "没有学生的成绩大于" & Xmin
Else
ReDim arr(1 To X, 1 To 2)
For i = 2 To r
If Cells(i, 3) > Xmin Then
n = n + 1
arr(n, 1) = Cells(i, 2)
arr(n, 2) = Cells(i, 3)
End If
Next
For j = 1 To n
For i = 1 To n
If arr(i, 2) = Application.Max(arr) Then
a = a & arr(i, 1) & "成绩为" & arr(i, 2) & " "
arr(i, 2) = ""
End If
Next
Next
End If
a = Application.Substitute(Application.Trim(a), " ", ",")
MsgBox a
[e2] = a
End Sub