简体   繁体   English

从下一个Excel工作表VBA复制和粘贴数据

[英]Copy and paste data from the next Excel sheet VBA

I'm a complete beginner with VBA and I looked at other topics but couldn't really find an answer (at least that I could understand). 我是VBA的入门者,我看了其他主题,但找不到真正的答案(至少我能理解)。

Basically I have Excel workbooks for different subjects during a research project. 基本上,我在研究项目中有针对不同主题的Excel工作簿。

I want to copy past some data from the subjects workbook and past it to a master Workbook. 我想从主题工作簿复制过去的某些数据,然后将其复制到主工作簿。 This code does it for one subject. 这段代码只针对一个主题。

It open the selected workbook, copy past what is needed in the right place then close it then open the next condition etc. 它打开选定的工作簿,在正确的位置复制过去需要的内容,然后关闭它,然后打开下一个条件,依此类推。

I'd like to find a way to tell it to open the next workbook (ie: Patient1GlobalP.xlsm becomes Patient2GlobalP.xlsm, then 3 then 4...) and do the same but copy to the next cell (the range that is copied stay the same but Range("C2").Select becomes Range("D2").Select for the copy destination, C1509 becomes D1509 and C3016 becomes D3016 then E then F...). 我想找到一种方法来告诉它打开下一个工作簿(即:Patient1GlobalP.xlsm变成Patient2GlobalP.xlsm,然后是3再是4 ...),并执行相同的操作,但是复制到下一个单元格(范围是复制的内容保持不变,但Range("C2").Select变为Range("D2").Select复制目标,C1509变为D1509,C3016变为D3016,然后E然后F ...)。 I can do it by hand but there has to be a more elegant solution like some kind of offset. 我可以手工完成,但必须有一个更优雅的解决方案,例如某种补偿。

Windows("Patient1GlobalP.xlsm").Activate
    Range("B1:B1505").Select
    Selection.Copy
    Windows("master.xlsx").Activate
    Sheets("Ankle X").Select
    Range("C2").Select
    ActiveSheet.Paste
    Application.CutCopyMode = False
    Windows("Patient1GlobalP.xlsm").Activate
    Range("C1:C1505").Select
    Selection.Copy
    Windows("master.xlsx").Activate
    Sheets("Ankle Y").Select
    Range("C2").Select
    ActiveSheet.Paste
    Application.CutCopyMode = False
    Windows("Patient1GlobalP.xlsm").Activate
    Windows("Patient1GlobalP.xlsm").Activate
    Range("C1:C1505").Select
    Selection.Copy
    Windows("master.xlsx").Activate
    Sheets("Ankle Z").Select
    Range("C2").Select
    ActiveSheet.Paste
    Application.CutCopyMode = False
    Windows("Patient1GlobalP.xlsm").Activate
    Range("N1:N1505").Select
    Selection.Copy
    Windows("master.xlsx").Activate
    Sheets("Knee X").Select
    Range("C2").Select
    ActiveSheet.Paste
    Application.CutCopyMode = False
        Windows("Patient1GlobalP.xlsm").Activate
    Range("O1:O1505").Select
    Selection.Copy
    Windows("master.xlsx").Activate
    Sheets("Knee Y").Select
    Range("C2").Select
    ActiveSheet.Paste
    Application.CutCopyMode = False
        Windows("Patient1GlobalP.xlsm").Activate
    Range("P1:P1505").Select
    Selection.Copy
    Windows("master.xlsx").Activate
    Sheets("Knee Z").Select
    Range("C2").Select
    ActiveSheet.Paste
    Application.CutCopyMode = False
        Windows("Patient1GlobalP.xlsm").Activate
    Range("I1:I1505").Select
    Selection.Copy
    Windows("master.xlsx").Activate
    Sheets("Hip X").Select
    Range("C2").Select
    ActiveSheet.Paste
    Application.CutCopyMode = False
    Windows("Patient1GlobalP.xlsm").Activate
    Range("J1:J1505").Select
    Selection.Copy
    Windows("master.xlsx").Activate
    Sheets("Hip Y").Select
    Range("C2").Select
    ActiveSheet.Paste
    Application.CutCopyMode = False
    Windows("Patient1GlobalP.xlsm").Activate
    Range("J1:J1505").Select
    Selection.Copy
    Windows("master.xlsx").Activate
    Sheets("Hip Z").Select
    Range("C2").Select
    ActiveSheet.Paste
    Application.CutCopyMode = False
