简体   繁体   English

根据单元格值查找所有行,然后复制到其他工作表。

[英]Find all rows based on a cell value and copy to different sheet.

I am a excel/vba noob. 我是excel / vba新手。 I have very little experience with vba. 我对vba的经验很少。 I am not asking for someone to do my work for me just help me with some samples so I can start figuring out what I need. 我并不是要有人为我做事,只是帮我提供一些样品,这样我就可以开始弄清楚我的需要了。

I have been looking for a way to help me search a spread sheet based on a value I place in a cell. 我一直在寻找一种方法来帮助我根据放置在单元格中的值搜索电子表格。 Here is what I have. 这就是我所拥有的。 I have a spread sheet that has 4 tabs, the first 3 tabs have different data and different number of columns. 我有一个包含4个标签的电子表格,前3个标签具有不同的数据和不同的列数。 The one thing that is the same on each table is the first three columns. 每个表上都相同的一件事是前三列。

date         case#       ticket#
7/14/2018   50807966    5330826969
7/3/2018    50811017    5330860547
7/1/2018    50811022    5330860631
7/13/2018   50811026    5330860683

What I want to do is on sheet 4 enter a number in a cell labeled ticket# and find all rows on each sheet and copy it to the 4th sheet. 我想做的是在工作表4上的标有票证编号的单元格中输入一个数字,然后查找每张工作表上的所有行并将其复制到第四张工作表中。 I want it to copy each row found to the next empty row on sheet 4. I will start with sheet one and work through sheet 3. 我希望它将找到的每一行复制到工作表4的下一个空行。我将从工作表1开始并遍历工作表3。

What I would like help on is how to do the first sheet. 我需要帮助的是如何做第一张纸。 Once I have an Idea of how to do one sheet I can figure out how to do the rest. 一旦有了关于如何做一张纸的想法,我就可以弄清楚剩下的事情该怎么做。

Its not clear what your 4th sheet looks like. 目前尚不清楚您的第四张纸是什么样的。 This will look for a ticket number on your 4th sheet in cell A1. 这将在单元格A1中的第4张纸上查找票证号码。

Once executed, it will loop through each sheet and filter/copy/paste the first 4 columns for all values in filter. 执行后,它将循环遍历每张工作表,并对过滤器中的所有值进行过滤/复制/粘贴前4列。

You will need to change .Sheets("Sheet4") to match your sheet: .Sheets("?") 您将需要更改.Sheets("Sheet4")以匹配您的工作表: .Sheets("?")

Option Explicit

Sub Macro1()
Dim Ws As Worksheet, MyWs As Worksheet
Dim wsLRow As Long, MyLRow As Long

Set MyWs = ThisWorkbook.Sheets("Sheet4")

Dim TicketNumber As String
TicketNumber = MyWs.Range("A1")

Application.ScreenUpdating = False
For Each Ws In Worksheets
    If Ws.Name <> MyWs.Name Then
        With Ws
            If .AutoFilterMode Then .AutoFilterMode = False
            wsLRow = .Range("A" & .Rows.Count).End(xlUp).Row
            MyLRow = MyWs.Range("A" & MyWs.Rows.Count).End(xlUp).Offset(1).Row
            .Range("A:A").AutoFilter 1, TicketNumber
            .Range("A2:D" & wsLRow).SpecialCells(xlCellTypeVisible).Copy
            MyWs.Range("A" & MyLRow).PasteSpecial
            .AutoFilterMode = False
        End With
    End If
Next Ws
Application.ScreenUpdating = True

End Sub

If will need to handle the error when your filter does not find anything. 当您的过滤器找不到任何内容时,是否需要处理错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM