简体   繁体   中英

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". Any assistance would be greatly appreciated.

-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. That string is the concatenation of "A2" and Cells.SpecialCells(xlCellTypeLastCell).Row).SpecialCells(xlCellTypeVisible) .

What is Cells.SpecialCells(xlCellTypeLastCell).Row).SpecialCells(xlCellTypeVisible) ? Well implicitely Cells is a range representing every cell in the active spreadsheet, ThisWorkbook.ActiveSheet.Cells . Then you're asking for SpecialCells(xlCellTypeLastCell) and taking it's row? What happened to sht1 and 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...

First things first, I'd recommend you avoid using string concatenation for finding cells. None of this "A" & numericVariable stuff. Use something more direct like Cells(numericVariable, 1) . Excessive string manipulation will likely hurt you. The cells property can take a row and a column as parameters.

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.

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. .Range(ws.Cells(2, 1), ws.Cells(lRow, 5)) . That range, is from the second row, first column, aka "A2" . It goes to the row number contained in lRow on the 5th column, aka "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)

Once we have that first row, we can get different columns from it with either .Cells or .Column .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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