【发布时间】:2015-07-24 23:07:33
【问题描述】:
我有一个关于如何实现目标的问题。我正在尝试以我可以通过 Excel VBA 宏与 Minitab 一起使用的格式获取数据。基本上,我有来自机器的测试数据,它收集各种属性和变量数据。我有兴趣找到满足多个标准的项目数量,并计算该数量。因此,更具体地说,在我的 1stTimeYield 列中,我想为显示的每个日期计算每个输入了“1”的序列号(失败以“X”开头)。查看下面的屏幕截图,对于 2015 年 2 月 1 日,我的总数应该是 3。我还需要计算该日期的序列号总数,在本例中为 4。我的目标是在 2 月 1 日的 5 次数据收集中达到 75% 的第一次通过率。 然后,我需要将此数据输入到我在代码中创建的工作表上的相应行/列中(我将在此文本下方粘贴代码)。在找到一种方法将我收集计数的日期与我制作的表格中列出的日期进行比较之后,我提出的计数的粘贴应该相对简单。如果您有这些问题,请提出澄清问题。 从本质上讲,我对如何有效地获取我需要的数据感到有点困惑。
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
【问题讨论】:
-
你不能使用
COUNTIFS()内置函数来获取计数吗?