2014-03-26 71 views
0

我有一個過濾範圍的宏,我有一個值範圍,我想表示應用過濾器後選擇的行數。VBA在過濾範圍上選擇特定行數

我有大部分的代碼排序,我只是被困在選擇只可見的行。 EG。工作表1包含我標記爲NOC1的可變數字(1,2,3,4等)。

現在,一旦應用過濾器,它將選擇正確的行數,但也會選擇隱藏的單元格。我只希望它只選擇可見的單元格。

下面是代碼:

Set TopVisibleCell = Rstatus.Offset(1).Rows.SpecialCells(xlCellTypeVisible).Rows(1) 
TopVisibleCell.Select 
Selection.Resize(Selection.Rows.Count + NOC1 - 1, _ 
Selection.Columns.Count).Copy 

任何幫助將不勝感激。

謝謝!

編輯:

請原諒我那可憐的描述,看來我沒有表達自己清楚。 請找到Sample.xlsm的鏈接,希望能夠解決我的問題。

鏈接:Sample Workbook

感謝您的幫助

回答

0

可以循環使用計數器:

Sub FilterCDA() 
    Dim sh1       As Worksheet 
    Dim N       As Long 
    Dim TopVisibleCell    As Range 
    Dim sh2       As Worksheet 
    Dim HeaderRow     As Long 
    Dim LastFilterRow    As Long 
    Dim st       As String 
    Dim rng1      As Range 
    Dim rng2      As Range 
    Dim rng3      As Range 
    Dim VTR       As String 
    Dim W       As Integer 
    Dim R       As Integer 
    Dim NOC       As Range 
    Dim NOC1      As Integer 
    Dim rSelect      As Range 
    Dim rCell      As Range 


    Set sh1 = Sheets("Request") 
    Set sh2 = Sheets("Request") 

    C = 2 
    Set NOC = sh2.Range("D2") 
    NOC1 = NOC.Value 

    LR = Worksheets("ORT").Range("A" & Rows.Count).End(xlUp).Row 
    Set Rstatus1 = Worksheets("ORT").Range("G2:G" & LR) 
    Set Rstatus = Worksheets("ORT").Range("A1:G" & LR) 
    N = sh1.Cells(Rows.Count, "C").End(xlUp).Row 

    Sheets("CSV").Cells.NumberFormat = "@" 
    For i = 2 To N 
     v = sh1.Cells(i, 3).Value 
     If v <> "" Then 
     st = st & v & "," 
     End If 
    Next i 
    st = Mid(st, 1, Len(st) - 1) 
    Arr1 = Split(st, ",") 
    Sheets("ORT").Activate 
    For i = LBound(Arr1) To UBound(Arr1) 
     Sheets("ORT").AutoFilterMode = False 
     With Sheets("ORT").Range("A:G") 
     .AutoFilter Field:=3, Criteria1:=Arr1(i), Operator:=xlFilterValues 
     End With 

     Fr = Worksheets("ORT").Range("C" & Rows.Count).End(xlUp).Row - 1 

     ' No rows filtered then Fr = 0 

     If Fr > 0 Then 

     With Rstatus 
      Set rVis = .Resize(.Rows.Count - 1, 1).Offset(1).SpecialCells(xlCellTypeVisible) 
     End With 

     For Each rCell In rVis.Cells 
      If rSelect Is Nothing Then 
       Set rSelect = rCell.Resize(, Rstatus.Columns.Count) 
      Else 
       Set rSelect = Union(rSelect, rCell.Resize(, Rstatus.Columns.Count)) 
      End If 
      lCounter = lCounter + 1 
      If lCounter >= NOC1 Then Exit For 
     Next rCell 

     rSelect.Copy 
     Sheets("CSV").Cells(Rows.Count, "A").End(xlUp).Offset(1).PasteSpecial xlPasteValues 

     ElseIf Fr = 0 Then 
     End If 

     Set NOC = NOC.Offset(1) 
     NOC1 = NOC.Value 
    Next i 
    Sheets("ORT").AutoFilterMode = False 

    Sheets("Request").Select 
    Range("E2").Select 
    ActiveCell.FormulaR1C1 = "=COUNTIF('CSV'!C[-2],'Request'!RC[-2])" 
    On Error Resume Next 
    Selection.AutoFill Destination:=Range("E2:E" & Range("C" & Rows.Count).End(xlUp).Row), Type:=xlFillCopy 
    Columns("E:E").Select 
    Selection.Copy 
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _ 
                    :=False, Transpose:=False 

    Range("A1").Select 
    Sheets("Control").Select 
    Range("A1").Select 


End Sub 
+0

請參閱編輯並鏈接到示例工作簿。我很抱歉在我的初始文章中沒有完全清楚。謝謝 – user2298601

+0

@加里的學生請參閱編輯和鏈接到示例工作簿。我很抱歉在我的初始文章中沒有完全清楚。謝謝 – user2298601

+0

@ user2298601代碼已更改爲與您的工作簿相匹配:-) – JosieP

0

如果列#1是標題行,你要選擇的自動篩選的可視範圍並沒有「垃圾」中列的篩選下方一個,則:

Sub SelectVisibleA() 
    Dim NLastVisible As Long, r As Range 
    NLastVisible = Cells(Rows.Count, "A").End(xlUp).Row 
    Set r = Range("A2:A" & NLastVisible).Cells.SpecialCells(xlCellTypeVisible) 
    r.Select 
End Sub 

將在一個列中的可見物質...........你需要RESIZE獲得更多列。

+0

感謝您的回覆,也許是我原來的職位wasnt清楚。我知道如何選擇所有可見的單元格,但是如何在過濾的範圍內選擇「X」數量的可見行?假設「X」是一個變量,每次都會改變。謝謝 – user2298601

+0

@ user2298601:在我的例子中,** r **是單列中的一個範圍.......... ** r(7)**將是第七個可見單元,所以** r( 7).EntireRow **將是第七個可見行...... ** UNION(r(2),r(3))**將是第二個和第三個可見單元格等等 –

+0

@ Gary的學生r(7)相當於r.cells(7),並且不僅限於可見細胞。你必須使用'for each'循環 – JosieP