2016-08-12 161 views
1

基本上,它會檢查ws2中的每一行,其中列=「更新」然後提取特定列數據並將其引發到ws1中的相應單元格中。這是第一次實施,所有事情都順利進行,現在由於某種原因需要一點時間才能完成。Excel VBA,for循環忽略隱藏行

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中放置一個過濾器,這樣我就可以最小化它需要檢查的行數。現在我很確定下面的代碼不會忽略隱藏的行。當我運行循環時,我需要幫助調整代碼以排除隱藏的行。

+0

只需使用自動篩選後評論添加在Worksheet2上顯示[這裏](http://stackoverflow.com/questions/11631363/how-to-copy-a-line-in-excel-using-a-specific-word-and-pasting-to-another- excel -s)a然後循環通過該範圍 –

回答

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 

請嘗試上面,看它是否符合您的需求

+0

嗨感謝您的答覆。我們是否也可以檢查隱藏行的ws2?謝謝。 – wh3resmycar2