lundi 7 juillet 2014

Excel VBA select and delete sheets using UserForm


Vote count:

0




My userform was supposed to make a list of existing sheets in the current workbook, and allow the user to choose which sheets to keep. Then it would remove the rest. Unfortunately, the program seems to remove sheets at random, and I can not think of a solution. Could you help me? I am a beginner in VBA. The code is below.



Option Explicit

Private Sub UserForm_Initialize()
Dim WS As Worksheet
For Each WS In ActiveWorkbook.Worksheets
UserForm1.ListBox1.AddItem (WS.Name)
ListBox1.MultiSelect = fmMultiSelectMulti
Next
End Sub

Private Sub CommandButton1_Click()
Dim I, A As Long
A = ListBox1.ListCount
For I = 0 To A - 1
If ListBox1.Selected(I) = False Then
Sheets(I + 1).Delete
End If
Next
Unload (UserForm1)
End Sub


PS. I tried using sheet names, but I couldn't figure out a way to get names of the non-selected sheets from the list. ListBox(I).Name didn't seem to work.



asked 51 secs ago






Aucun commentaire:

Enregistrer un commentaire