我正在尝试使用数据验证来验证我的数据

时间:2017-01-11 11:24:49

标签: excel vba excel-vba excel-2007

我有当前股票表中的数据列表,我想检查用户输入的值是否与列表中的值匹配。假设当前的股票表有16.001,15.002,15.0089等,我想检查用户是否输入与当前股票中列出的值之一匹配的值。我在两张纸上的范围都是动态的。我得到运行时错误424。

X = load_data()
labels = X['label'].values
X.drop(['label'], axis=1, inplace=True)

final_X = X.values
#scaling makes not difference for RF but left it here since I tried
# a Voting Classifier as well afterwards
scaler = StandardScaler()
X_scl = scaler.fit_transform(final_X)
X_train, X_test, y_train, y_test = train_test_split(X_scl, labels,     
test_size=0.2, random_state=46)

clf = RandomForestClassifier(n_estimators=100, n_jobs=-1)

clf.fit(X_train, y_train)
print ("Classifier:", clf)
print("Detailed classification report:")
print()
print("The model is trained on the full development set.")
print("The scores are computed on the full evaluation set.")
print()
y_true, y_pred = y_test, clf.predict(X_test)
print(classification_report(y_true, y_pred))

1 个答案:

答案 0 :(得分:0)

这不是答案,仅仅作为评论太长了。

阅读下面稍加修改的代码,并查看我的评论(特别是?的评论,其中指出了您的错误)。

注意:作为良好的编码习惯,请始终在代码的开头使用Option Explicit,当您尝试编译VBAProject 时,您将收到错误,并且光标将指向所有没有正确定义变量的行。

<强>温度。修改后的代码

Option Explicit 

' Public variables definition section
Public Platecode As Integrer
Public code as Boolean ' <-- not the greateset variable name
Public ws As Worksheet     

'==============================================================

Sub consumption()

Dim counter As Integer
Dim i As Integer
Dim x As Integer
'Dim ws As Worksheet ' <-- remove from here, define as Public
Dim wc As Worksheet
Dim rangs As Range, rangc As Range

Set ws = ThisWorkbook.Worksheets("current stock")
Set wc = ThisWorkbook.Worksheets("consumption")

Call getCodeFromUser 

End Sub

'==================================================================

Function getCodeFromUser As Variant

Dim Rng As Range

Do
    Platecode = Application.InputBox(prompt:="Enter a code", Type:=1) ' where is Platecode defined or set ?

    Set Rng = ws.Range("c4:c128").Find(What:=Platecode, LookAt:=xlWhole)
    If Not Rng Is Nothing Then Exit Function ' <-- you want to exit the function if Find was Successful ?

    With ws.Cells(i, 4).Validation ' < where is i defined or set ?
        .Delete
        .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:=xlBetween, Formula1:="=" & rangs.Address ' <-- where is rangs defined or set ?

        If ws.Cells(i, 4) Is Nothing Then ' <-- this If doesn't need to be nested inside the 'With' above
            MsgBox "Something is wrong with " & ws.Cells(i, 4).value
        End If
    End With
Loop Until code = False ' where is code defined ? where do you reset it?

End Function