在if语句中使用.oleobjects.textbox.text

时间:2017-09-02 22:22:15

标签: excel excel-vba vba

我的工作表上有一个文本框和一个png图像(不在userform中)。 png图像加载日历,日历上选择的值将返回到文本框。

我还想在if循环中使用.oleobject。 就像一些xyz变量> = .oleobject

以下是供参考的代码。

Sub futurejoiners()
Dim lr As Long

Windows("EMPDATA.xlsm").Activate
Sheets("INPUTDATA").Select

lr = ActiveSheet.Cells.Find("*", SearchOrder:=xlByRows, searchdirection:=xlPrevious).Row

For i = 2 To lr
Windows("EMPDATA.xlsm").Activate
If ThisWorkbook.Sheets("Sheet1").OLEObjects("TextBox2").Object.Text = "" Then
    MsgBox "Please select the reporting start and end dates in Sheet1"
    Exit Sub
Else
    If Cells(i, "J").value >= ThisWorkbook.Sheets("Sheet1").OLEObjects("TextBox2").Object.Text Then
    Cells(i, "J").Select
    Selection.EntireRow.Delete
    End If
End If
Next
End Sub

我得到一个运行时错误1004无法在if语句中获取工作表类的oleobjects属性,它检查=""

我坚信我在调用该工作簿的工作簿或工作表时会出现问题。

请帮忙。在此先感谢:)

1 个答案:

答案 0 :(得分:0)

我不确定它是否运作良好。但是最好不要使用select,activate方法。

Sub futurejoiners()
Dim lr As Long
Dim myWS  As Worksheet
Dim myWB As Workbook
Dim myDate
Dim i As Long

Set myWB = worboooks("EMPDATA.xlsm")
Set myWS = myWB.Sheets("INPUTDATA")
'Windows("EMPDATA.xlsm").Activate
'Sheets("INPUTDATA").Select
With myWS
    lr = .Cells.Find("*", SearchOrder:=xlByRows, searchdirection:=xlPrevious).Row

    For i = lr To 2 Step -1 '<~~ large to small step -1
        'Windows("EMPDATA.xlsm").Activate
        myDate = ThisWorkbook.Sheets("Sheet1").OLEObjects("TextBox2").Object.Text

        If myDate = "" Then
            MsgBox "Please select the reporting start and end dates in Sheet1"
            Exit Sub
        Else
            If IsNumeric(myDate) Then
            Else
                myDate = DateValue(myDate)
            End If
            'If .Cells(i, "J").Value >= ThisWorkbook.Sheets("Sheet1").OLEObjects("TextBox2").Object.Text Then
            If .Cells(i, "J").Value >= myDate Then
                Cells(i, "J").EntireRow.Delete
            End If
        End If
    Next
End Sub