如何在Excel设置自动统计公式:
答案:2 悬赏:0 手机版
解决时间 2021-04-04 21:06
- 提问者网友:皆是孤独
- 2021-04-03 20:18
如何在Excel设置自动统计公式:
最佳答案
- 五星知识达人网友:旧脸谱
- 2021-04-03 21:49
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
endcol = [IV2].End(xlToLeft).Column
j = 1
Dim lishi(1 To 10000)
Static i As Integer
i = 4
line3:
r = 1
line1:
If Cells(2, i).Value = Cells(2, i + 1).Value Then
r = r + 1
i = i + 1
GoTo line1
Else
lishi(j) = r
j = j + 1
i = i + 1
If i = endcol + 1 Then GoTo line2
GoTo line3
End If
line2:
maxlishi = Application.WorksheetFunction.Max(lishi())
Dim lishi1(1 To 10000)
Dim lishi2(1 To 10000)
For q = 1 To j
lishi1(q) = lishi(q * 2 - 1)
lishi2(q) = lishi(q * 2)
Next
maxlishi1 = Application.WorksheetFunction.Max(lishi1())
maxlishi2 = Application.WorksheetFunction.Max(lishi2())
Debug.Print maxlishi1, maxlishi2
a = 1
For z = 4 To endcol
If Cells(2, z).Value = Cells(2, z + 1).Value Then
a = a + 1
Else
GoTo line4
End If
Next
line4:
If a = maxlishi1 Then
With Range("b2").Borders
.LineStyle = xlContinuous
.Weight = xlMedium
.ColorIndex = 5
End With
Range("b2").Font.ColorIndex = 3
Range("b2").Interior.ColorIndex = 6
Range("b2").Value = "连" & Range("d2") & maxlishi1
Else
With Range("b2").Borders
.LineStyle = xlNone
End With
Range("b2").Font.ColorIndex = 1
Range("b2").Interior.ColorIndex = xlNone
If Range("d2").Value = "错" Then
dx = "对"
Else
dx = "错"
End If
Range("b2").Value = "历史连" & Range("d2") & maxlishi1 & Chr(10) + Chr(13) & "连" & dx & maxlishi2
End If
End Sub
公式是没办法完成这么复杂的计算和判断的,只能用VBA了,我做了一下,用上面这段代码。。。经过我反复测试是完全正确的。而且全部自动的。根据你的那张图来写的这段代码。所以直接复制,不用更改。
我不知道你这张表有多少数据,所以设数组大小的时候设的是10000,如果你有需要可以直接改代码。如果最近的数据是连对或连错达到历史最大值时将会蓝框黄底红字显示。如果最近的数据没有达到历史最大值时将会黑字显示历史的连错连对记录。如果要更改显示你也可以直接改代码。。。
给你讲讲怎么用这个代码。打开excel,按alt+F11打开VBE,按ctrl+r打开工程资源管理器。然后双击管理器中的sheet1(你的数据在sheet1就点sheet1,在sheet2就点sheet2)。然后把我上面的代码全部复制到右边的代码窗口,像我这个图这样,然后关闭,你在excel中就能看到效果了,如果还有问题联系我!
你复制的时候注意把代码复制完,不然会出错。。。反正我测试过很多次,结果绝对没问题。。
endcol = [IV2].End(xlToLeft).Column
j = 1
Dim lishi(1 To 10000)
Static i As Integer
i = 4
line3:
r = 1
line1:
If Cells(2, i).Value = Cells(2, i + 1).Value Then
r = r + 1
i = i + 1
GoTo line1
Else
lishi(j) = r
j = j + 1
i = i + 1
If i = endcol + 1 Then GoTo line2
GoTo line3
End If
line2:
maxlishi = Application.WorksheetFunction.Max(lishi())
Dim lishi1(1 To 10000)
Dim lishi2(1 To 10000)
For q = 1 To j
lishi1(q) = lishi(q * 2 - 1)
lishi2(q) = lishi(q * 2)
Next
maxlishi1 = Application.WorksheetFunction.Max(lishi1())
maxlishi2 = Application.WorksheetFunction.Max(lishi2())
Debug.Print maxlishi1, maxlishi2
a = 1
For z = 4 To endcol
If Cells(2, z).Value = Cells(2, z + 1).Value Then
a = a + 1
Else
GoTo line4
End If
Next
line4:
If a = maxlishi1 Then
With Range("b2").Borders
.LineStyle = xlContinuous
.Weight = xlMedium
.ColorIndex = 5
End With
Range("b2").Font.ColorIndex = 3
Range("b2").Interior.ColorIndex = 6
Range("b2").Value = "连" & Range("d2") & maxlishi1
Else
With Range("b2").Borders
.LineStyle = xlNone
End With
Range("b2").Font.ColorIndex = 1
Range("b2").Interior.ColorIndex = xlNone
If Range("d2").Value = "错" Then
dx = "对"
Else
dx = "错"
End If
Range("b2").Value = "历史连" & Range("d2") & maxlishi1 & Chr(10) + Chr(13) & "连" & dx & maxlishi2
End If
End Sub
公式是没办法完成这么复杂的计算和判断的,只能用VBA了,我做了一下,用上面这段代码。。。经过我反复测试是完全正确的。而且全部自动的。根据你的那张图来写的这段代码。所以直接复制,不用更改。
我不知道你这张表有多少数据,所以设数组大小的时候设的是10000,如果你有需要可以直接改代码。如果最近的数据是连对或连错达到历史最大值时将会蓝框黄底红字显示。如果最近的数据没有达到历史最大值时将会黑字显示历史的连错连对记录。如果要更改显示你也可以直接改代码。。。
给你讲讲怎么用这个代码。打开excel,按alt+F11打开VBE,按ctrl+r打开工程资源管理器。然后双击管理器中的sheet1(你的数据在sheet1就点sheet1,在sheet2就点sheet2)。然后把我上面的代码全部复制到右边的代码窗口,像我这个图这样,然后关闭,你在excel中就能看到效果了,如果还有问题联系我!
你复制的时候注意把代码复制完,不然会出错。。。反正我测试过很多次,结果绝对没问题。。
全部回答
- 1楼网友:一袍清酒付
- 2021-04-03 23:15
自动筛选数据并统计平均值,就是为了抛开隐藏数值,但是要单独用公式是无法忽略隐藏数值,所以我们采用Excel中列表功能解决这类问题。
参考:office2013
选中数值范围
点击功能区“插入”,选择“表格“。(快捷键:Ctrl+T)
点击“表格样式选项”,选择”汇总行“
根据自己的需要筛选序列,最后在”汇总行“选择”平均值“
点击放大查看
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