简体   繁体   English

使用VBA,如何在不打开xlsx文件的情况下将xlsx文件导入xlsm?

[英]With VBA, how to import a xlsx file into a xlsm without opening the xlsx file?

This is part of my current code: 这是我当前代码的一部分:

Option Explicit 显式期权

Sub ImportData()

    Dim wkbCrntWorkBook As Workbook
    Dim wkbSourceBook   As Workbook
    Dim fNameAndPath As Variant

    Set wkbCrntWorkBook = ActiveWorkbook
    fNameAndPath = Application.GetOpenFilename(FileFilter:="Excel 2007, *.xlsx; *.xlsm; *.xlsa", Title:="Select File To Import")
    If fNameAndPath = False Then Exit Sub
    Call ReadDataFromCloseFile(fNameAndPath)


    Set wkbCrntWorkBook = Nothing
    Set wkbSourceBook = Nothing

End Sub

Sub ReadDataFromCloseFile(filePath As Variant)
    On Error GoTo ErrHandler
    Application.ScreenUpdating = False

    Dim src As Workbook

    Set src = Workbooks.Open(filePath, True, True)

' COPY DATA FROM SOURCE (CLOSE WORKGROUP) TO THE DESTINATION WORKBOOK.
' GET THE TOTAL ROWS FROM THE SOURCE WORKBOOK.
Dim lastLine As Long   ' last line from source
lastLine = src.Worksheets(source_sheet_1_name).Range("A" & Rows.count).End(xlUp).Row
Worksheets("abc").Range("A3:A40")).Value= src.Worksheets("cde").Range("A4:A41").Value


' CLOSE THE SOURCE FILE.
src.Close False             ' FALSE - DON'T SAVE THE SOURCE FILE.
Set src = Nothing

ErrHandler:
    Application.EnableEvents = True
    Application.ScreenUpdating = True
End Sub

According to this , the source workbook of the data will not be opened. 根据 ,数据源工作簿将无法打开。 However, as soon as I clicked the file name in the file browser to select the file to be imported, excel opened the file. 但是,在文件浏览器中单击文件名以选择要导入的文件后,excel便打开了该文件。

Does anyone know how I should achieve the objective (regardless of the link in the last paragraph)? 有谁知道我应该如何实现目标(无论上一段中的链接如何)?

(My objective is to copy/import certain columns from an xlsx file to xlsm file according to certain criterien automatically after the user indicate the file to be imported (without opening the file to be imported). However, after I select the file to be imported by double click, the file to be imported is merely opened. The program does not carry forward to import the corresponding columns after the file to be imported is open.) (我的目标是在用户指示要导入的文件(不打开要导入的文件)之后,根据某些标准自动将xlsx文件中的某些列复制/导入到xlsm文件中。但是,在选择文件后双击导入,仅打开要导入的文件。打开要导入的文件后,程序不继续导入相应的列。)

There are a few things you can do here. 您可以在这里做几件事。 The file needs to be opened, one way or another. 需要以一种或另一种方式打开文件。 But making it as locked down/hidden as possible will be the key. 但是,使其尽可能地锁定/隐藏将是关键。

You can try a few different things: 您可以尝试一些不同的方法:

1) Before you open the workbook, set Application.ScreenUpdating = False , and then at the end before closing out of the sub, make sure you set it back to True . 1)在打开工作簿之前,设置Application.ScreenUpdating = False ,然后在关闭子项之前最后确保将其设置回True

2) After opening the workbook with src = workbooks.open... try setting Application.Visible = False . 2)使用src = workbooks.open...打开工作src = workbooks.open...尝试设置Application.Visible = False Once you are finished with the import process, and closing out of the source workbook, make sure you set it back to true: Application.Visible = True . 完成导入过程并关闭源工作簿后,请确保将其设置为true: Application.Visible = True

This should allow you to open the workbook, keep the updating off so no one can see what happens, and close out of the workbook with minimal resistance/visibility. 这应该允许您打开工作簿,保持更新状态,以便没人能看到发生了什么,并以最小的阻力/可见性关闭工作簿。

The reason it might be causing an error, or just outright not working is possibly from the workbook sheet name you are trying to call. 可能导致错误或完全无法正常工作的原因可能来自您尝试调用的工作簿工作表名称。 I do not see the assignment of the variable, so lastLine = src.Worksheets(source_sheet_1_name) should actually say the sheetname in it, such as lastLine = src.Worksheets("Sheet1") 我看不到变量的赋值,因此lastLine = src.Worksheets(source_sheet_1_name)实际上应该在其中说出工作表名称,例如lastLine = src.Worksheets("Sheet1")

Give this a shot and let me know. 试一下,让我知道。

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

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