这是我的草稿数据
工作表名称: “第一”
数据表示 X 和 Y 字段的通过和失败。如果区域在 X 上失败,则将其标记为 F ,如果通过,则该字段 X 将标记为 P 。与字段 Y
相同的过程还有
Sheet_name: “ SECOND”
以下是工作表的摘要:“第一” 它计算通过和失败的次数。 使用Countif函数的想法。
= COUNTIF(FIRST!B2:B5,“ P”)
= COUNTIF(FIRST!C2:C5,“ F”)
我想做的是
当您尝试单击通过次数和失败次数时。它将重定向到新的工作表,在该工作表中向工作表提供通过和失败区域的数据。
示例: 如果我单击“通过”字段下的“ 3” 它将给我这样的东西,
| X |
Area1 | p |
Area2 | p |
Area4 | p |
对不起,这不是我的项目,作业或考试。 我只需要了解单击单元格时打开数据的逻辑。
答案 0 :(得分:0)
Option Explicit
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Const cStrPass As String = "A3" ' Pass Cell Range
Const cStrFail As String = "B3" ' Fail Cell Range
If Target = Range(cStrPass) Then
CellClick Range("A3")
End If
If Target = Range(cStrFail) Then
CellClick Range("B3")
End If
End Sub
Sub CellClick(CellRange As Range)
Const cVntName1 As Variant = "FIRST"
Const cVntName3 As Variant = "THIRD"
Dim vntSrc As Variant ' Source Array
Dim vntTgt As Variant ' Target Array
Dim lngLastRow As Long ' Source Last Row
Dim i As Long ' Source Row Counter
Dim k As Long ' Target Row Counter
Dim j As Integer ' Source/Target Column Counter
Dim strPF As String ' PassFail String
' Paste Source Range into Source Array.
With Worksheets(cVntName1)
lngLastRow = .Cells(.Rows.Count, "B").End(xlUp).Row
vntSrc = .Range("A2", .Cells(lngLastRow, "B"))
End With
' Determine PassFail String.
If CellRange.Column = 1 Then
strPF = "P"
Else
strPF = "F"
End If
' Count rows for Target Array
For i = 1 To UBound(vntSrc)
If vntSrc(i, 2) = strPF Then
k = k + 1
End If
Next
' Write data to Target Array
ReDim vntTgt(1 To k, 1 To 2)
k = 0
For i = 1 To UBound(vntSrc)
If vntSrc(i, 2) = strPF Then
k = k + 1
For j = 1 To UBound(vntSrc, 2)
vntTgt(k, j) = vntSrc(i, j)
Next
End If
Next
' Paste Target Array into Target Range.
With Worksheets(cVntName3)
.Range("A2", "B" & .Rows.Count).ClearContents
.Range("A2").Resize(UBound(vntTgt), UBound(vntTgt, 2)) = vntTgt
.Select
End With
End Sub