vba - 在一个工作表中查找一个值,对其进行计数并将结果粘贴到另一个工作表中

标签 vba excel

我有两张表sheet1和sheet2

在sheet1的AX列中,我使用公式打印了我当前的一周。我正在寻找sheet1、T 和U 列,并计算两列中1 的数量。

两列中 1 的计数应粘贴到sheet2 中,并在AX 列中查看sheet1 的周。如果列 AX 的周数为 24,那么它应该沿着工作表 2、A 列运行 24,并将 T 的计数值粘贴到 B 列中,将 U 的计数值粘贴到 C 列中,并计算两者的百分比并粘贴到 C 中和 D。

我尝试了一段代码,我经常得到它为0,我很惊讶我错了。既不检查计数,也不检查周数。

Sub test()
Dim col As Range
Dim row As Range
Dim sh1 As Worksheet
Dim sh2 As Worksheet
Dim T As Integer
Dim U As Integer
Dim wk As String

Set sh1 = Sheets("BW")
Set sh2 = Sheets("Results")



For Each col In sh2.Columns 'This loops through all populated columns in row one
    If sh2.Cells(1, col.Column).Value = "" Then
        Exit For
    End If

    wk = sh2.Cells(1, col.Column).Value

    For Each rw In sh1.Rows
        If sh1.Cells(rw.row, 50).Value = "" Then
            Exit For
        End If

        If sh1.Cells(rw.row, 50) = wk And sh1.Cells(rw.row, 20) = 1 Then
            T = T + 1
        End If

        If sh1.Cells(rw.row, 50) = wk And sh1.Cells(rw.row, 21) = 0 Then
            U = U + 1
        End If
    Next rw

sh2.Cells(2, col.Column) = T 'put counters into 2nd and 3rd row under each week, you can adjust this to put the number in a different cell.
sh2.Cells(3, col.Column) = U

T = 0 'reset counters to start looking at next week.
U = 0

Next col

End Sub

Resembels sheet1, where i have my data

Resembels sheet2, where I have the column A already filled, Need to fill column B, C , D and E. B and C are the Count values from sheet1 column T and U, D and E are the percentage values from column b and c of sheet2. Clarification img: for eg, this is how the sheet2 Looks. I already made an evalaluation last before week.now i am running a formula in sheet1, which gives current week in AX. SO, the sheet2 runs through column a, identifies the same week as sheet1 and add the Count value only in the current week, it does not Change the value of earlier week or next week.currently the code Returns 0, in all the weeks and Returns the Count value for current week. i want it to return only for current week.

最佳答案

从问题中可以看出,A 列中指示的给定周的工作表“结果”在 B 列和 C 列中分别显示另一工作表的 T 列和 U 列中 1 的计数。

解决此问题的一种方法是,对于“结果”工作表中的每一行,计数器会查看“AX”列中指示的该周“BW”工作表的所有行,以从 T 和 U 列中获取计数。

这会给出一些想法:

Sub test()
    Dim i As Integer, j As Integer, cntT As Integer, cntU As Integer, ws As Worksheet
    Set ws = Sheets("Results")
    Sheets("BW").Select
    For i = 2 To WorksheetFunction.CountA(ws.Columns(1))
        If ws.Range("A" & i) = Val(Format(Now, "ww")) Then Exit For
    Next i
    ws.Range("B" & i & ":" & "E" & i).ClearContents
    cntT = 0
    cntU = 0
    For j = 5 To WorksheetFunction.CountA(Columns(50))
        If ws.Range("A" & i) = Range("AX" & j) And Range("AA" & j) <> "" Then
            If Range("T" & j) = 1 Then cntT = cntT + 1
            If Range("U" & j) = 1 Then cntU = cntU + 1
        End If
    Next j
    If cntT <> 0 Then ws.Range("B" & i) = cntT
    If cntU <> 0 Then ws.Range("C" & i) = cntU
    If cntT + cntU <> 0 Then
        ws.Range("D" & i) = cntT / (cntT + cntU)
        ws.Range("E" & i) = cntU / (cntT + cntU)
    End If
    ws.Range("D" & i & ":E" & i).NumberFormat = "0%"
End Sub

更新:根据随后与 @Mikz 的讨论,以下标准添加到了上述更新的代码中:

  1. 它只会覆盖当前周,因此对于 2017 年 7 月 2 日,如果在“结果”表的“A 列”中找到,它只会覆盖当前的 27 周
  2. 如果输出为0,则目标单元格留空
  3. 如果“BW”表的AA列的相应单元格为空,则该行不计入1秒
  4. 工作表“BW”数据从第 5 行开始

关于vba - 在一个工作表中查找一个值,对其进行计数并将结果粘贴到另一个工作表中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44825918/

相关文章:

vba - VBA (Excel) 中的范围减法

excel - 有没有办法在单个单元格中使用下划线、粗体和斜体?

.net - Office 功能区 XML 与 Office 标准功能区设计器?

ruby - 使用 Ruby 解析 XLS 和 XLSX (MS Excel) 文件?

excel - 仅复制和粘贴可见单元格

excel - 如何在 Excel 加载项中保存用户首选项?

excel - 尝试将变量用作值 vba 到 sql

Java 创建 csv 文件,有 "和 ; 在其中,在 Excel 中正确打开

vba - VB 与 VBA 有什么区别?

python - 需要从列表中删除重复项。 Set() 函数不起作用。也不是 for 循环方法