简体   繁体   English

Excel VBA-将两列从一个工作簿复制并粘贴到另一个工作簿

[英]Excel VBA - Copy and Paste two columns from one workbook into another

I'm new to vba. 我是vba的新手。 But, I have a big problem we're trying to solve at work. 但是,我有一个大问题,我们正在努力解决。 We receive payments for services that come through a commuter pretax deduction program. 我们通过通勤者税前扣除计划收取服务费用。 This is usually around 160+ payments that we have to enter into three systems. 通常,我们必须输入三个系统才能支付大约160多种付款。 I've built a spreadsheet that cuts down on errors when we enter deposits. 我建立了一个电子表格,可以减少我们输入存款时的错误。 But, I'm trying to import the payments from the commuter program into my spreadsheet to cut out about an hour(or two) of data entry. 但是,我试图将通勤者程序中的付款导入到电子表格中,以减少大约一小时(或两个小时)的数据输入。 The spreadsheet that I'm trying to import it to is an excel table. 我要导入到的电子表格是一个excel表。 I'm trying to copy and paste two columns. 我正在尝试复制和粘贴两列。 I would like column D(Account #'s) in the "WAGEWORKS IMPORT" spreadsheet to only copy the used cells in column G and paste them into the active workbook in column B, and, copy the used cells in Column D($ Amt. of Payments) and paste them into the active workbook in Column I(The active workbook is ThisWorkbook-the bookkeeper will use a command button that I will assign later after I perfect the code) I can only get it to copy the cells from column G and copy them into the cells in Column B. The cells from Column D are getting pasted into column I, however they are doing so at the end of the table which is 600+ rows below where I need them. 我希望“ WAGEWORKS IMPORT”电子表格中的D列(帐户编号)仅复制G列中使用的单元格并将它们粘贴到B列中的活动工作簿中,并复制D列中的使用过的单元格($ Amt并将其粘贴到第I列中的活动工作簿中(活动工作簿为ThisWorkbook-簿记员将使用命令按钮,在完善代码后,我将在稍后分配该命令按钮),我只能获取它来复制列中的单元格G并将它们复制到B列中的单元格中。D列中的单元格将粘贴到I列中,但是它们是在表的末尾进行的,该表位于我需要它们的下方600多个行。 I need the corresponding payments to match the account numbers in the same row. 我需要相应的付款以匹配同一行中的帐号。 My code is below. 我的代码如下。 Can anyone help? 有人可以帮忙吗?

Sub Wageworks_Import()
Application.ScreenUpdating = False

Dim lastrow As Long, erow As Long


Set x = Workbooks.Open("J:\Accounting - Copy\Accounting Projects\Wageworks Import\WAGEWORKS IMPORT.xlsx")

Workbooks.Open("J:\Accounting - Copy\Accounting Projects\Wageworks Import\WAGEWORKS IMPORT.xlsx").Activate


Sheets("index").Range("G10:G100").Copy


ThisWorkbook.Activate
Sheets("ENTRY").Select
Set lastCell = ActiveSheet.Cells(Rows.Count, "B").End(xlUp)
If IsEmpty(lastCell.Value) Then
  Set lastCell = lastCell.End(xlUp)
End If
lastrow = lastCell.Row + 1
Range("B" & lastrow).Select
Selection.PasteSpecial xlPasteValues


Workbooks.Open("J:\Accounting - Copy\Accounting Projects\Wageworks Import\WAGEWORKS IMPORT.xlsx").Activate


Sheets("index").Range("D10:D100").Copy

ThisWorkbook.Activate
Sheets("ENTRY").Select
Set lastCell = ActiveSheet.Cells(Rows.Count, "I").End(xlUp)
If IsEmpty(lastCell.Value) Then
  Set lastCell = lastCell.End(xlUp)
End If
lastrow = lastCell.Row + 1
Range("I" & lastrow).Select
Selection.PasteSpecial xlPasteValues


Application.CutCopyMode = False
Sheet1.Columns.AutoFit

Application.ScreenUpdating = True

End Sub

This does the same you do, but shorter and more efficient. 这样做与您相同,但更短,更有效。 It will paste the data always into the same rows. 它将数据始终粘贴到相同的行中。

    Sub Wageworks_Import()
Application.ScreenUpdating = False

Dim lrM, lrF, erow As Long
Dim wbk As Workbook
Dim sht, sht2 As Worksheet

Set wbk = Workbooks.Open("J:\Accounting - Copy\Accounting Projects\Wageworks Import\WAGEWORKS IMPORT.xlsx")
Set sht = wbk.Worksheets("index")
Set sht2 = ThisWorkbook.Worksheets("ENTRY")

lrF = sht.Cells(Rows.Count, 7).End(xlUp).Row
lrM = sht2.Range("B:B").Find("*", SearchDirection:=xlPrevious).Row

sht.Range(Cells(10, 7), Cells(lrF, 7)).Copy _
Destination:=sht2.Range("B" & lrM + 1)

sht.Range(Cells(10, 4), Cells(lrF, 4)).Copy _
Destination:=sht2.Range("I" & lrM + 1)

Application.CutCopyMode = False
sht2.Columns.AutoFit

wbk.Close saveChanges:=False
Application.ScreenUpdating = True
End Sub

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

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