Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel 如何从一张工作表中复制过滤后的数据并粘贴到另一张工作表中?_Excel_Vba - Fatal编程技术网

Excel 如何从一张工作表中复制过滤后的数据并粘贴到另一张工作表中?

Excel 如何从一张工作表中复制过滤后的数据并粘贴到另一张工作表中?,excel,vba,Excel,Vba,我输入了自动筛选代码来指定copypaste操作的条件,但不确定是否正确执行了该操作 Sub test3() Dim ws1 As Worksheet, ws2 As Worksheet Dim copyFrom As Range Dim lRow As Long Dim strSearch As String Set ws1 = Worksheets("sheet1") strSearch = "LOCAL" With ws1 '~~> Remove any filt

我输入了自动筛选代码来指定copypaste操作的条件,但不确定是否正确执行了该操作

Sub test3()

Dim ws1 As Worksheet, ws2 As Worksheet
Dim copyFrom As Range
Dim lRow As Long
Dim strSearch As String


Set ws1 = Worksheets("sheet1")

strSearch = "LOCAL"

With ws1

    '~~> Remove any filters
    .AutoFilterMode = False


    lRow = .Range("L" & .Rows.Count).End(xlUp).Row

    With .Range("L4:L" & lRow)
        .AutoFilter Field:=1, Criteria1:="=*" & strSearch & "*"
        Set copyFrom = .Offset(1, 0).SpecialCells(xlCellTypeVisible).EntireRow
    End With

    '~~> Remove any filters
    .AutoFilterMode = False
End With

'~~> Destination File

Set ws2 = Worksheets("Sheet2")

With ws2
    If Application.WorksheetFunction.CountA(.Cells) <> 0 Then
        lRow = .Cells.Find(What:="*", _
                      After:=.Range("A1"), _
                      Lookat:=xlPart, _
                      LookIn:=xlFormulas, _
                      SearchOrder:=xlByRows, _
                      SearchDirection:=xlPrevious, _
                      MatchCase:=False).Row
    Else
        lRow = 1
    End If

    copyFrom.Copy .Rows(lRow)
End With

Worksheets("Sheet2").Columns().AutoFit
Cells(1, 1).Activate
子测试3()
将ws1标注为工作表,将ws2标注为工作表
从As范围变暗复制
暗淡的光线和长的一样
作为字符串的Dim stresearch
设置ws1=工作表(“表1”)
strSearch=“本地”
使用ws1
“~~>删除任何筛选器
.AutoFilterMode=False
lRow=.Range(“L”和.Rows.Count).End(xlUp).Row
带.Range(“L4:L”和lRow)
.AutoFilter字段:=1,标准1:=“=*”&strSearch&“*”
设置copyFrom=.Offset(1,0).SpecialCells(xlCellTypeVisible).EntireRow
以
“~~>删除任何筛选器
.AutoFilterMode=False
以
“~~>目标文件
设置ws2=工作表(“表2”)
与ws2
如果Application.WorksheetFunction.CountA(.Cells)为0,则
lRow=.Cells.Find(内容:=“*”_
之后:=.范围(“A1”)_
看:=xlPart_
LookIn:=xl公式_
搜索顺序:=xlByRows_
搜索方向:=xlPrevious_
MatchCase:=False)。行
其他的
lRow=1
如果结束
copyFrom.Copy.Rows(lRow)
以
工作表(“Sheet2”).Columns().AutoFit
细胞(1,1)。激活
端接头

如果有人能指出我使用的代码出了什么问题,我将不胜感激。
谢谢。

请解释一下到底出了什么问题?您是否特别看到了部分
Set copyFrom=.Offset(1,0).SpecialCells(xlCellTypeVisible).EntireRow
?@SiddharthRout欢迎回来;)你去过哪里?度假;)旁注:除非与旧API通信,否则始终使用
Long
而不是
Integer
。Excel的行数超过了
Integer
所能处理的行数。@SiddharthRout我将浏览链接。谢谢分享。