过滤和返回行,第一行总是返回,vba

时间:2015-02-03 05:29:53

标签: excel vba excel-vba filter copying

我有一个组合框,其中包含我希望在另一个工作簿列中搜索的值。然后,使用autofilter的代码返回在同一列中具有该值的行(第4列)。 它正常工作但是源的第一行总是被复制到目的地,天气确实或没有在特定列中找到值。 正在使用偏移或单元格移位,因为两个工作表中的前两行都是标题

Sub CommandButton1_Click()
'Look in data repository for the Combobox filter value and only return those associated rows (can be more than one)

Dim DataBlock As Range, Dest As Range
Dim LastRow As Long, LastCol As Long
Dim SheetOne As Worksheet, SheetTwo As Worksheet
Dim PN As String
PN = ComboBox1.Value

'set references up-front
Set SheetTwo = ThisWorkbook.Worksheets("Report") 'this is the expiditing report
Set SheetOne = Workbooks.Open("C:\Users\Colin\Documents\Nexen\Data Repository V1.xlsm").Sheets("Data") 'this is the expiditing report
Set Dest = SheetTwo.Cells(3, 1) '<~ this is where we'll put the filtered data

'identify the "data block" range, which is where
'the rectangle of information that Ill apply
'.autofilter to
With SheetOne
    LastRow = .Range("A" & .Rows.Count).End(xlUp).Row
    LastCol = .Cells(2, .Columns.Count).End(xlToLeft).Column
    Set DataBlock = .Range(.Cells(3, 1), .Cells(LastRow, LastCol))
    'Set DataBlock = Range("A3:AV65000") 'for testing
End With

'apply the autofilter to column D (i.e. column 4)
With DataBlock
    'can use offset .Offset(2, 0).
    .AutoFilter Field:=4, Criteria1:=PN
    'copy the still-visible cells to sheet 2
    .SpecialCells(xlCellTypeVisible).Copy Destination:=Dest
End With

'turn off the autofilter
With SheetOne
    .AutoFilterMode = False
    If .FilterMode = True Then .ShowAllData
End With

End Sub

Sub CommandButton2_Click()

Dim MyBook As String
Dim MyRange As Range
    'Get name of current wb
    MyBook = ThisWorkbook.Name
  Set MyRange = MyBook.Sheets("Report").Range("T3,AC65000")

'ActiveWorkbook.Close savechanges:=True
 MyBook.Activate


End Sub

![ETR] [1]

那么为什么我不顾第一排呢?我尝试了很多东西。

1 个答案:

答案 0 :(得分:0)

您的.Range需要位于Table,并且标头AutoFilter才能正常运行。