Export Specific Sheet in Listbox to New Workbook

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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?
 
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
'<<=============
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top