Excel 如何更改此vba代码以仅获取自动筛选的范围而不是第二行?
正如我在问题中所说,我在复制和粘贴自动过滤范围时遇到困难。代码每次都会获取第二行以及自动筛选的范围。你知道如何解决这个问题吗Excel 如何更改此vba代码以仅获取自动筛选的范围而不是第二行?,excel,vba,Excel,Vba,正如我在问题中所说,我在复制和粘贴自动过滤范围时遇到困难。代码每次都会获取第二行以及自动筛选的范围。你知道如何解决这个问题吗 Sub Copy() Dim src As Worksheet Dim tgt As Worksheet Dim filterRange As Range Dim copyRange As Range Dim lastRow As Long Dim DestLastRow As
Sub Copy()
Dim src As Worksheet
Dim tgt As Worksheet
Dim filterRange As Range
Dim copyRange As Range
Dim lastRow As Long
Dim DestLastRow As Long
Set src = Workbooks("Shipping Test.xlsm").Worksheets("LOWES HD")
Set tgt = Workbooks("Reports.xlsm").Worksheets("All Data")
' turn off any autofilters that are already set
src.AutoFilterMode = False
' find the last row with data in column A
lastRow = src.Range("A" & src.Rows.Count).End(xlUp).Row
' the range that we are auto-filtering (all columns)
Set filterRange = src.Range("A2:S" & lastRow)
' the range we want to copy (only columns we want to copy)
' in this case we are copying country from column A
' we set the range to start in row 2 to prevent copying the header
Set copyRange = src.Range("A2:S" & lastRow)
' filter range based on column J
filterRange.AutoFilter field:=10, Criteria1:="DISPATCHED"
' copy the visible cells to our target range
DestLastRow = tgt.Cells(tgt.Rows.Count, "A").End(xlUp).Offset(1).Row
copyRange.SpecialCells(xlCellTypeVisible).Copy tgt.Range("A" & DestLastRow)
'Turn off Auto Filter again
src.AutoFilterMode = False
第2行是标题行吗?@Gary的学生否标题是第1行,第2行是第一行有数据通常您在标题行而不是第一行数据上设置过滤器,然后您可以使用
filterange
和Offset
和Resize
来设置复制范围。但是,通过将copyRange
设置为范围(“A2:S”&lastRow)
,您告诉它包含Row2
,您只需将2
替换为3
@GMalc,谢谢您的工作!