编译错误对于没有下一步 - VBA代码

问题描述 投票:0回答:1

我试图写一些VBA代码,将搜索一个名为“19年2月1日”工作表中,列(T),并寻找“部署”这个词,如果它发现这个词,然后将整个行复制到一个名为“新的工作表部署”。我添加了一个For循环,我知道我需要添加一个Next但我不能弄明白。

这里是我到目前为止的代码。

Sub CopyRows()    
    Dim cell As Range
    Dim lastRow As Long
    Dim i As Long

    Dim wksh1 As Worksheet
    Set wksh1 = Worksheets("01 Feb 19")

    Dim wksh2 As Worksheet
    Set wksh2 = Worksheets("Deployed")

    lastRow = Range("A" & Rows.Count).End(xlUp).Row 'finds the last row
    i = 1

    For Each cell In wksh1.Range("T1:T" & lastRow) 'looks in T column until the last row
        If cell.Value = "Deployed" Then 'searches for word deployed
            cell.EntireRow.Copy wksh2.Cells(i, 1) 'copies entire row into Deployed work sheet      
        End If
End Sub
excel vba for-loop next
1个回答
0
投票

你错过Next cellFor循环结束

Sub CopyRows()
    Dim cell As Range
    Dim lastRow As Long
    Dim i As Long

    Dim wksh1 As Worksheet
    Set wksh1 = Worksheets("01 Feb 19")

    Dim wksh2 As Worksheet
    Set wksh2 = Worksheets("Deployed")

    lastRow = Range("A" & Rows.Count).End(xlUp).Row 'finds the last row
    i = 1

    For Each cell In wksh1.Range("T1:T" & lastRow) 'looks in T column until the last row
        If cell.Value = "Deployed" Then 'searches for word deployed
            cell.EntireRow.Copy wksh2.Cells(i, 1) 'copies entire row into Deployed work sheet
            i = i + 1 ' <-- Added so that rows don't overwrite each other. Remove if it is intended to overwrite each row
        End If
    Next cell ' <-- Added
End Sub
© www.soinside.com 2019 - 2024. All rights reserved.