I'm receiving several different excel spreadsheets with up to 30 different column headers. I really only need about 8 or 10 of the columns from each spreadsheet. I'm sick of scrolling left and right finding the columns I need. I'd like to have a macro that pops up a dialog box with all the headers available. I'd like to select the headers that I want and have it cut and paste them from left to right so they are all next to each other.
I'm new to VBA and trying to learn it but this is a little over my head. Help anyone??
I've found ways to organize my columns the same way every time but each spreadsheet has different columns and orders so I need to be able to select them.
I suggest using userform with a listbox where you can select the header. You can try the code below.
Requirement: Userform ListBox > Multiselect Property should be = 1 Button > to load the selected data to a new worksheet
Dim mySH As Worksheet
Dim oSH As Worksheet 'Output Worksheet
Private Sub cmd_load_Click()
Dim i As Integer
Dim col_count As Integer
col_count = 1
Dim col_header As String
Dim ns_srow As Integer
ns_srow = 1
'LOOP THRU ALL ITEMS IN LISTBOX AND GET ALL SELECTED
For i = 0 To lst_header.ListCount - 1
If lst_header.Selected(i) Then
col_header = lst_header.List(i)
'FIND THE COLUMN HEADER POSITION AND TRANSFER TO NEWSHEET THE DATA
For a = 1 To mySH.Cells(1, Columns.Count).End(xlToLeft).Column
If mySH.Cells(1, a).Value = col_header Then
For b = 1 To mySH.Cells(Rows.Count, a).End(xlUp).Row
oSH.Cells(ns_srow, col_count).Value = mySH.Cells(b, a).Value
ns_srow = ns_srow + 1
Next b
col_count = col_count + 1
ns_srow = 1
Exit For
End If
Next a
End If
Next i
MsgBox "Data Completed"
End
End Sub
Private Sub UserForm_Initialize()
Set mySH = ThisWorkbook.Sheets("Data") 'name of your raw data worksheet
Set oSH = ThisWorkbook.Sheets("Output") 'output worksheet
'Assuming that column header is at row 1
For a = 1 To mySH.Cells(1, Columns.Count).End(xlToLeft).Column
lst_header.AddItem mySH.Cells(1, a).Value
Next a
End Sub