Copying range and pasting into new workbook

后端 未结 2 618
死守一世寂寞
死守一世寂寞 2021-01-03 01:26

This should be really simple, but I\'ve been trawling forums and SO answers for hours to find the answer with no luck, so am (reluctantly) creating a question of my own.

2条回答
  •  没有蜡笔的小新
    2021-01-03 02:03

    This works for me.

    Private Sub CommandButton1_Click()
    Dim newWB As Workbook, currentWB As Workbook
    Dim newS As Worksheet, currentS As Worksheet
    
    'Copy the data you need
    Set currentWB = ThisWorkbook
    Set currentS = currentWB .Sheets("Sheet1")
    currentS .Range("A:M").Select
    Selection.Copy
    
    'Create a new file that will receive the data
    Set newWB = Workbooks.Add
        With newWB
            Set newS = newWB.Sheets("Sheet1")
            newS.Range("A1").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, _
            SkipBlanks:=False, Transpose:=False
            'Save in CSV
            Application.DisplayAlerts = False
            .SaveAs Filename:="C:\Temporary.csv", FileFormat:=xlCSV
            Application.DisplayAlerts = True
        End With
    End Sub
    

提交回复
热议问题