Transfer a listbox+ button code to a userform code

GeneBF

New Member
Joined
Jun 28, 2022
Messages
35
Office Version
  1. 365
Platform
  1. Windows
Hi , a helpful user helped me once with this code
I need a dropdown that will list all worksheets in a multi-select listbox and selected sheets will have specific column copy pasted to a new sheet.
example: I have sheet A, B, C, and D. Listbox will have that Sheet listed,and whatever i select column F:F of each will be copied to a new sheet with 1 column space each.

heres what he gave me + whatever i added

VBA Code:
Private Sub CommandButton4_Click()

    Dim sheetsListBox As ListBox
    Dim copySheetsButton As Button
    Dim ws As Worksheet   
   
    With ActiveSheet
        Set sheetsListBox = Nothing
        On Error Resume Next
        Set sheetsListBox = .ListBoxes("List Sheets")
        On Error GoTo 0
        If sheetsListBox Is Nothing Then Set sheetsListBox = .ListBoxes.Add(.Range("K2").Left, .Range("K2").Top, 160, 84)
       
        Set copySheetsButton = Nothing
        On Error Resume Next
        Set copySheetsButton = .Buttons("Copy Column")
        On Error GoTo 0
        If copySheetsButton Is Nothing Then Set copySheetsButton = .Buttons.Add(sheetsListBox.Left, sheetsListBox.Top + sheetsListBox.Height + 5, 140, 60)
    End With

    With sheetsListBox
        .Name = "List Sheets"
        .RemoveAllItems
        .Top = 15
        .Width = 165
        .Left = 720
        .MultiSelect = xlSimple
        For Each ws In ActiveWorkbook.Worksheets
            .AddItem ws.Name
        Next
    End With
   
    With copySheetsButton
        .Name = "Copy Column"
        .Top = sheetsListBox.Top + sheetsListBox.Height + 5
        .Width = 165
        .Left = 720
        .Caption = "Generate Block File"
        .OnAction = "Copy_Column_From_Selected_Sheets"
    End With          
       
End Sub


Public Sub Copy_Column_From_Selected_Sheets()

    Dim currentSheet As Worksheet
    Dim sheetsListBox As ListBox
    Dim copySheetsButton As Button
    Dim i As Long, numSelectedSheets As Long, colNumber As Long
    Dim NewWs As Worksheet
    Dim NewWsName As String
    Dim SavePath As String
    Dim fdObj As Object
                  
   
    Set currentSheet = ActiveSheet
    Set sheetsListBox = ActiveSheet.ListBoxes("List Sheets")
    Set copySheetsButton = ActiveSheet.Buttons("Copy Column")
   
    With sheetsListBox
        numSelectedSheets = 0
        For i = 1 To .ListCount
            If .Selected(i) Then numSelectedSheets = numSelectedSheets + 1
        Next
          
          
    If numSelectedSheets = 0 Then
    MsgBox "no sheet(s) selected", vbCritical
    Exit Sub
    Else
    End If
   
    NewWsName = InputBox("Input PROJECT File Name" & vbNewLine & "Files are saved on:" & vbNewLine & " :desktop\Excel Exports ", "Exports")
    If NewWsName = "" Then
    MsgBox "no Project file name entered", vbCritical
    Exit Sub
    Else
    End If
   
   
        If numSelectedSheets > 0 Then
            Set NewWs = ActiveWorkbook.Worksheets.Add(Before:=Worksheets(1))
            NewWs.Name = NewWsName
            colNumber = 6
            For i = 1 To .ListCount
                If .Selected(i) Then
                    ActiveWorkbook.Worksheets(.List(i)).Columns("F:F").Copy
                    NewWs.Cells(1, colNumber).PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
                    NewWs.Cells(1, colNumber).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
                    colNumber = colNumber + 2
                    ActiveWorkbook.Worksheets(.List(i)).Columns("A:D").Copy
                    NewWs.Cells(1, 1).PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
                    NewWs.Cells(1, 1).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
                    NewWs.Cells(6, colNumber - 2).FormulaR1C1 = .List(i) & " QTY"
                   
                End If
            Next
        Else
            MsgBox "No sheet(s) selected", vbCritical
        End If
    End With
   
    sheetsListBox.Delete
    copySheetsButton.Delete
   
   
   SavePath = CreateObject("WScript.Shell").specialfolders("Desktop")
   
   
   
        Application.ScreenUpdating = False
    Set fdObj = CreateObject("Scripting.FileSystemObject")
   
     If fdObj.FolderExists(SavePath & "\Excel Exports") Then
       
    Else

        fdObj.CreateFolder (SavePath & "\Excel Exports")
    End If
    Application.ScreenUpdating = True
   
   
    Sheets(NewWsName).Select
    Sheets(NewWsName).Move
    Application.DisplayAlerts = False
     ActiveWorkbook.SaveAs Filename:=SavePath & "\Excel Exports\" & NewWsName & ".xlsx", _
        FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False
        ActiveWorkbook.Saved = True
        Application.DisplayAlerts = True
    
    
     Workbooks(NewWsName & ".XLSx").Close SaveChanges:=False
       
End Sub

now I have set-up a userform with a listbox and button already, i'll need specific sheets starting with key letters to only show those matching char.
heres my userform with a listbox set-up already and button

VBA Code:
Private Sub UserForm_Initialize()
  Dim i As Long
             
    For i = 1 To ActiveWorkbook.Sheets.Count
        If Sheets(i).Name Like "CP*" Then
       
        ListBox1.AddItem ActiveWorkbook.Sheets(i).Name
       
        Else
               
    End If
   
    Next   
   
    End Sub

now I am trying to replicate/ transfer the code to the button in my userform. the problem is I cant figure that out as Im not familiar with the original code given to me.
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
Still trying to figure this out,

here is a sample file for this problem Sample Workbook

the Command Button 6 is the one im trying to fix , the other button "generate block file" is the working code for this
The Button6 is linked to a userform.
The other button's code is linked to module2

Im trying to replicate the "generate" button code to work with userform using button6,

1659421382165.png
 
Upvote 0

Forum statistics

Threads
1,223,702
Messages
6,173,959
Members
452,539
Latest member
delvey

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