Transfer selected items from listbox to sheets range and as sheets name

eddmar1993

New Member
Joined
May 7, 2019
Messages
2
Hello! I am just new to this site and new to VBA.
I have two listboxes (Months and Product Names). I set these to 1-fmMultiSelectMulti. What I wanted is that every time I click OK button new sheets will be added with names as in the listbox(Months) and the selected items from listbox(Product Names) will be transfer to "D1" of the new sheets. I want it to offset columns.

I am fine with listbox(Months). This is my code:

Code:
Private Sub cmdOK_Click()
    
        Dim sName As String
        Dim x As Integer
        Dim ws As Worksheet
    
        For x = 0 To lbMonths.ListCount - 1
            If lbMonths.Selected(x) = True Then
        
                sName = lbMonths.List(x)
                Set ws = Sheets.Add(After:=Sheets(Sheets.Count))
                ws.Name = sName
            
            Else: Exit Sub
        
            End If
        Next x
      
End Sub

My problem now is the second listbox(Product Names). I want the codes to be in the cmdOK.

Anyone can help me?
 

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
Hi & welcome to MrExcel.
How about
Code:
Private Sub cmdOK_Click()
   Dim i As Long, j As Long
   Dim Ary As Variant
   Dim Ws As Worksheet
   
   With Me.[COLOR=#ff0000]ListBox2[/COLOR]
      ReDim Ary(1 To .ListCount)
      For i = 0 To .ListCount - 1
         If .Selected(i) Then
            j = j + 1
            Ary(j) = .List(i)
         End If
      Next i
   End With
   With Me.lbMonths     
      For i = 0 To .ListCount - 1
         If .Selected(i) Then
            Set Ws = Sheets.Add(, Sheets(Sheets.Count))
            Ws.Name = .List(i)
            Ws.Range("D1").Resize(, j).Value = Ary
         End If
      Next i
   End With
End Sub
Change value in red to match your product names listbox
 
Upvote 0
You're welcome & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,214,805
Messages
6,121,656
Members
449,045
Latest member
Marcus05

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top