Workbooks.Open Filename:="C:\Users\hugo\Desktop\data\Patient1LocalP.xlsx"
    Windows("Patient1LocalP.xlsx").Activate
    Range("B1:B1505").Select
    Selection.Copy
    Windows("master.xlsx").Activate
    Sheets("Ankle X").Select
    Range("C1509").Select
    ActiveSheet.Paste
    Application.CutCopyMode = False
    Windows("Patient1LocalP.xlsx").Activate
    Range("C1:C1505").Select
    Selection.Copy
    Windows("master.xlsx").Activate
    Sheets("Ankle Y").Select
    Range("C1509").Select
    ActiveSheet.Paste
    Application.CutCopyMode = False
    Windows("Patient1LocalP.xlsx").Activate
    Windows("Patient1LocalP.xlsx").Activate
    Range("C1:C1505").Select
    Selection.Copy
    Windows("master.xlsx").Activate
    Sheets("Ankle Z").Select
    Range("C1509").Select
    ActiveSheet.Paste
    Application.CutCopyMode = False
    Windows("Patient1LocalP.xlsx").Activate
    Range("N1:N1505").Select
    Selection.Copy
    Windows("master.xlsx").Activate
    Sheets("Knee X").Select
    Range("C1509").Select
    ActiveSheet.Paste
    Application.CutCopyMode = False
        Windows("Patient1LocalP.xlsx").Activate
    Range("O1:O1505").Select
    Selection.Copy
    Windows("master.xlsx").Activate
    Sheets("Knee Y").Select
    Range("C1509").Select
    ActiveSheet.Paste
    Application.CutCopyMode = False
        Windows("Patient1LocalP.xlsx").Activate
    Range("P1:P1505").Select
    Selection.Copy
    Windows("master.xlsx").Activate
    Sheets("Knee Z").Select
    Range("C1509").Select
    ActiveSheet.Paste
    Application.CutCopyMode = False
        Windows("Patient1LocalP.xlsx").Activate
    Range("I1:I1505").Select
    Selection.Copy
    Windows("master.xlsx").Activate
    Sheets("Hip X").Select
    Range("C1509").Select
    ActiveSheet.Paste
    Application.CutCopyMode = False
    Windows("Patient1LocalP.xlsx").Activate
    Range("J1:J1505").Select
    Selection.Copy
    Windows("master.xlsx").Activate
    Sheets("Hip Y").Select
    Range("C1509").Select
    ActiveSheet.Paste
    Application.CutCopyMode = False
    Windows("Patient1LocalP.xlsx").Activate
    Range("J1:J1505").Select
    Selection.Copy
    Windows("master.xlsx").Activate
    Sheets("Hip Z").Select
    Range("C1509").Select
    ActiveSheet.Paste
    Application.CutCopyMode = False
    Windows("Patient1LocalP.xlsx").Activate
    ActiveWindow.Close
    Workbooks.Open Filename:="C:\Users\hugo\Desktop\data\Patient1Nopert.xlsx"
    Windows("Patient1Nopert.xlsx").Activate
    Range("B1:B1505").Select
    Selection.Copy
    Windows("master.xlsx").Activate
    Sheets("Ankle X").Select
    Range("C3016").Select
    ActiveSheet.Paste
    Application.CutCopyMode = False
    Windows("Patient1Nopert.xlsx").Activate
    Range("C1:C1505").Select
    Selection.Copy
    Windows("master.xlsx").Activate
    Sheets("Ankle Y").Select
    Range("C3016").Select
    ActiveSheet.Paste
    Application.CutCopyMode = False
    Windows("Patient1Nopert.xlsx").Activate
    Windows("Patient1Nopert.xlsx").Activate
    Range("C1:C1505").Select
    Selection.Copy
    Windows("master.xlsx").Activate
    Sheets("Ankle Z").Select
    Range("C3016").Select
    ActiveSheet.Paste
    Application.CutCopyMode = False
    Windows("Patient1Nopert.xlsx").Activate
    Range("N1:N1505").Select
    Selection.Copy
    Windows("master.xlsx").Activate
    Sheets("Knee X").Select
    Range("C3016").Select
    ActiveSheet.Paste
    Application.CutCopyMode = False
        Windows("Patient1Nopert.xlsx").Activate
    Range("O1:O1505").Select
    Selection.Copy
    Windows("master.xlsx").Activate
    Sheets("Knee Y").Select
    Range("C3016").Select
    ActiveSheet.Paste
    Application.CutCopyMode = False
        Windows("Patient1Nopert.xlsx").Activate
    Range("P1:P1505").Select
    Selection.Copy
    Windows("master.xlsx").Activate
    Sheets("Knee Z").Select
    Range("C3016").Select
    ActiveSheet.Paste
    Application.CutCopyMode = False
        Windows("Patient1Nopert.xlsx").Activate
    Range("I1:I1505").Select
    Selection.Copy
    Windows("master.xlsx").Activate
    Sheets("Hip X").Select
    Range("C3016").Select
    ActiveSheet.Paste
    Application.CutCopyMode = False
    Windows("Patient1Nopert.xlsx").Activate
    Range("J1:J1505").Select
    Selection.Copy
    Windows("master.xlsx").Activate
    Sheets("Hip Y").Select
    Range("C3016").Select
    ActiveSheet.Paste
    Application.CutCopyMode = False
    Windows("Patient1Nopert.xlsx").Activate
    Range("J1:J1505").Select
    Selection.Copy
    Windows("master.xlsx").Activate
    Sheets("Hip Z").Select
    Range("C3016").Select
    ActiveSheet.Paste
    Application.CutCopyMode = False
    Windows("Patient1Nopert.xlsx").Activate
    ActiveWindow.Close

