简体   繁体   English

使用VB.NET从Excel中的特定范围复制

[英]Copying from a specific range in Excel using VB.NET

I am currently building a program where I copy a pivot table from one workbook and copy it into another, and am currently having trouble copying the table, as the number of rows depends on how many people buys a product every month. 我目前正在构建一个程序,该程序将一个工作簿中的数据透视表复制到另一个工作簿中,并且由于行数取决于每月有多少人购买产品,因此目前无法复制该表。 I tried using a UsedRange on that specific range, and it didn't work. 我尝试在该特定范围内使用UsedRange,但没有用。 Something along these lines... 这些东西...

    targetSheet.Range("N3:S50").UsedRange.Copy()

Any ideas? 有任何想法吗?

Range("A1:B2").Select
Selection.Copy

The following code grabs the data for a particular sheet (in this case tSheet). 以下代码获取特定工作表(在本例中为tSheet)的数据。 It then finds the upper-left cell address, copies all data, deletes everything from the spreadsheet, and then pastes it back. 然后,它找到左上角的单元格地址,复制所有数据,从电子表格中删除所有内容,然后将其粘贴回。 This has the effect of resetting the UsedRange on a particular sheet. 这具有在特定工作表上重置UsedRange的效果。

Dim values 'holds info on the usedRange of the current spreadsheet
Dim usedRangeAddress As String 'holds the address of the usedRange of the current spreadsheet (i.e. "A1")
usedRangeAddress = tSheet.UsedRange.Address 'get the upper most left cell address
values = tSheet.UsedRange.Value'Store values of all cells to array.     
tSheet.Cells.Delete() 'Delete all cells in the sheet     
tSheet.Range(usedRangeAddress).Value = values'Restore values to their initial locations     

So, with this, you should be able to utilize the first line of code, and then a second line to paste to your otherSheet 因此,有了这一点,您应该能够利用第一行代码,然后利用第二行代码粘贴到您的otherSheet中。

Dim values 'holds info on the usedRange of the current spreadsheet
values = tSheet.UsedRange.Value'Store values of all cells to array.     
otherSheet.Range(yourTargetAddress).Value = values

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

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