简体   繁体   English

VBA如何按y列中的日期计算x列中值的实例?

[英]VBA How to count instances of a value in column x by date in column y?

I have a question about how to achieve my goal. 我对如何实现自己的目标有疑问。 I am trying to get data in a format that I can use with Minitab via Excel VBA macro. 我试图通过Excel VBA宏获取可以与Minitab一起使用的格式的数据。 Basically, I have test data from a machine, and that collects various attribute and variable data. 基本上,我从一台机器上获得测试数据,并收集各种属性和变量数据。 I am interested in finding the number of items that meet several criteria, and counting that number. 我有兴趣查找满足几个条件的项目数量,并计算该数量。 So, more specifically, in my 1stTimeYield column, I want to count each serial number that has a '1' entered (failures start with 'X'), for each date that is shown. 因此,更具体地说,在我的1stTimeYield列中,我希望为显示的每个日期计算每个输入了“ 1”的序列号(失败以“ X”开头)。 Looking at the screenshot below, for 01-Feb-15, I should get a total number of 3 for my count. 看下面的屏幕截图,对于15年2月1日,我的总数应该为3。 I also need to count the total number of serial numbers for this date, which in this case is 4. My goal is to get to the first pass yield, of 75% for the 5 data collections on Feb 1. I then need to enter this data into the respective rows/columns on the worksheet that I create in my code (I will paste the code below this text). 我还需要计算该日期的序列号总数,在这种情况下为4。我的目标是对于2月1日的5个数据收集达到75%的首过合格率。然后,我需要输入将此数据放入我在代码中创建的工作表上的相应行/列中(我会将代码粘贴到此文本下方)。 The pasting of the counts I come up with should be relatively straight forward, after finding a way to compare the date I collected the counts for with the dates listed in the sheet that I make. 在找到一种将我收集的计数日期与我的工作表中列出的日期进行比较的方法之后,我想出的计数的粘贴应该相对简单一些。 Please ask clarification questions if you have them. 如果您有任何疑问,请询问。 Essentially, I am a little stumped as to how to efficiently and effectively get the data that I need by date. 从本质上讲,我对如何有效地按日期获取所需的数据有些困惑。 FPY数据样本

Sub Main()

    Dim iNumSheets As Long
    'Add new Worksheet; check if exists already
    Sheets.Add After:=Sheets(Sheets.Count)
    iNumSheets = Sheets.Count
    If SheetExist("FPY Data") Then
        Application.DisplayAlerts = False
        Sheets(iNumSheets).Delete
        Application.DisplayAlerts = True
        End
    Else
        Sheets(iNumSheets).Name = "FPY Data"
    End If

    'Create and Format the Date and Yield Comments
    Dim sDate As String
    Dim sYield As String
    Sheets("FPY Data").Select
    Cells(1, 1).Value = "Date"
    Cells(1, 2).Value = "First Pass"
    Cells(1, 3).Value = "Total Pass"
    Cells(1, 4).Value = "FPY (%)"
    Columns("A:A").ColumnWidth = 10.33
    Columns("B:B").ColumnWidth = 10.33
    Cells.Select
    With Selection
        .HorizontalAlignment = xlGeneral
        .VerticalAlignment = xlCenter
        .WrapText = False
        .Orientation = 0
        .AddIndent = False
        .IndentLevel = 0
        .ShrinkToFit = False
        .ReadingOrder = xlContext
        .MergeCells = False
    End With
    With Selection
        .HorizontalAlignment = xlCenter
        .VerticalAlignment = xlCenter
        .WrapText = False
        .Orientation = 0
        .AddIndent = False
        .IndentLevel = 0
        .ShrinkToFit = False
        .ReadingOrder = xlContext
        .MergeCells = False
    End With
    Cells(1, 1).Select

    'Find Dates and copy to Yield Worksheet
    Dim iRow As Long
    Dim wSheet As Worksheet
    Set wSheet = ThisWorkbook.Worksheets(1)
    iRow = wSheet.Cells.Find("*", searchorder:=xlByRows, searchdirection:=xlPrevious).Row

    'Copy first date in data
    Dim iTemp As Long
    iTemp = 3
    Worksheets("FPY Data").Cells(2, 1).Value = Worksheets(1).Cells(2, 2).Value
    For iCounter = 3 To iRow        'Loop through data for dates

        If Worksheets(1).Cells(iCounter, 2).Value = Worksheets(1).Cells(iCounter - 1, 2).Value Then
            'Do not copy new date to FPY data
        Else
            'Copy Date to next available cell & increment counter
            Worksheets("FPY Data").Cells(iTemp, 1).Value = Worksheets(1).Cells(iCounter, 2).Value
            iTemp = iTemp + 1
        End If

    Next iCounter

    'Count number of First Time Passes