Here's one possible approach: 这是一种可能的方法:

Sub Tester()

    Const SRC_FLDR As String = "C:\something\myfiles\"

    Dim wbM As Workbook, wbS As Workbook, shtS As Worksheet
    Dim i As Long, colOff As Long

    Set wbM = Workbooks("master.xlsx")

    colOff = 0 'paste position offset

    For i = 1 To 10 'eg
        Set wbS = Workbooks.Open(SRC_FLDR & "Patient" & i & "GlobalP.xlsm")
        Set shtS = wbS.Sheets(1)

        'copy ranges to specific positions on Master sheets
        '  offset changes each time through the loop
        'NOTE: no need to use Activate/Select here
        shtS.Range("B1:B1505").Copy wbS.Sheets("Ankle X").Range("C2").Offset(0, colOff)
        shtS.Range("C1:C1505").Copy wbS.Sheets("Ankle Y").Range("C2").Offset(0, colOff)
        shtS.Range("C1:C1505").Copy wbS.Sheets("Ankle Z").Range("C2").Offset(0, colOff)
        'etc...
        'etc...

        wbS.Close savechanges:=False
        colOff = colOff + 1

    Next i

End Sub

暂无
暂无

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

相关问题 使用VBA excel从一张工作表中复制表格并将其粘贴到另一张工作表的下一个空行中 - copy a table from one Sheet and paste it in the next empty row of another Sheet using VBA excel 使用excel VBA将数据从一张纸复制并粘贴到另一张纸,然后从第二张纸复制到第三张纸 - Copy and paste data from one a sheet to another sheet, and from second sheet to third using excel VBA Excel VBA查找行,复制内容,粘贴在下一张工作表中,然后删除原始数据 - Excel VBA Find Row, copy contents, paste in next sheet then delete original data 如果条件匹配(但不是+1),excel vba从另一个工作表复制/粘贴数据 - excel vba to copy/paste data fron another sheet if conditions match (but not +1) excel vba如果条件匹配,则从另一张表复制/粘贴数据 - excel vba to copy/paste data fron another sheet if conditions match VBA 将数据从 Project 复制粘贴到 Excel - VBA Copy Paste Data into Excel from Project 在Excel VBA中将数据从一张纸移动到另一张纸而无需复制粘贴 - Moving data from one sheet to another without copy paste in Excel VBA 使用Excel VBA中的自动筛选器从Excel工作表剪切粘贴数据 - Cut Paste data from excel sheet using Autofilter in Excel VBA Excel VBA自动筛选,复制并粘贴到命名工作表 - Excel VBA autofilter, copy and paste to named sheet VBA代码将某些数据从工作表复制并粘贴到另一个 - VBA code to copy and paste certain data from sheet to another
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM