将excel范围导出为新的Excel工作表,并将其另存为对话框

时间:2013-06-11 11:07:31

标签: excel-vba export-to-excel vba excel

我想在excel表单中创建一个按钮,该按钮将复制特定范围(命名范围),然后打开另存为对话框,用户可以选择该位置并将该范围保存为新的Excel工作表。这可能吗?

我能够编写一个代码,将命名范围复制到剪贴板,然后用户可以将其粘贴到MS字等中。以下是我现在的代码。

Sub copyValueTable()
  Dim oRange As Range
  Set oRange = Sheets("Analysis").Range("FullValueTable")
  oRange.CopyPicture xlScreen, xlPicture
  oRange.Copy
End Sub

1 个答案:

答案 0 :(得分:1)

尝试以下代码:

Sub copyValueTable()

    On Error Resume Next

    Dim rng As Range
    Dim wkb As Workbook
    Dim sht As Worksheet

    Set rng = Application.InputBox("Please select the range" & vbNewLine & "Enter named range", Type:=8)

    On Error GoTo 0

    If Not rng Is Nothing Then
        fileSaveName = Application.GetSaveAsFilename(fileFilter:="xls Files (*.xls), *.xls")
        If fileSaveName <> False Then

            ActiveSheet.Copy
            Set wkb = ActiveWorkbook
            Set sht = wkb.Sheets(1)
            sht.Cells.Clear
            rng.Copy sht.Range("A1")

            wkb.SaveAs fileSaveName
            wkb.Close
        End If
    End If
End Sub