【问题标题】:How to copy data from closed workbooks (keeping them closed) into master workbook using VBA?如何使用 VBA 将关闭的工作簿中的数据(保持关闭状态)复制到主工作簿中?
【发布时间】:2019-12-01 01:43:06
【问题描述】:

我需要使用 VBA 将数据从已关闭的工作簿中复制到主工作簿中,而无需打开它们。

我在 4-6 个文件中使用 Workbooks.Open。每个需要打开的文件都会大大减慢复制操作。

我需要高效的 VBA 代码来复制数据而无需打开每个文件。

这是我的代码示例:

Set x = Workbooks.Open("C:\Bel.xls")
'Now, copy what you want from x:
x.Sheets("Daily Figures").Range("A13:j102").Copy
'Now, paste to y worksheet
y.Activate
Sheets("Data - Daily").Range("N2").PasteSpecial
'Close x:
Application.CutCopyMode = False
x.Close
Sheets("sheet1").Range("M4") = Date

【问题讨论】:

  • 你试过谷歌吗?执行Excel4Macro
  • 尝试-y.Sheets("Data - Daily").Range("N2").PasteSpecial-然后在打开工作簿之前查看application.screenupdating=false
  • 您通常最好从控制的角度打开工作簿,而不是 xlm 宏的正常“关闭”方法、ADO 和创建直接链接。例如,ADO 可能会遇到混合数据类型的问题
  • 另外,尝试使用 Application.EnableEvents = false 和 Application.EnableEvents = true 。每次进行粘贴时,都会触发一个事件。 Application.EnableEvents = false 可以提高速度。

标签: excel vba copy


【解决方案1】:

试试这个。它使用 ADO 工作,无需打开源文件:

Sub TransferData()
Dim sourceFile As Variant

Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual

sourceFile = "C:\Bel.xls"

GetData sourceFile, "Daily Figures", "A13:j102", Sheets("Data - Daily").Range("N2"), False, False

Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True

End Sub

Public Sub GetData(sourceFile As Variant, SourceSheet As String, _
                   SourceRange As String, TargetRange As Range, Header As Boolean, UseHeaderRow As Boolean)
' 30-Dec-2007, working in Excel 2000-2007
' http://www.rondebruin.nl/ado.htm

Dim rsCon As Object
Dim rsData As Object
Dim szConnect As String
Dim szSQL As String
Dim lCount As Long

' Create the connection string.
If Header = False Then
    If Val(Application.Version) < 12 Then
        szConnect = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
                    "Data Source=" & sourceFile & ";" & _
                    "Extended Properties=""Excel 8.0;HDR=No"";"
    Else
        szConnect = "Provider=Microsoft.ACE.OLEDB.12.0;" & _
                    "Data Source=" & sourceFile & ";" & _
                    "Extended Properties=""Excel 12.0;HDR=No"";"
    End If
Else
    If Val(Application.Version) < 12 Then
        szConnect = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
                    "Data Source=" & sourceFile & ";" & _
                    "Extended Properties=""Excel 8.0;HDR=Yes"";"
    Else
        szConnect = "Provider=Microsoft.ACE.OLEDB.12.0;" & _
                    "Data Source=" & sourceFile & ";" & _
                    "Extended Properties=""Excel 12.0;HDR=Yes"";"
    End If
End If

If SourceSheet = "" Then
    ' workbook level name
    szSQL = "SELECT * FROM " & SourceRange$ & ";"
Else
    ' worksheet level name or range
    szSQL = "SELECT * FROM [" & SourceSheet$ & "$" & SourceRange$ & "];"
End If

On Error GoTo SomethingWrong

Set rsCon = CreateObject("ADODB.Connection")
Set rsData = CreateObject("ADODB.Recordset")

rsCon.Open szConnect
rsData.Open szSQL, rsCon, 0, 1, 1

' Check to make sure we received data and copy the data
If Not rsData.EOF Then

    If Header = False Then
        TargetRange.Cells(1, 1).CopyFromRecordset rsData
    Else
        'Add the header cell in each column if the last argument is True
        If UseHeaderRow Then
            For lCount = 0 To rsData.Fields.Count - 1
                TargetRange.Cells(1, 1 + lCount).Value = _
                rsData.Fields(lCount).Name
            Next lCount
            TargetRange.Cells(2, 1).CopyFromRecordset rsData
        Else
            TargetRange.Cells(1, 1).CopyFromRecordset rsData
        End If
    End If

Else
    MsgBox "No records returned from : " & sourceFile, vbCritical
End If

' Clean up our Recordset object.
rsData.Close
Set rsData = Nothing
rsCon.Close
Set rsCon = Nothing
Exit Sub

SomethingWrong:
    MsgBox "The file name, Sheet name or Range is invalid of : " & sourceFile, _
           vbExclamation, "Error"
    On Error GoTo 0

End Sub

【讨论】:

  • 我使用相同的 GetData 函数从关闭的文件中检索数据。但是,Excel 一直在抱怨“外部表格不是预期的格式”。另一方面,如果我将复制数据的文件保持打开状态,则该函数将正常工作。
  • 由于某种原因,GetData 不适用于 Excel12 (.xlsx) 扩展
  • 也许它应该检查文件类型是否为 xlsx 以确定使用哪个连接字符串。
猜你喜欢
  • 1970-01-01
  • 2017-02-22
  • 2014-02-24
  • 2023-02-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-06-12
  • 1970-01-01
相关资源
最近更新 更多