使用包含五列的Excel工作表,基于过滤第三列,我必须将第五列中的数据复制到另一个Excel工作表。
我做了类似的事情,但我无法在过滤后复制第五列的特定数据:
DataTable.AddSheet "Sheet1"
DataTable.ImportSheet "A:\Data.xlsx","Sheet1","Sheet1"
RowCount= DataTable.GetSheet("Sheet1").GetRowCount
Set objexcel = CreateObject("excel.application")
Set objWorkbook = objExcel.WorkBooks.Open ("A:\Data.xlsx")
Set objSheet = objWorkbook.Worksheets(1)
objexcel.Visible=false
objexcel.Rows(1).Insert
objexcel.Columns(2).Insert
objexcel.Columns(2).Insert
objSheet.cells(1,1).value="Minutes Interval"
objSheet.cells(1,2).value="Hour Interval"
objSheet.cells(1,3).value="Hourly Filter"
objSheet.cells(1,4).value="HH:MM:SS"
objSheet.cells(1,5).value="Weight(g)"
For i = 2 To RowCount+2 Step 1
objSheet.cells(i,2).value="=A"&i&"/60"
objSheet.cells(i,3).value="=INT(B"&i&")=B"&i
Next
ObjSheet.cells(2,3).autofilter 3,"True"
Set objRange = objSheet.Range("A1")
objWorkbook.Worksheets(1).UsedRange.Copy '--- to copy entire sheet data to AnotherExcel2
Set objWorkbookn= objExcel.Workbooks.Open ("A:\IntermediateExcel.xlsx")
Set objSheetn = objWorkbookn.Worksheets(1)
objWorkbookn.Worksheets(1).Range("A1").PasteSpecial Paste =xlValues '-- pasted here on AnotherExcel2
Set objRange = objSheet.Range("E2").EntireColumn '-- here selected 5th column entirely
objRange.Copy '-- copied 5th column
Set objWorkbook2= objExcel.Workbooks.Open ("A:\Result.xlsx")
objWorkbook2.Worksheets(1).Range("E2").PasteSpecial Paste =xlValues '-- pasted to resultant excelsheet
objWorkbook.save
objWorkbook.close
objWorkbook2.save
objWorkbook2.close
objWorkbookn.save
objWorkbookn.close
Set objWorkbook = nothing
Set objexcel = nothing
答案 0 :(得分:0)
正如您所提到的,您希望粘贴唯一已过滤的数据,而不是使用此
Set objRange = objSheet.Range("E2").EntireColumn
objRange.Copy 'this will copy the entire column
使用它,它只会复制可见的,即过滤的值。
Set objRange = objSheet.Range("E2").EntireColumn
objRange.SpecialCells(xlCellTypeVisible).Copy 'this will copy only visible cells
并按照您的方式粘贴值。