Excel IF语句中ISTEXT函数的VBA错误

Excel IF语句中ISTEXT函数的VBA错误,excel,vba,Excel,Vba,刚开始使用VBA,我基本上希望检查列中的项目是否为文本,然后将其复制到行中的另一个工作表中。我在IF语句的第一行被 错误424-需要对象 我看了其中的一些问题和网站,似乎不知道哪里出了问题 非常感谢 Sub Copier() Dim i As Integer Dim j As Integer j = 1 For i = 1 To 100 If IsText.Sheets("Strategies").Cells(i, 6) = True Then Sheets("S

刚开始使用VBA,我基本上希望检查列中的项目是否为文本,然后将其复制到行中的另一个工作表中。我在IF语句的第一行被

错误424-需要对象

我看了其中的一些问题和网站,似乎不知道哪里出了问题

非常感谢

Sub Copier() 

Dim i As Integer
Dim j As Integer

j = 1

For i = 1 To 100
    If IsText.Sheets("Strategies").Cells(i, 6) = True Then
        Sheets("Strategies").Select
        Cells(i, 6).Select
        Selection.Copy
        Sheets("Stats").Select
        Cells(2, j).Select
        Sheets("Stats").Paste
        j = j + 1
    End If
Next i

End Sub
工作表函数
类的方法

您的语法错误,更正如下:

If WorksheetFunction.IsText(Sheets("Strategies").Cells(i, 6)) = True Then
不应使用
调用
IsText()
方法,而应使用
()
,如下所示:

For i = 1 To 100
    s = Sheets("Strategies").Cells(i, 6).Value
    If Application.WorksheetFunction.IsText(s)Then
        Sheets("Strategies").Select
        Cells(i, 6).Select
        Selection.Copy
        Sheets("Stats").Select
        Cells(2, j).Select
        Sheets("Stats").Paste
        j = j + 1
    End If
Next i

使用Variant非常快

Sub test()

Dim i As Integer
Dim j As Integer
Dim Wf As WorksheetFunction
Dim fromWs As Worksheet, ToWs As Worksheet
Dim vDB, vR()

Set fromWs = Sheets("Strategies")
Set ToWs = Sheets("Stats")

Set Wf = WorksheetFunction
vDB = fromWs.Range("f1").Resize(100)



    For i = 1 To UBound(vDB, 1)
        If Wf.IsText(vDB(i, 1)) Then
            j = j + 1
            ReDim Preserve vR(1 To j)
            vR(j) = vDB(i, 1)
        End If
    Next i
    If j > 0 Then
        ToWs.Range("a2").Resize(1, j) = vR
    End If
End Sub

你可以把整个事情整理如下:

Dim i As Integer, j As Integer
Dim sourcesheet As Worksheet, targetsheet As Worksheet

j = 1

Set sourcesheet = Sheets("Strategies")
Set targetsheet = Sheets("Stats")

With sourcesheet   
    For i = 1 To 100
        s = .Cells(i, 6).Value
        If Application.WorksheetFunction.IsText(s) Then
            .Cells(i, 6).Copy targetsheet.Cells(2, j)
            j = j + 1
        End If
    Next i
End With
引用应该在
()
中,而不是在
之后:请参见此处:如果Wf.IsText(vDB(i,1))那么与If not is numeric(vDB(i,1))那么相同。