it-swarm.cn

通过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           

如果它是我将退出WhileLoop,如果它不是我将复制一切。实际上这不会像我想的那样工作,但我找不到合适的算法也不会有问题。

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

15
Andrei Ion

你可能喜欢函数 GetInfoFromClosedFile()


编辑:由于以上链接似乎不再起作用,我正在添加 备用链接1备用链接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
7
Patrick Honorez

将数据从工作簿复制到另一个工作簿的最佳(也是最简单)方法是使用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
14
JMax

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

Application.EnableEvents = False
Application.ScreenUpdating = False

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

Application.DisplayAlerts = False
Application.Calculation = xlManual

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

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

以下是使用多个Excel文件的一些 专家指南 概述了可用于引用数据的不同方法

扩展问题是如何循环遍历同一文件夹中包含的多个文件。您可以使用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

希望上面的一些将是有用的

5
Ed Bolton

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

Dim wkbk As Workbook

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

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

4
jonsca

我有同样的问题,但应用提供的解决方案更改了要写入的文件。一旦我选择了新的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作为外部工作表中的工作表。

0
Alex_P