运行时错误424对象需要VBA

时间:2017-09-13 13:10:58

标签: vba excel-vba excel

我有这段代码使用四个给定案例中的每一个的公式来计算收益率。但是,我收到了一个对象错误并且一直试图解决它但是徒劳无功。

最初我在代码开头有'Options Explicit',但我把它取下来,我尝试改变yieldProbe的类型,但它没有帮助。我对VBA比较陌生,任何帮助都会受到赞赏。

Sub getYield()

Dim siteProbe, binProbe As Variant
Dim yieldProbe As Variant
Dim SelectFolder As String
Dim MyObj As Object, MySource As Object, file As Variant
Dim file_path, list_file, final_message As String
Dim csvFiles As Variant
Dim probe As Workbook
Dim probeSh As Worksheet
Dim lastRowProbe As Long

SelectFolder = GetFolder("c:\")
Application.ScreenUpdating = False
 'Check user did not cancel folder selection
If SelectFolder = "" Then
    MsgBox "No Folder Selected - Cannot continue", vbCritical
    End
End If

SelectFolder = SelectFolder & "\"
csvFiles = Dir(SelectFolder & "*.csv")
Do While csvFiles <> ""



    Set probe = Workbooks.Open(SelectFolder & csvFiles) 'will auto open
    Set probeSh = probe.ActiveSheet 'name of sheet ex probe sheet in probe workbook
    lastRowProbe = probeSh.Range("D10000").End(xlUp).Row 'gets last row of Probe Sheet
    Columns("Z:Z").Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
    Range("Z1").Value = "Yield"

    For i_probe = lastRowProbe To 7 Step -1 'to loop through all Probe rows to obtain the HBin and Site Values
    siteProbe = probeSh.Cells(i_probe, 6).Value 'set site value of current wafer of probe sheet
    binProbe = probeSh.Cells(i_probe, 3) 'set bin value of current wafer of probe sheet
    yieldProbe = probeSh.Cells(i_probe, 26) 'set the yield value for the Z column


     Next i_probe

    Select Case siteProbe



    Case "1"

    yieldProbe = ((Application.WorksheetFunction.CountIf(binProbe, "1")) / Application.WorksheetFunction.Count(binProbe)) * 100

    Case "2"

   yieldProbe = ((Application.WorksheetFunction.CountIf(binProbe, "1")) / Application.WorksheetFunction.Count(binProbe)) * 100

    Case "3"

    ' error 424 here:
    yieldProbe = ((Application.WorksheetFunction.CountIf(binProbe, "1")) / Application.WorksheetFunction.Count(binProbe)) * 100

    Case "4"

    yieldProbe = ((Application.WorksheetFunction.CountIf(binProbe, "1")) / Application.WorksheetFunction.Count(binProbe)) * 100

    Case "5"

    yieldProbe = ((Application.WorksheetFunction.CountIf(binProbe, "1")) / Application.WorksheetFunction.Count(binProbe)) * 100

    End Select






    x = x + 1
    probe.Close True
    csvFiles = Dir

Loop

Application.ScreenUpdating = True
MsgBox "A total of " & CStr(x) & " files processed", vbInformation
On Error GoTo 0
Exit Sub



End Sub

Function GetFolder(strPath As String) As String
Dim fldr As FileDialog
Dim sItem As String


Set fldr = Application.FileDialog(msoFileDialogFolderPicker)
With fldr
    .Title = "Select probe files"
    .AllowMultiSelect = False
    .InitialFileName = strPath
    If .Show <> -1 Then GoTo NextCode
    sItem = .SelectedItems(1)
End With
NextCode:
GetFolder = sItem
Set fldr = Nothing
End Function

1 个答案:

答案 0 :(得分:1)

binProbe = probeSh.Cells(i_probe, 3) 'set bin value of current wafer of probe sheet

忽略这个(以及siteProbeyieldProbe)在循环的每次新迭代中被覆盖的事实,我怀疑这些{{1} } block可以按你的意愿工作。

Case被声明为binProbe,但按其分配的方式,其内容将与该单元格的无关 - 该& #39;字符串,日期,双重,......或错误。

此处有一个Minimal, Complete, and Verifiable example,可以重现确切的错误消息并向您发出以下信息:

Variant

这是什么意思?这意味着如果您在中断/调试模式下探测 Public Sub BlowUp() Debug.Print Application.WorksheetFunction.CountIf(CVErr(xlErrValue), "1") End Sub 的值,您会发现它包含一个Excel单元格错误值,该值不能与之一起使用数学:您正在使用binProbe#N/A#VALUE!或其他任何方式为您的工作表功能提供信息 - 它不知道如何处理它,并抛出&#34 ;对象必需&#34;在你身边,因为它期望得到#REF!(或数组),而不是错误值。

要为其指定Range,请在分配时声明RangebinProbe As Range的值:

Set

然后在将其值用于任何事情之前,请验证它不是单元格错误:

Set binProbe = probeSh.Cells(i_probe, 3) 'set bin value of current wafer of probe sheet