vba - Excel VBA,for循环忽略隐藏行

标签 vba excel

基本上,它的作用是检查 ws2 中的每一行,其中 a column = "Update"然后提取特定列数据并将其扔到 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 中放置一个过滤器,这样我可以最大限度地减少它需要检查的行数。现在我很确定下面的代码不会忽略隐藏的行。当我运行循环时,我需要帮助调整代码以排除隐藏行。

最佳答案

尝试检查 .EntireRow.Hidden属性(property):

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

请尝试以上方法,看看是否满足您的需求

关于vba - Excel VBA,for循环忽略隐藏行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38908816/

相关文章:

java - 如何在不使用 EOMONTH 的情况下在 Excel 中查找上个月的最后一天

Excel VBA在没有循环的情况下交换vba范围的列

vba - 在 Range 属性中引用许多多个命名范围时出错

excel - 一张纸中的 2 个宏针对相同的单元格

arrays - 扩展每个列单元格的列单元格

vba - 如何在vba中刷新/加载Excel中的RTD Bloomberg函数(BDH)

excel - IF语句: how to leave cell blank if condition is false ("" does not work)

excel - 单元格内容的工具提示excel

python - 写入 excel 文件时超过 url 数量

Excel VBA : How do I PasteSpecial into same row of Cell location after Cell is changed?