2015-09-05 111 views
-2

我是新来的VBA和我需要帮助与以下:汇入动态范围从一个Excel工作簿到另一个Excel工作簿

我有3个Excel工作簿(WB1,WB2和WB3)。 wb1和wb2都包含列A(日期)和列B(数字)。在每月的基础上,新的数据被添加到这两列。 wb1和wb2存储在同一个文件夹中。

在wb3中,我根据wb1或wb2的数据执行计算。因此,我需要在wb3的下拉列表中选择相应的工作簿(wb1或wb2)。我选择的动态数据范围应该被复制到wb3中。

回答

0

的设立

两个工作簿保存在工作簿.XLSM一个文件夹

enter image description here

数据验证英寸

enter image description here

关闭的.xlsx工作簿,运行代码之前。该代码将打开并关闭选定的工作簿。

选择工作簿,然后单击按钮

enter image description here

Sub Button3_Click() 
    Dim wb As Workbook 
    Dim ws As Worksheet 
    Dim lstRw As Long 
    Dim rng As Range 
    Dim GetRng As String 
    Dim MyDir As String 
    Dim MyFile As String 

    Set wb = ThisWorkbook 
    Set ws = wb.Sheets("Sheet1") 
    GetRng = ws.Range("A2") 

    MyDir = "C:\Users\Dave\Downloads\SampleFolder\" 'change folder location" 
    MyFile = Dir(MyDir & GetRng & "*.xlsx") 
    ChDir MyDir 

    Application.ScreenUpdating = 0 
    Application.DisplayAlerts = 0 

    Workbooks.Open (MyFile) 

    With Worksheets("Sheet1") 

     lstRw = .Cells(Rows.Count, "A").End(xlUp).Row 
     Set rng = Range(.Cells(2, 1), .Cells(lstRw, 2)) 
     rng.Copy ws.Cells(Rows.Count, "A").End(xlUp).Offset(1, 0) 
     ActiveWorkbook.Close True 

    End With 

    Application.DisplayAlerts = 1 
    MyFile = Dir() 


End Sub 
相关问题