End Sub

Function SheetExist(strSheetName As String) As Boolean
    Dim i As Integer

    For i = 1 To Worksheets.Count
        If Worksheets(i).Name = strSheetName Then
            SheetExist = True
            Exit Function
        End If
    Next i
End Function

I understand that you have been working on a VBA solution but if formulas will do, some efficient non-array standard formulas can be applied to achieve your results¹. 我了解您一直在研究VBA解决方案,但如果可以使用公式,则可以应用一些有效的非数组标准公式来获得结果¹。

在此处输入图片说明

The first thing you need are a list of unique dates. 您需要的第一件事是唯一日期列表。 Excel treats dates as numbers so you can retrieve a ranked unique list starting with this formula in F2, Excel将日期视为数字,因此您可以从F2中的此公式开始检索排名的唯一列表,

=SMALL($B:$B, COUNTIF($B:$B, "<="&$F1)+1)

Next is a COUNTIFS function that takes all of your conditrions into consideration in G2. 接下来是一个COUNTIFS函数 ,它将G2中的所有条件考虑在内。

=COUNTIFS($B:$B, $F2,$D:$D, 1,$C:$C, "<>X*")

Your heavily redacted data left the question of the serial number column containing duplicates unanswered so I assumed that there may be duplicates. 您的大量编辑数据使序列号列中的问题无法解决,因此我认为可能存在重复。 H2's formula is, H2的公式是

=SUMPRODUCT(($B$2:$B$9999=$F2)/COUNTIF($C$2:$C$9999, $C$2:$C$9999&""))

Your narrative was unclear on whether that count should contain the serial numbers starting with X so here is the same count with X's excluded in I2, 您的叙述不清楚该计数是否应包含以X开头的序列号,因此这里的计数与I2中排除的X相同,

=SUMPRODUCT((($B$2:$B$9999=$F2)*(LEFT($C$2:$C$9999,1)<>"X"))/COUNTIF($C$2:$C$9999,$C$2:$C$9999&""))

Fill down as necessary. 根据需要填写。 When you run out of dates to pull into column F you will get a #REF! 当您用完日期进入F列时,您将获得#REF! error. 错误。 Stop filling down there. 别在那儿加油了。

¹ fwiw, if I did this in VBA, I'd be using Application.WorksheetFunction to mash up several of these functions into the VBA code both for code efficiency and expediency. ¹ 再者,如果我在VBA中这样做,我将使用Application.WorksheetFunction将其中几个函数混入VBA代码中,以提高代码效率和便利性。

'Find Last Row of FPY Data Worksheet
Set wSheet = ThisWorkbook.Worksheets("FPY Data")

'Fill First Pass Column
Cells(2, 2).Activate
ActiveCell.FormulaR1C1 = "=COUNTIFS(PartData!C,'FPY Data'!RC[-1],PartData!C[20],1,PartData!C[1],""<>X*"")"
iRow = wSheet.Cells.Find("*", searchorder:=xlByRows, searchdirection:=xlPrevious).Row
ActiveCell.Select
Selection.AutoFill Destination:=ActiveCell.Range("A1:A" & iRow - 1)

'Fill Total Pass Column
Cells(2, 3).Activate
ActiveCell.FormulaR1C1 = "=COUNTIFS(PartData!C[-1],'FPY Data'!C[-2],PartData!C,""<>X*"")"
ActiveCell.Select
Selection.AutoFill Destination:=ActiveCell.Range("A1:A" & iRow - 1)

'Fill Percentage field
Cells(2, 4).Activate
ActiveCell.FormulaR1C1 = "=RC[-2]/RC[-1]*100"
ActiveCell.Select
Selection.AutoFill Destination:=ActiveCell.Range("A1:A" & iRow - 1)
Range(Cells(2, 4), Cells(iRow, 4)).Select
Selection.NumberFormat = "0.00"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM