|
本帖最后由 bosslxt 于 2016-4-7 15:03 编辑
编了一个,可以试用下:- Sub 两个文件数据复制()
- Dim WB1 As Workbook, WB2 As Workbook, SH As Worksheet, Rng As Range
- '查找“CB”文件,如没有打开,则打开该文件,并设为对象WB1
- For Each WB1 In Workbooks
- If WB1.Name Like "CB.xl*" Then GoTo NextProgram_1
- Next
- With Application.FileDialog(msoFileDialogFilePicker)
- .Title = "请指定对比文件"
- .AllowMultiSelect = False
- .Show
- If .SelectedItems.Count = 0 Then Exit Sub '未选择文件
- Application.ScreenUpdating = False
- Workbooks.Open Filename:=.SelectedItems(1)
- Set WB1 = ActiveWorkbook
- End With
- NextProgram_1:
- '查找“新成本单样表”文件,如没有打开,则打开该文件,并设为对象WB2
- For Each WB2 In Workbooks
- If WB2.Name Like "新成本单样表.xl*" Then GoTo NextProgram_2
- Next
- With Application.FileDialog(msoFileDialogFilePicker)
- .Title = "请指定目标文件"
- .AllowMultiSelect = False
- .Show
- If .SelectedItems.Count = 0 Then Exit Sub '未选择文件
- Application.ScreenUpdating = False
- Workbooks.Open Filename:=.SelectedItems(1)
- Set WB2 = ActiveWorkbook
- End With
- NextProgram_2:
- Application.ScreenUpdating = False
- WB2.Activate
- For Each SH In WB2.Sheets
- SH.Select
- If SH.Range("A3").Value = WB1.Sheets(SH.Name).Range("A3").Value Then
- For Each Rng In SH.Range("D12:P13", "R12:W13")
- Rng.Value = WB1.Sheets(SH.Name).Range(Rng.Offset(4, 4).Address).Value
- Next
- End If
- Next
- Application.ScreenUpdating = True
- End Sub
复制代码 |
|