将变量设置为找到的值会产生"编译错误"在vba

时间:2015-11-30 18:37:26

标签: excel-vba compilation vba excel

我试图根据我找到的值设置一个变量,但VB会产生"编译错误:无效的标识符",并突出显示我的变量。我将在下面发布代码并确定产生错误的行。

代码的作用: a)要求用户输入start&结束日期格式为周/年 b)如果用户这样做,代码将在该范围内找到指定的值,并取该值低于该值4行的值。 这就是它 - 非常简单&直截了当...

Private Sub Workbook_Open()

Dim ans As Variant, stDt As Variant, endDt As Variant, endCl As Variant, stCl    As Variant, wsheet As Worksheet, wbook As Workbook, _
fn_stDt As Variant, wk As Integer, rl_st_Dt As String, rl_end_Dt As String,       fn_endDt As Variant

Set wbook = Workbooks("wbook1.xlsm")
Set wsheet = wbook.Worksheets("sheet1")

ans = MsgBox("Would you like to specify the range of dates now?", vbYesNo,    "Select answer")

If ans = vbYes Then
 stDt = Application.InputBox("Please input the starting date", "Start week  for forecast / sales", "ww/yyyy", vbOKCancel, vbQuestion)
    If stCl = vbCancel Then
        Exit Sub
    End If

endDt = Application.InputBox("Please input the end date", "End week for forecast / sales", "ww/yyyy", vbOKCancel, vbQuestion)
    If endCl = vbCancel Then
        Exit Sub
    End If
Else
    Exit Sub
End If

With wsheet.Range("S5", Range("S5").End(xlToRight))

 Set fn_stDt = .Find(what:=stDt, LookIn:=xlValues, searchorder:=xlByRows, _
 searchdirection:=xlNext, MatchCase:=False, searchformat:=False)
 If Not fn_stDt Is Nothing Then
    wk = fn_stDt.Offset(-2, 0).Value
    If wk.Value <> wk.Offset(0, 1).Value Then 'Compile Error: Invalid qualifier error appears first here and highlights "wk"
        rl_st_Dt.Value = wk.Offset(-4, 0).Value
    ElseIf wk.Value = wk.Offset(0, 1) Then
        rl_st_Dt.Value = wk.Offset(-4, 1).Value
    End If
End If

End With

With wsheet.Range("S5", Range("S5").End(xlToRight))

 Set fn_endDt = .Find(what:=endDt, LookIn:=xlValues, searchorder:=xlByRows,  _
 searchdirection:=xlNext, MatchCase:=False, searchformat:=False)
 If Not fn_endDt Is Nothing Then
    wk.Value = fn_endDt.Offset(-2, 0).Value
    If wk.Value <> wk.Offset(0, 1).Value Then
        rl_end_Dt.Value = wk.Offset(-4, 0).Value
    ElseIf wk.Value = wk.Offset(0, 1).Value Then
        rl_end_Dt.Value = wk.Offset(-4, 1).Value
    End If
 End If

 End With
 End Sub

有人可以提出上述决议吗?

由于

0 个答案:

没有答案