ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Export Specific Sheet in Listbox to New Workbook (https://www.excelbanter.com/excel-programming/387397-export-specific-sheet-listbox-new-workbook.html)

Carlee

Export Specific Sheet in Listbox to New Workbook
 
Hi all,

I have a userform with a listbox on it. The list box contains the names of
several sheets in my workbook. I would like to enable the user to select a
sheet in the list box, and click "Export". On click, i'd like Excel to copy
the selected Sheet to a new workbook. The new workbook will be named the
same as the sheet selected, and placed on the users Desktop.

Can anyone shed some light on this for me?
--
Carlee

Norman Jones

Export Specific Sheet in Listbox to New Workbook
 
Hi Carlee,

'----------------
I have a userform with a listbox on it. The list box contains the names of
several sheets in my workbook. I would like to enable the user to select a
sheet in the list box, and click "Export". On click, i'd like Excel to copy
the selected Sheet to a new workbook. The new workbook will be named the
same as the sheet selected, and placed on the users Desktop.

Can anyone shed some light on this for me?
'---------------

In the Userform module, paste the following code:

'=============
Private Sub cbExport_Click() '<<==== CHANGE
Dim SH As Worksheet
Dim WB As Workbook
Dim WB2 As Workbook

Set WB = ThisWorkbook

WB.Sheets(Me.ListBox1.Value).Copy
Set WB2 = ActiveWorkbook

With WB2
.SaveAs Filename:=WB2.Sheets(1).Name & ".xls"
Call SendToDesktop(WB2)
.Close
End With
End Sub
'<<=============

In a standard module, paste:

'=============
Sub SendToDesktop(WB As Workbook)
Dim oWSH As Object
Dim oShortcut As Object
Dim myPath As String
Dim myShortcutPath As String
Dim sStr As String

With WB
myPath = .FullName
sStr = "\" & Left(.Name, Len(.Name) - 4)
End With

Set oWSH = CreateObject("WScript.Shell")

With oWSH
myShortcutPath = .SpecialFolders.Item("Desktop")
Set oShortcut = .CreateShortcut _
(myShortcutPath & sStr & ".lnk")
End With

With oShortcut
.TargetPath = myPath
.Save
End With

Set oWSH = Nothing
End Sub
'<<=============


---
Regards,
Norman




All times are GMT +1. The time now is 01:56 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com