2016年7月28日星期四

vba sheet copy, val copy.

Sub copyValToAnotherBook()
    Application.DisplayAlerts = False
    Dim srcSheet As Workbook
    Dim desSheet As Workbook
    Dim ws As Worksheet
    Dim vals As Variant
    '## Open both workbooks first:
    Set srcSheet = Workbooks.Open("C:\1\s.xlsx")
    Set desSheet = Workbooks.Open("C:\2\d.xlsx")
   
    '新規シート
    With ThisWorkbook
        Set ws = desSheet.Sheets.Add(After:=desSheet.Sheets(desSheet.Sheets.Count))
        ws.name = "news"
    End With
   Windows("s.xlsx").Activate
   srcSheet.Sheets("ss").Select
   srcSheet.Sheets("ss").Range("A1:B3").Select
   'Store the value in a variable:
   vals = srcSheet.Sheets("ss").Range("A1:B3").Value
    'Use the variable to assign a value to the other file/sheet:
    desSheet.Sheets("news").Range("A1:B3").Value = vals
   
    'Close x:
    srcSheet.Close
    desSheet.Save
   
    desSheet.Close
End Sub




Private Sub CreateSheet()
    Dim ws As Worksheet
    Set ws = ThisWorkbook.Sheets.Add(After:= _
             ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count))
    ws.name = "Tempo"
End Sub




Sub CopyAllSheetsToAnotherBook()
    Dim srcSheet As Workbook
    Dim desSheet As Workbook
    Dim currentSheet As Worksheet
    Dim sheetIndex As Integer
    sheetIndex = 1
   
    Set srcSheet = Workbooks.Open("C:\1\s.xlsx")
    Set desSheet = Workbooks.Open("C:\2\d.xlsx")

    srcSheet.Activate
   
    For Each currentSheet In Worksheets
        Windows("s.xlsx").Activate
        currentSheet.Select
        currentSheet.Copy Before:=Workbooks("d.xlsx").Sheets(desSheet.Sheets.Count)
        sheetIndex = sheetIndex + 1
    Next currentSheet

End Sub




Sub CopyOneSheetToAnotherBook()
    Application.DisplayAlerts = False 'dont show Msg when exit
    Dim srcSheet   As Workbook
    Dim desSheet   As Workbook
    Dim currentSheet As Worksheet
    Dim sheetIndex As Integer
    sheetIndex = 1
   
    Set srcSheet = Workbooks.Open("C:\1\s.xlsx")
    Set desSheet = Workbooks.Open("C:\2\d.xlsx")

    Windows("s.xlsx").Activate
    Sheets("ss2").Select
    Sheets("ss2").Copy Before:=Workbooks("d.xlsx").Sheets(desSheet.Sheets.Count)
   
     srcSheet.Close
     desSheet.Save
   
     desSheet.Close
End Sub

没有评论:

发表评论