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
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
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.
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.