简体   繁体   English

将过滤的单元格复制到另一张纸上

[英]Copy filtered cells to another sheet

I have four rows of information that I filter. 我过滤了四行信息。 (month, Name, Service, units) I filter the information by name and month. (月份,名称,服务,单位)我按名称和月份过滤信息。 (depending on information needed) I have put together the following to try and grab the top row of filtered data and place it on another sheet. (取决于所需的信息)我整理了以下内容,以尝试获取已过滤数据的第一行并将其放在另一张纸上。

Sub Mine()
   Dim sht1 As Worksheet
   Dim sht2 As Worksheet
   Dim lRow As Long

      Set sht1 = ThisWorkbook.Sheets("Export")
      Set sht2 = ThisWorkbook.Sheets("DB1")

      lRow = sht1.Cells(Rows.Count, "A").End(xlUp).Row + 1

      sht1.Range("b" & lRow) = sht2.Range("A2" & Cells.SpecialCells(xlCellTypeLastCell).Row).SpecialCells(xlCellTypeVisible) 'Month
      sht1.Range("a" & lRow) = sht2.Range("E2" & Cells.SpecialCells(xlCellTypeLastCell).Row).SpecialCells(xlCellTypeVisible) 'Client Name
      sht1.Range("c" & lRow) = sht2.Range("C2" & Cells.SpecialCells(xlCellTypeLastCell).Row).SpecialCells(xlCellTypeVisible) 'Service
      sht1.Range("d" & lRow) = sht2.Range("H1" & Cells.SpecialCells(xlCellTypeLastCell).Row).SpecialCells(xlCellTypeVisible) 'Units

  End Sub

This does not error out, it just doesn't copy anything to the "export" sheet. 这不会出错,只是不会将任何内容复制到“导出”工作表中。 The only way I can get anything to copy from one sheet to another is to take the specialcells out. 我可以将任何内容从一张纸复制到另一张纸的唯一方法是取出特殊单元。 As follows... 如下...

Sub Mine()
 Dim sht1 As Worksheet
 Dim sht2 As Worksheet
 Dim lRow As Long

     Set sht1 = ThisWorkbook.Sheets("Export")
     Set sht2 = ThisWorkbook.Sheets("DB1")

     lRow = sht1.Cells(Rows.Count, "A").End(xlUp).Row + 1

     sht1.Range("b" & lRow) = sht2.Range("A2") 'Month
     sht1.Range("a" & lRow) = sht2.Range("E2") 'Client Name
     sht1.Range("c" & lRow) = sht2.Range("C2") 'Service
     sht1.Range("d" & lRow) = sht2.Range("H1") 'Units

End Sub

But as said prior, it only copies the first line, filtered or not. 但是如前所述,它仅复制第一行,无论是否过滤。 My goal is to obtain the filtered top line of "DB1" and copy it to sequenced cells in "Export". 我的目标是获取过滤后的“ DB1”顶行并将其复制到“导出”中的已排序单元格中。 Any assistance would be greatly appreciated. 任何帮助将不胜感激。

-JGr3g -JGr3g

Okay, so I don't think your code is doing what you think it is. 好的,所以我认为您的代码没有按照您的想法做。 Lets take some apart 让我们分开

sht2.Range("A2" & Cells.SpecialCells(xlCellTypeLastCell).Row).SpecialCells(xlCellTypeVisible)

You're calling sht2.Range() and passing it a string. 您正在调用sht2.Range()并将其传递给字符串。 That string is the concatenation of "A2" and Cells.SpecialCells(xlCellTypeLastCell).Row).SpecialCells(xlCellTypeVisible) . 该字符串是"A2"Cells.SpecialCells(xlCellTypeLastCell).Row).SpecialCells(xlCellTypeVisible)的串联。

What is Cells.SpecialCells(xlCellTypeLastCell).Row).SpecialCells(xlCellTypeVisible) ? 什么是Cells.SpecialCells(xlCellTypeLastCell).Row).SpecialCells(xlCellTypeVisible) Well implicitely Cells is a range representing every cell in the active spreadsheet, ThisWorkbook.ActiveSheet.Cells . 确切地说,“ Cells是一个范围,表示活动电子表格ThisWorkbook.ActiveSheet.Cells中的每个单元格。 Then you're asking for SpecialCells(xlCellTypeLastCell) and taking it's row? 然后,您要查询SpecialCells(xlCellTypeLastCell)并使其成为行? What happened to sht1 and sht2? sht1和sht2怎么了? You aren't using them, you're using the active sheet? 您没有使用它们,您正在使用活动表吗?

So if the last cell of the active sheet is in row 50, you are asking for "A2" & 50 which would get "A250" , which would be blank since the last used row is 50... 因此,如果活动工作表的最后一个单元格在第50行中,则您要求输入"A2" & 50 ,这将得到"A250" ,由于最后使用的行是50,因此它将为空白...

First things first, I'd recommend you avoid using string concatenation for finding cells. 首先,我建议您避免使用字符串串联查找单元格。 None of this "A" & numericVariable stuff. 没有任何"A" & numericVariable东西。 Use something more direct like Cells(numericVariable, 1) . 使用更直接的东西,例如Cells(numericVariable, 1) Excessive string manipulation will likely hurt you. 过多的字符串操作可能会伤害您。 The cells property can take a row and a column as parameters. cells属性可以将行和列作为参数。

Is your filered range a table/listobject or just a plain old range with an AutoFilter on it? 您的归档范围是表/列表对象还是只是带有自动筛选的普通旧范围? If it's a listobject, get the listobject, get it's DataBodyRange, use .SpecialCells(xlCellTypeVisible) on that, THEN get the first row of the resulting range. 如果是列表对象,则获取列表对象,获取其DataBodyRange,在其上使用.SpecialCells(xlCellTypeVisible) ,然后获取结果范围的第一行。

Similarly if it's an autofiltered range, get the target data range, the entire data range. 同样,如果它是自动筛选的范围,则获取目标数据范围,即整个数据范围。 Once you have that, grab the visible cells, then get the first row. 一旦有了它,抓住可见的单元格,然后获得第一行。

Edit: Untested example 编辑:未经测试的例子

set targetRow = ws.Range(ws.Cells(2, 1), ws.Cells(lRow, 5)).SpecialCells(xlCellTypeVisible).Row(1)
targetSheet.Range("A2") = targetRow.Cells(1,2)

Okay, so what's this doing? 好的,这是做什么的? I'm taking a worksheet variable ws and getting a range on it. 我正在工作表变量ws并得到一个范围。 .Range(ws.Cells(2, 1), ws.Cells(lRow, 5)) . .Range(ws.Cells(2, 1), ws.Cells(lRow, 5)) That range, is from the second row, first column, aka "A2" . 该范围来自第二行第一列,又称"A2" It goes to the row number contained in lRow on the 5th column, aka "E" & lRow . 它转到第5列lRow中包含的行号,又名"E" & lRow

From there it gets only the visible cells .SpecialCells(xlCellTypeVisible) then gets the first row of the first area in the selection .Row(1) 从那里只获得可见的单元格.SpecialCells(xlCellTypeVisible)然后获得选择中第一个区域的第一行.Row(1)

Once we have that first row, we can get different columns from it with either .Cells or .Column . 一旦有了第一行,就可以使用.Cells.Column从中获得不同的列。

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

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