简体   繁体   English

如何自动更新 excel 链接数据?

[英]How to auto update excel linked data?

I have a source excel file(source) linked to a folder of files(templates), and all the templates are eventually linked to the output file(output).我有一个源 excel 文件(源)链接到文件(模板)的文件夹,所有模板最终都链接到 output 文件(输出)。 So if there are number updated in the source file, I need to open the templates one by one and refresh each template, and then refresh the output file to reflect the updates in all the template files.所以如果源文件中有更新的编号,我需要一个一个打开模板并刷新每个模板,然后刷新output文件,以反映所有模板文件中的更新。 I am wondering if there's a way to make this process more efficient?我想知道是否有办法让这个过程更有效率?

Thanks!谢谢!

I can't give a specific answer, but I can give you some tips that may hopefully allow you to create the program that you're looking for.我不能给出具体的答案,但我可以给你一些提示,希望它们能让你创建你正在寻找的程序。

To open a workbook in VBA you want to use Application.Workbooks.Open .要在 VBA 中打开工作簿,您需要使用Application.Workbooks.Open To use that method, you need to know the exact file name and location.要使用该方法,您需要知道确切的文件名和位置。 If you have a lot of files, you may want to define directories instead of individual file paths.如果您有很多文件,您可能想要定义目录而不是单个文件路径。 That would be done like so:可以这样做:

Dim Directory as Object

Set Directory = CreateObject("Scripting.FileSystemObject").getfolder(DirectoryPath)
'DirectoryPath is the folder location written like "C:\Users\Guy\Documents\My Received Files\"

You can then loop through the files in the folder like so:然后,您可以像这样遍历文件夹中的文件:

Dim oFile As Variant

For Each oFile In Directory.Files
    'Example of how to open them
    If oFile.Name Like "*.xls?" Then Application.Workbooks.Open oFile.Path
Next oFile

When you do Application.Workbooks.Open you can also save that workbook as a reference so that you can update the data inside it.当您执行 Application.Workbooks.Open 时,您还可以将该工作簿保存为参考,以便您可以更新其中的数据。 You would do it like so:你会这样做:

Dim wb As Workbook

Set wb = Application.Workbooks.Open(FilePath)
'FilePath is the specific location of the File

Once you have the workbook, you can reference whichever cells you want using references like.Sheets("Sheet1").Range("A1").获得工作簿后,您可以使用诸如.Sheets("Sheet1").Range("A1") 之类的引用来引用您想要的任何单元格。

To put everything I've discussed above into one example:将我上面讨论的所有内容放在一个示例中:

Sub Open_and_Edit()

    Dim Directory As Object, oFile As Variant, wb As Workbook, DirectoryPath As String

    'For this example, I'll use my desktop as the Directory
    DirectoryPath = "C:\Users\Michel\Desktop\"

    Set Directory = CreateObject("Scripting.FileSystemObject").getfolder(DirectoryPath)

    For Each oFile In Directory.Files
        'Opening every excel file on my desktop
        If oFile.Name Like "*.xls?" Then
            Set wb = Application.Workbooks.Open(oFile.Path)
            
            'opening and closing a dozen workbooks can cause annoying screen jittering, my prefered fix:
            wb.Windows(1).Visible = False
            
            wb.Sheets(1).Range("A1") = "Hello World!"
            
            wb.Close savechanges:=True
        End If
    Next oFile

End Sub

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

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