如何比较两个工作簿

时间:2016-04-14 12:10:18

标签: excel vba excel-vba macros

我喜欢将两个工作簿与不同的工作表进行比较,如何设置为工作簿1的对象 - > Sheet1,workbook2 --->(sheet1)

我可以比较同一工作簿中的工作表,但好像我想选择工作表“getopenfilename”。如何将名称指定为对象。

代码:

Dim tabWb As Workbook            'Workbook2
Dim tabWS As Worksheet           'analysing worksheet
Filename = Application.GetOpenFilename("Excel files (*.xls*),*.xl*", Title:="Open data")
Set wb = ActiveWorkbook
Set tabWS = Sheets("Tabelle1")


Dim bsmWS As Worksheet      ' workbook1
Set bsmWS = Sheets("Sheet1") ' currentworksheet

Workbook1(sheet1)是我当前的工作簿和工作表,我喜欢从另一个工作簿2(sheet1)获取一些数据。如何为两个工作表创建一个对象。我在“set bsmws”中编译失败

1 个答案:

答案 0 :(得分:2)

Sub test()

    Dim strFileName as String
    Dim wbTarget As Workbook
    Dim wbSource As Workbook
    Dim wsTarget As Worksheet
    Dim wsSource As Worksheet

    strFileName = Application.GetOpenFilename("Excel files (*.xls*),*.xl*", Title:="Open data")

    Set wbSource = ThisWorkbook
    Set wbTarget = Workbooks.Open(strFileName)

    Set wsSource = wbSource.Worksheets("Sheet1")
    Set wsTarget = wbTarget.Worksheets("Sheet1")

    'to copy from Target - > Source

    wsTarget.Range("B2").Resize(5, 5).Copy wsSource.Range("B2")

    'etc.

End Sub