在工作表中查找值并将结果粘贴到另一个工作表中

时间:2017-06-29 13:02:17

标签: excel vba excel-vba

我有两张Sheets sheet1和sheet2

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

两列中1的计数值应粘贴在sheet2中,查看AX列中sheet1的周。如果列AX的周数为24,那么它应该沿着sheet2,A列运行24,并在B列中粘贴T的Count值,在C列中粘贴U的Count值,并计算两者的百分比,并在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.

1 个答案:

答案 0 :(得分:2)

从问题中可以看出,A栏中所示给定周的表格“结果”显示在B栏和B栏中。列T&中的C计数为1。分别是其他表格的U.

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

这会给出一些想法:

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行开始