通过VBA从另一个工作簿复制数据

时间:2011-09-13 12:36:51

标签: excel vba excel-vba

伙计们这就是我想做的事情,我在做这件事时遇到了一些麻烦。 我有1个工作簿,我想从不同的文件中收集数据。

Do While THAT_DIFFERENT_FILE_SOMEWHERE_ON_MY_HDD.Cells(Rand, 1).Value <> "" And Rand < 65536
        then 'I will search if the last row in my main worksheet is in this file... 
End Loop           

如果是,我将退出 While 循环,如果不是,我会复制所有内容。实际上这不会像我想的那样工作,但我找不到合适的算法也不会有问题。

我的问题是我不知道如何访问不同的工作簿。

5 个答案:

答案 0 :(得分:14)

将数据从工作簿复制到另一个工作簿的最佳(也是最简单)方法是使用Excel的对象模型。

Option Explicit
Sub test()
    Dim wb As Workbook, wb2 As Workbook
    Dim ws As Worksheet
    Dim vFile As Variant

    'Set source workbook
    Set wb = ActiveWorkbook
    'Open the target workbook
    vFile = Application.GetOpenFilename("Excel-files,*.xls", _
        1, "Select One File To Open", , False)
    'if the user didn't select a file, exit sub
    If TypeName(vFile) = "Boolean" Then Exit Sub
    Workbooks.Open vFile
    'Set targetworkbook
    Set wb2 = ActiveWorkbook

    'For instance, copy data from a range in the first workbook to another range in the other workbook
    wb2.Worksheets("Sheet2").Range("C3:D4").Value = wb.Worksheets("Sheet1").Range("A1:B2").Value
End Sub

答案 1 :(得分:7)

您可能喜欢函数GetInfoFromClosedFile()


修改:由于以上链接似乎不再有效,我正在添加alternate link 1alternate link 2 +代码:

Private Function GetInfoFromClosedFile(ByVal wbPath As String, _
    wbName As String, wsName As String, cellRef As String) As Variant
Dim arg As String
    GetInfoFromClosedFile = ""
    If Right(wbPath, 1) <> "" Then wbPath = wbPath & ""
    If Dir(wbPath & "" & wbName) = "" Then Exit Function
    arg = "'" & wbPath & "[" & wbName & "]" & _
        wsName & "'!" & Range(cellRef).Address(True, True, xlR1C1)
    On Error Resume Next
    GetInfoFromClosedFile = ExecuteExcel4Macro(arg)
End Function

答案 2 :(得分:5)

没有理由不在Excel中打开多个工作簿。关键的代码行是:

Application.EnableEvents = False
Application.ScreenUpdating = False

...然后在代码运行时您将看不到任何内容,并且不会运行与第二个工作簿的打开相关联的代码。然后有......

Application.DisplayAlerts = False
Application.Calculation = xlManual

...以便阻止您获得与第二个文件的内容相关联的弹出消息,并避免任何缓慢的重新计算。确保在编程结束时重新设置为True / xlAutomatic

如果打开第二个工作簿不会导致性能问题,您也可以这样做。事实上,如果某些辅助文件不符合预期的格式,那么打开第二个工作簿会使尝试调试代码时非常有用

以下是一些expert guidance on using multiple Excel files,概述了可用于引用数据的不同方法

扩展问题是如何循环浏览同一文件夹中包含的多个文件。您可以使用:

来使用Windows文件夹选择器
With Application.FileDialog(msoFileDialogFolderPicker)
.Show
     If .Selected.Items.Count = 1 the InputFolder = .SelectedItems(1)
End With

FName = VBA.Dir(InputFolder)

Do While FName <> ""
'''Do function here
FName = VBA.Dir()
Loop

希望上面的一些内容可以使用

答案 3 :(得分:4)

您是否在寻找打开它们的语法:

Dim wkbk As Workbook

Set wkbk = Workbooks.Open("C:\MyDirectory\mysheet.xlsx")

然后,您可以使用wkbk.Sheets(1).Range("3:3")(或任何您需要的)

答案 4 :(得分:0)

我有一个相同的问题,但是应用提供的解决方案将文件写入。一旦选择了新的excel文件,我也会在该文件中进行写入,而不是在原始文件中进行写入。我对此问题的解决方案如下:

Sub GetData()

    Dim excelapp As Application
    Dim source As Workbook
    Dim srcSH1 As Worksheet
    Dim sh As Worksheet
    Dim path As String
    Dim nmr As Long
    Dim i As Long

    nmr = 20

    Set excelapp = New Application

    With Application.FileDialog(msoFileDialogOpen)
        .AllowMultiSelect = False
        .Filters.Add "Excel Files", "*.xlsx; *.xlsm; *.xls; *.xlsb", 1
        .Show
        path = .SelectedItems.Item(1)
    End With

    Set source = excelapp.Workbooks.Open(path)
    Set srcSH1 = source.Worksheets("Sheet1")
    Set sh = Sheets("Sheet1")

    For i = 1 To nmr
        sh.Cells(i, "A").Value = srcSH1.Cells(i, "A").Value
    Next i

End Sub

使用excelapp将调用一个新的应用程序。 with块设置外部文件的路径。最后,我将sourcesrcSH1设置为外部工作簿,作为外部工作表中的工作表。