[英]Using VB.net, how to copy a column from one worksheet in one book to another worksheet in another book?
I've seen this question asked a lot in the context of VBA, but not Visual Basic.我已经看到这个问题在 VBA 的上下文中被问了很多,但不是 Visual Basic。 In my situation, I must use Visual Basic, and so far I've been unable to get copying columns from one workbook and worksheet to another to workbook and worksheet.在我的情况下,我必须使用 Visual Basic,到目前为止,我无法将列从一个工作簿和工作表复制到另一个工作簿和工作表。
targetWorkBook = target_directory & "\" & "Target.xlsx"
Set objExcel3 = CreateObject("Excel.Application")
Set wb3 = objExcel3.Workbooks.Add
Set ws3 = wb3.Worksheets(1) 'Target worksheet
objExcel3.ActiveWorkbook.SaveAs targetWorkBook
Set objExcel2 = CreateObject("Excel.Application")
Set wb2 = objExcel2.Workbooks.Open("SourceWorkbook.xls")
Set ws2 = wb2.Worksheets(1) ' Source worksheet
lastRow = ws2.UsedRange.Rows.count 'Last row of source worksheet
'Find column by header name in source worksheet
Set fid = wb2.ws2.Range("A1:Z1").Find("FID")
'Copy contents in source workbook and worksheet from row 2 to lastRow
wb2.Worksheets(1).Range(ws2.Cells(2, fid.Column), ws2.Cells(lastRow, fid.Column)).Copy
'Paste to target column A, row 2 in target workbook and worksheet
wb3.ws3.Range("A2").PasteSpecial xlPasteValues
When I later open the target spreadsheet, I appear to have some kind of reference problem.当我稍后打开目标电子表格时,我似乎遇到了某种参考问题。 The data that was copied is actually "embedded", which is not what I want.复制的数据实际上是“嵌入”的,这不是我想要的。 I'm not sure what else to try, can anyone offer advice?我不确定还有什么可以尝试的,有人可以提供建议吗?
What about experimenting with the PasteSpecial parameter xlPasteValues?尝试使用 PasteSpecial 参数 xlPasteValues 怎么样? Maybe this isn't the right one.也许这不是正确的。
I found sample code #3 in this link and there is no parameter xlPasteValues used:我在此链接中找到了示例代码 #3,并且没有使用参数 xlPasteValues:
https://software-solutions-online.com/vba-range-copy-method-useful/ https://software-solutions-online.com/vba-range-copy-method-useful/
'copy the range from source book
wkbookSource.Worksheets("Range.Copy").Range("B6:F11").Copy
'paste the data on the target book
wkbookTarget.Worksheets(1).Range("A1").PasteSpecial
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.