Excel VBA,for循环忽略隐藏的行

时间:2016-08-12 02:13:37

标签: excel vba excel-vba

基本上它的作用是检查ws2中的每一行,其中列="更新"然后拉出一个特定的列数据并将其抛出到ws1中的相应单元格中。这是第一次实施,一切运行顺利,现在由于某种原因需要一些时间才能完成。

Dim LastRow As Long, CurRow As Long, DestRow As Long, DestLast As Long
Dim checkstatus As String
Dim ws1 As Worksheet, ws2 As Worksheet

Set ws1 = Sheets("Dashboard")
Set ws2 = Sheets("TempHRI")

LastRow = ws2.Range("B" & Rows.Count).End(xlUp).Row
DestLast = ws1.Range("E" & Rows.Count).End(xlUp).Row

For CurRow = 2 To LastRow 'Assumes first row has headers
checkstatus = CStr(ws2.Range("AB" & CurRow).Value)

If checkstatus = "UPDATE" Then
'Column that looks up the word "Update" in ws2
If Not ws1.Range("E15:E" & DestLast).Find(ws2.Range("B" & CurRow).Value, LookIn:=xlValues, LookAt:=xlWhole) Is Nothing Then
        DestRow = ws1.Range("E15:E" & DestLast).Find(ws2.Range("B" & CurRow).Value, LookIn:=xlValues, LookAt:=xlWhole).Row
    End If

    ws1.Range("I" & DestRow).Value = ws2.Range("F" & CurRow).Value 'assumes supervisor is in column C in both sheets

End If

Next CurRow

我想在ws1中放置一个过滤器,这样我可以最小化它需要检查的行数。现在我非常确定下面的代码不会忽略隐藏的行。我在运行循环时需要帮助调整代码以排除隐藏的行。

1 个答案:

答案 0 :(得分:3)

尝试检查.EntireRow.Hidden属性:

For CurRow = 2 To LastRow 'Assumes first row has headers
    ' DO something only if the row is NOT hidden
    If ws1.Rows(CurRow).EntireRow.Hidden = False Then
        checkstatus = CStr(ws2.Range("AB" & CurRow).Value)

        If checkstatus = "UPDATE" Then
        'Column that looks up the word "Update" in ws2
        If Not ws1.Range("E15:E" & DestLast).Find(ws2.Range("B" & CurRow).Value, LookIn:=xlValues, LookAt:=xlWhole) Is Nothing Then
                DestRow = ws1.Range("E15:E" & DestLast).Find(ws2.Range("B" & CurRow).Value, LookIn:=xlValues, LookAt:=xlWhole).Row
            End If

            ws1.Range("I" & DestRow).Value = ws2.Range("F" & CurRow).Value 'assumes supervisor is in column C in both sheets

        End If
    End If
Next CurRow

修改:在ws2

下面的评论后添加
For CurRow = 2 To LastRow 'Assumes first row has headers
    ' DO something only if the row is NOT hidden
    If ws1.Rows(CurRow).EntireRow.Hidden = False Then
        ' Checking ws2 as well, for hidden rows
        If ws2.Range("AB" & CurRow).EntireRow.Hidden = False Then
            checkstatus = CStr(ws2.Range("AB" & CurRow).Value)

            If checkstatus = "UPDATE" Then
            'Column that looks up the word "Update" in ws2
            If Not ws1.Range("E15:E" & DestLast).Find(ws2.Range("B" & CurRow).Value, LookIn:=xlValues, LookAt:=xlWhole) Is Nothing Then
                    DestRow = ws1.Range("E15:E" & DestLast).Find(ws2.Range("B" & CurRow).Value, LookIn:=xlValues, LookAt:=xlWhole).Row
                End If

                ws1.Range("I" & DestRow).Value = ws2.Range("F" & CurRow).Value 'assumes supervisor is in column C in both sheets

            End If
        End If
    End If
Next CurRow

请尝试以上操作,看看它是否符合您的需求

相关问题