excel vba复制选定的数据范围从多个工作表到新工作簿

时间:2017-07-27 00:32:04

标签: excel vba

我想将所选数据范围从sheet1,sheet2和sheet3复制到新工作簿sheet1,sheet2和sheet3,下面的代码只是为sheet1和sheet2工作精细复制,而不是sheet3;当来到sheet3时,它总是复制sheet1数据而不是sheet3;我的代码中缺少什么?

(注意:对于所有第1张和第3张纸,我引用要从单元格F1复制的数据范围。)

谢谢。

Set Source1 = Range(Sheet12.Range("f1")).SpecialCells(xlCellTypeVisible)

On Error GoTo 0

If Source1 Is Nothing Then
    MsgBox "The source is not a range or the sheet is protected. " & _
           "Please correct and try again.", vbOKOnly
    Exit Sub
End If

With Application
    .ScreenUpdating = False
    .EnableEvents = False
End With

Set wb = ActiveWorkbook
Set Dest = Workbooks.Add(xlWBATWorksheet)
Source1.Copy
With Dest.Sheets(1)
    .Cells(1).PasteSpecial Paste:=8
    .Cells(1).PasteSpecial Paste:=xlPasteValues
    .Cells(1).PasteSpecial Paste:=xlPasteFormats
    .Cells(1).Select
    Application.CutCopyMode = False


End With

Sheets.Add After:=Sheets(Sheets.count)
Dest.Sheets("Sheet2").Activate

' --------2------------------------

 Set dest2 = ActiveWorkbook

Set source2 = Sheet16.Range("a2:q1000")

source2.Copy

With dest2.Sheets(2)
    .Cells(1).PasteSpecial Paste:=8
    .Cells(1).PasteSpecial Paste:=xlPasteValues
    .Cells(1).PasteSpecial Paste:=xlPasteFormats
    .Cells(1).Select
        Application.CutCopyMode = False

End With

'----------3--------

Sheets.Add After:=Sheets(Sheets.count)
Dest.Sheets("Sheet3").Activate

 Set dest3 = ActiveWorkbook
Set source3 = Range(Sheet20.Range("f1")).SpecialCells(xlCellTypeVisible) 

source3.Copy

With dest3.Sheets(3)
    .Cells(1).PasteSpecial Paste:=8
    .Cells(1).PasteSpecial Paste:=xlPasteValues
    .Cells(1).PasteSpecial Paste:=xlPasteFormats
    .Cells(1).Select
       Application.CutCopyMode = False

End With
'----------/3-------

0 个答案:

没有答案