I need help writing a macro that will copy and paste data from one workbook to a new workbook based on user input. That is, the macro should do 3 things:
allow the user to select the workbook that contains the data to be copied into a new workbook.
prompt the user to select the columns of data to copy into the new workbook, preferably by column header only.
prompt the user to save the file.
The code below allows the user to select the workbook and places the range of data from the open workbook into a userform, but I don't know how to copy the selected data from that userform into a new workbook. The code also displays the data from the work book in rows, but I only want a list of the column headers.
Note: "multiColumnRange" is a defined name of the range of data, but this is 'hard-coded' that I would like to be dynamic. That is, the range of data will vary based on the workbook.
Sub Select_Workbook()
'Disables screen updating
Application.ScreenUpdating = False
'Defines the variable to hold the value of the file to open
Dim FileToOpen As Variant
'Defines the variable of the location of the file and the new workbook
Dim OpenBook As Workbook, NewBook As Workbook
Dim strCol As String
'Defines variable to hold value of table range
Dim rngMultiColumn As Range
'Sets the variable to the file that is selected
FileToOpen = Application.GetOpenFilename(Title:="Browse for your File & Import Range", FileFilter:="Excel Files (*.xls*),*xls*")
'Checks if the user selected cancel and stores the Boolean value and not string
If FileToOpen <> False Then
Set OpenBook = Workbooks.Open(FileToOpen)
'Define source range, referring to table's data range
Set rngMultiColumn = OpenBook.Worksheets("Export").Range("multiColumnRange")
With ufrmListBoxMultiColumn.lboxExampleMC
.ColumnWidths = "120;120;120"
.List = rngMultiColumn.Cells.Value
End With
ufrmListBoxMultiColumn.Show
'Creates new workbook and assigns it to variable NewBook
Set NewBook = Workbooks.Add
End sub