简体   繁体   English

如何自动将数据输入从一个工作簿复制到另一个工作簿,而无需激活宏或打开工作簿

[英]How to automatically copy data entry from 1 workbook into another without having to activate macro or open workbook

I have come across a problem while trying to do the following: 我在尝试执行以下操作时遇到了一个问题:

I have 2 workbooks 1 called Master and the other one called Slave. 我有2本工作簿,其中1本叫做Master,另一本叫做Slave。 Master has a worksheet called Data where data is inputted into it automatically every so often. Master有一个名为Data的工作表,其中的数据经常自动输入其中。 Data is inputted into cells ranging from A8 to BL8. 数据输入到A8到BL8范围的单元中。 Every time new data is stored, it pushes the old data downwards and saves it into again A8:BL8. 每次存储新数据时,它会将旧数据向下推,然后再次将其保存到A8:BL8中。 Slave has a worksheet called DATA with the exact same format. 从站有一个称为DATA的工作表,格式完全相同。

My goal is the following: 我的目标是:

I want to write a VBA code where each time new data is inputted into(USUALLY 6-7 NEW DATA ENTRIES PER 24 HOURS) Master workbook(Worksheet called Data), I want it to update the same information onto the Slave workbook (worksheet called DATA) without having to even open or run the workbook(Data can be inputted at random times throughout the day so I am not always around to keep track and need it to transfer data on its own). 我想编写一个VBA代码,每次将新数据输入(每24小时通常6-7个新数据条目)主工作簿(称为“数据表”的工作表),我希望它将相同的信息更新到从属工作簿(称为“工作表”的工作表)上。 DATA)而不必打开或运行工作簿(数据可以在一天中的任意时间输入,因此我并不总是在跟踪并需要它自己传输数据)。

Both workbooks are saved in different locations on my computer. 这两个工作簿都保存在我的计算机上的不同位置。 My attempt worked, however I was only able to get it to work with copying the current values in the cell range A8:BL8 and moving it over to the workbook I want by triggering the macro with a button(inserting)... (I hope my problem is clear) so it only updates the very latest data and not ALL the data. 我的尝试奏效了,但是我只能通过使用按钮(插入)触发宏来将它复制到单元格区域A8:BL8中的当前值,并将其移到我想要的工作簿上来工作...(我希望我的问题很清楚),因此它只会更新最新的数据,而不会更新所有数据。

My goal eventually is to be able to do this between many workbooks with the same format (into the same workbook Slave(worksheet DATA)) 我的目标最终是要能够在许多具有相同格式的工作簿之间(在同一个工作簿Slave(worksheet DATA)中进行此操作)

My Code runs from workbook Slave (worksheet DATA) via a button assigned to this macro. 我的代码通过分配给该宏的按钮从工作簿从属(工作表DATA)运行。

Sub OpenCopyPaste()

    Dim wb As Workbook
    Dim Reportwb As Workbook
    Set Reportwb = Workbooks("Slave.xlsm")
    Set wb = Workbooks.Open(Filename:="C:\Users\yilmadu001          \Desktop\Master.xlsm")
    wb.Sheets("Data").Range("A8:BL8").Copy
    Reportwb.Sheets("DATA").Range("A8:BL8").Insert Shift:=xlDown
    Reportwb.Save
    wb.Close False
    Application.ScreenUpdating = True
End Sub

As per your comment, to run your code before save put the following into the ThisWorkbook object. 根据您的评论,要在保存之前运行代码,请将以下内容放入ThisWorkbook对象。

Private Sub Workbook_BeforeSave(Cancel As Boolean)

    Dim wb As Workbook
    Dim Reportwb As Workbook
    Set Reportwb = Workbooks("Slave.xlsm")
    Set wb = Workbooks.Open(Filename:="C:\Users\yilmadu001          \Desktop\Master.xlsm")
    wb.Sheets("Data").Range("A8:BL8").Copy
    Reportwb.Sheets("DATA").Range("A8:BL8").Insert Shift:=xlDown
    Reportwb.Save
    wb.Close False
    Application.ScreenUpdating = True

End Sub

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

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