从选定的工作簿中复制和粘贴特定值

时间:2014-03-21 02:51:27

标签: excel vba

您好我希望使用浏览提示从选定的工作簿中复制一组特定的单元格。在下面的代码中,我可以让宏成功运行,但是当宏完成运行时,所选单元格中的值不会出现在活动工作簿中。宏运行时我没有收到任何错误,但是我也没有看到活动工作簿中的值。有什么建议?代码如下所示。

Sub ImportData()

Dim wb1 As Workbook
Dim wb2 As Workbook
Dim Sheet As Worksheet
Dim PasteStart As Range

Set wb1 = ActiveWorkbook
wb1.Worksheets("Template").Unprotect Password:="cna"

FileToOpen = Application.GetOpenFilename _
(Title:="Please choose Exsiting PO Template", _
FileFilter:="Report Files *.xls (*.xls),")

If FileToOpen = False Then
    MsgBox "No File Specified.", vbExclamation, "ERROR"
Else

    Set wb2 = Workbooks.Open(Filename:=FileToOpen)
    wb2.Worksheets("Template").Unprotect Password:="cna"
    With wb2.Sheets("Template")
        Range("A4").Select
        Selection.Copy
    With wb1.Sheets("Template")
        Range("A4").Select
        Selection.PasteSpecial xlValues


    End With
    End With

    With wb2.Sheets("Template")
        Range("B4").Select
        Selection.Copy
    With wb1.Sheets("Template")
        Range("B4").Select
        Selection.PasteSpecial xlValues
    End With
    End With


    With wb2.Sheets("Template")
        Range("C4").Select
        Selection.Copy
    With wb1.Sheets("Template")
        Range("C4").Select
        Selection.PasteSpecial xlValues
    End With
    End With


    With wb2.Sheets("Template")
        Range("D4").Select
        Selection.Copy
    With wb1.Sheets("Template")
        Range("D4").Select
        Selection.PasteSpecial xlValues
    End With
    End With


    With wb2.Sheets("Template")
        Range("A7").Select
        Selection.Copy
    With wb1.Sheets("Template")
        Range("A7").Select
        Selection.PasteSpecial xlValues
    End With
    End With


    With wb2.Sheets("Template")
        Range("B7").Select
        Selection.Copy
    With wb1.Sheets("Template")
        Range("B7").Select
        Selection.PasteSpecial xlValues
    End With
    End With


    With wb2.Sheets("Template")
        Range("C7").Select
        Selection.Copy
    With wb1.Sheets("Template")
        Range("C7").Select
        Selection.PasteSpecial xlValues
    End With
    End With


    With wb2.Sheets("Template")
        Range("D7").Select
        Selection.Copy
    With wb1.Sheets("Template")
        Range("E7").Select
        Selection.PasteSpecial xlValues
    End With
    End With


    With wb2.Sheets("Template")
        Range("E7").Select
        Selection.Copy
    With wb1.Sheets("Template")
        Range("E7").Select
        Selection.PasteSpecial xlValues
    End With
    End With



    With wb2.Sheets("Template")
        Range("F7").Select
        Selection.Copy
    With wb1.Sheets("Template")
        Range("F7").Select
        Selection.PasteSpecial xlValues
    End With
    End With


    With wb2.Sheets("Template")
        Range("A10").Select
        Selection.Copy
    With wb1.Sheets("Template")
        Range("A10").Select
        Selection.PasteSpecial xlValues
    End With
    End With


    With wb2.Sheets("Template")
        Range("B10").Select
        Selection.Copy
    With wb1.Sheets("Template")
        Range("B10").Select
        Selection.PasteSpecial xlValues
    End With
    End With


    With wb2.Sheets("Template")
        Range("C10").Select
        Selection.Copy
    With wb1.Sheets("Template")
        Range("C10").Select
        Selection.PasteSpecial xlValues
    End With
    End With


    With wb2.Sheets("Template")
        Range("D10").Select
        Selection.Copy
    With wb1.Sheets("Template")
        Range("D10").Select
        Selection.PasteSpecial xlValues
    End With
    End With


    With wb2.Sheets("Template")
        Range("E10").Select
        Selection.Copy
    With wb1.Sheets("Template")
        Range("E10").Select
        Selection.PasteSpecial xlValues
    End With
    End With


    With wb2.Sheets("Template")
        Range("F10").Select
        Selection.Copy
    With wb1.Sheets("Template")
        Range("F10").Select
        Selection.PasteSpecial xlValues
    End With
    End With


    With wb2.Sheets("Template")
        Range("G10").Select
        Selection.Copy
    With wb1.Sheets("Template")
        Range("G10").Select
        Selection.PasteSpecial xlValues
    End With
    End With


    With wb2.Sheets("Template")
        Range("I10").Select
        Selection.Copy
    With wb1.Sheets("Template")
        Range("I10").Select
        Selection.PasteSpecial xlValues
    End With
    End With


    With wb2.Sheets("Template")
        Range("A13").Select
        Selection.Copy
    With wb1.Sheets("Template")
        Range("A13").Select
        Selection.PasteSpecial xlValues
    End With
    End With


    With wb2.Sheets("Template")
        Range("B13").Select
        Selection.Copy
    With wb1.Sheets("Template")
        Range("B13").Select
        Selection.PasteSpecial xlValues
    End With
    End With


    With wb2.Sheets("Template")
        Range("A19").Select
        Selection.Copy
    With wb1.Sheets("Template")
        Range("A19").Select
        Selection.PasteSpecial xlValues
    End With
    End With


    With wb2.Sheets("Template")
        Range("B19").Select
        Selection.Copy
    With wb1.Sheets("Template")
        Range("B19").Select
        Selection.PasteSpecial xlValues
    End With
    End With

    With wb2.Sheets("Template")
        Range("C19").Select
        Selection.Copy
    With wb1.Sheets("Template")
        Range("C19").Select
        Selection.PasteSpecial xlValues
    End With
    End With


    With wb2.Sheets("Template")
        Range("F19").Select
        Selection.Copy
    With wb1.Sheets("Template")
        Range("F19").Select
        Selection.PasteSpecial xlValues
    End With
    End With

    With wb2.Sheets("Template")
        Range("G19").Select
        Selection.Copy
    With wb1.Sheets("Template")
        Range("G19").Select
        Selection.PasteSpecial xlPasteAll
    End With
    End With


    With wb2.Sheets("Template")
        Range("A26").Select
        Selection.Copy
    With wb1.Sheets("Template")
        Range("A26").Select
        Selection.PasteSpecial xlValues
    End With
    End With

    With wb2.Sheets("Template")
        Range("B26").Select
        Selection.Copy
    With wb1.Sheets("Template")
        Range("B26").Select
        Selection.PasteSpecial xlValues
    End With
    End With

    With wb2.Sheets("Template")
        Range("C26").Select
        Selection.Copy
    With wb1.Sheets("Template")
        Range("C26").Select
        Selection.PasteSpecial xlValues
    End With
    End With

    With wb2.Sheets("Template")
        Range("D26").Select
        Selection.Copy
    With wb1.Sheets("Template")
        Range("D26").Select
        Selection.PasteSpecial xlValues
    End With
    End With

    With wb2.Sheets("Template")
        Range("E26").Select
        Selection.Copy
    With wb1.Sheets("Template")
        Range("E26").Select
        Selection.PasteSpecial xlValues
    End With
    End With

    With wb2.Sheets("Template")
        Range("F26").Select
        Selection.Copy
    With wb1.Sheets("Template")
        Range("F26").Select
        Selection.PasteSpecial xlValues
    End With
    End With

    With wb2.Sheets("Template")
        Range("G26").Select
        Selection.Copy
    With wb1.Sheets("Template")
        Range("G26").Select
        Selection.PasteSpecial xlValues
    End With
    End With

    With wb2.Sheets("Template")
        Range("H26").Select
        Selection.Copy
    With wb1.Sheets("Template")
        Range("H26").Select
        Selection.PasteSpecial xlValues
    End With
    End With

    With wb2.Sheets("Template")
        Range("A29").Select
        Selection.Copy
    With wb1.Sheets("Template")
        Range("A29").Select
        Selection.PasteSpecial xlValues
    End With
    End With

    With wb2.Sheets("Template")
        Range("B29").Select
        Selection.Copy
    With wb1.Sheets("Template")
        Range("B29").Select
        Selection.PasteSpecial xlValues
    End With
    End With

    With wb2.Sheets("Template")
        Range("C29").Select
        Selection.Copy
    With wb1.Sheets("Template")
        Range("C29").Select
        Selection.PasteSpecial xlValues
    End With
    End With

    With wb2.Sheets("Template")
        Range("D29").Select
        Selection.Copy
    With wb1.Sheets("Template")
        Range("D29").Select
        Selection.PasteSpecial xlPasteAll
    End With
    End With

    With wb2.Sheets("Template")
        Range("E29").Select
        Selection.Copy
    With wb1.Sheets("Template")
        Range("E29").Select
        Selection.PasteSpecial xlValues
    End With
    End With

    With wb2.Sheets("Template")
        Range("F29").Select
        Selection.Copy
    With wb1.Sheets("Template")
        Range("F29").Select
        Selection.PasteSpecial xlValues
    End With
    End With

    With wb2.Sheets("Template")
        Range("A32").Select
        Selection.Copy
    With wb1.Sheets("Template")
        Range("A32").Select
        Selection.PasteSpecial xlValues
    End With
    End With

    With wb2.Sheets("Template")
        Range("B32").Select
        Selection.Copy
    With wb1.Sheets("Template")
        Range("B32").Select
        Selection.PasteSpecial xlValues
    End With
    End With

    With wb2.Sheets("Template")
        Range("C32").Select
        Selection.Copy
    With wb1.Sheets("Template")
        Range("C32").Select
        Selection.PasteSpecial xlValues
    End With
    End With

    With wb2.Sheets("Template")
        Range("D32").Select
        Selection.Copy
    With wb1.Sheets("Template")
        Range("D32").Select
        Selection.PasteSpecial xlValues
    End With
    End With

    With wb2.Sheets("Template")
        Range("E32").Select
        Selection.Copy
    With wb1.Sheets("Template")
        Range("E32").Select
        Selection.PasteSpecial xlValues
    End With
    End With

    With wb2.Sheets("Template")
        Range("F32").Select
        Selection.Copy
    With wb1.Sheets("Template")
        Range("F32").Select
        Selection.PasteSpecial xlValues
    End With
    End With

    With wb2.Sheets("Template")
        Range("G32").Select
        Selection.Copy
    With wb1.Sheets("Template")
        Range("G32").Select
        Selection.PasteSpecial xlValues
    End With
    End With

    With wb2.Sheets("Template")
        Range("H32").Select
        Selection.Copy
    With wb1.Sheets("Template")
        Range("H32").Select
        Selection.PasteSpecial xlValues
    End With
    End With


End If

wb2.Close

End Sub

1 个答案:

答案 0 :(得分:0)

我认为中的点(。)

With wb2.Sheets("Template")
        .Range("A4").Copy ' missing . operator in the beginning
    With wb1.Sheets("Template")
        .Range("A4").PasteSpecial xlValues 'also try to avoid .select in your code -it will slow down your code
 end with
    End With