简体   繁体   English

使用VBA将数据从具有2列的excel表复制和粘贴到具有8列的excel表

[英]copy and paste data from excel table with 2 colums to excel table with 8 columns using VBA

i like to copy table body of a 2 column table in a workbook to the first 2 columns of a 8 column table in another workbook. 我想将工作簿中2列表的表主体复制到另一个工作簿中8列表的前2列。 i wrote this code but when pasting the body to the other table i get the 2 columns repeated on column 3 and 4, 5 and 6 and 7 and 8 我写了这段代码,但是当将主体粘贴到另一个表时,我在第3列,第4列,第5列,第6列,第7列和第8列重复了2列

Dim wbk As Workbook

Sub overzetten_naar_planning()

Dim folderPath As String, fileName As String, filePath As String
Dim LastRow As Variant
Dim Wb As Workbook
Set Wb = ThisWorkbook

' create path containing the planning file
folderPath = ThisWorkbook.Path & "\"
fileName = "6s planning 2015.xlsx"
filePath = folderPath & fileName

' check if planning is already open in your session.
If IsWorkBookOpen(filePath) Then
    Set Wba = Workbooks(fileName)
Else
    Set Wba = Workbooks.Open(filePath, UpdateLinks:=0)
End If

Wba.Activate
Set LastRow = ActiveSheet.ListObjects("Planning6S").ListRows.Add
ThisWorkbook.Activate
ActiveSheet.ListObjects("WerkplaatsTabel").DataBodyRange.Copy
LastRow.Range.PasteSpecial xlPasteValues

End Sub

Function IsWorkBookOpen(fileName As String)
Dim ff As Long, ErrNo As Long

On Error Resume Next
ff = FreeFile()
Open fileName For Input Lock Read As #ff
Close ff
ErrNo = Err
On Error GoTo 0

Select Case ErrNo
Case 0:    IsWorkBookOpen = False
Case 70:   IsWorkBookOpen = True
Case Else: Error ErrNo
End Select
End Function

The reason for the two columns repeating is in your setting of the paste range 两列重复的原因是您设置的粘贴范围

Wba.Activate Set LastRow = ActiveSheet.ListObjects("Planning6S").ListRows.Add

LastRow is a range and your planning6S table spans 8 columns. LastRow是一个范围,您的planning6S表跨越8列。 So, if you copied 2 columns and then pasted them into a 1 row by 8 column range then Excel will repeat the two column clipboard across all 8 selected columns. 因此,如果您复制了2列,然后将它们粘贴到8列范围的1行中,则Excel将在所有8个选定的列上重复两列剪贴板。

I'm pretty sure the problem is in these lines: 我很确定问题出在以下几行:

Set LastRow = ActiveSheet.ListObjects("Planning6S").ListRows.Add

... ...

ActiveSheet.ListObjects("WerkplaatsTabel").DataBodyRange.Copy
LastRow.Range.PasteSpecial xlPasteValues

The first line inserts a new row , which is necessary if the table is empty before the copy, but since your import table is 8 columns wide and your export table only two columns wide, the two columns will be repeated 4 times. 第一行插入新 ,如果复制之前表为空,则这是必需的,但是由于导入表的宽度为8列,而导出表的宽度仅为两列,因此这两列将重复4次。 (In keeping with Mark Fitzgerald's reply). (与马克·菲茨杰拉德的答复一致)。

Try this instead: 尝试以下方法:

Dim LR as variant
Set LR = ActiveSheet.ListObjects("Planning6S").DataBodyRange.Columns("A:B") 'your desired copy range`

ActiveSheet.ListObjects("WerkplaatsTabel").DataBodyRange.Copy LR

将其粘贴到LastRow范围的第一个左上角单元格中是否可行?

LastRow.Cells(1,1).PasteSpecial xlPasteValues

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

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