I am trying to create a table subset from a larger table. I'm pulling data from certain columns based on data that is filtered so everything up till the first blank row is reached is copied and pasted on a new sheet. Ideally I would like to create a specific type of formatted table, but for now I'm trying to copy the same format as the main table but excel seems to run very repetitively and I'm wondering if its because of redundancies.
Sub Lists()
Dim i As Integer 'define variables, i is a counter, K is a counter, c is an array to hold the values of column numbers to be coppied
'Dim k As Integer ****this variable is no longer needed with this new code of including the formating
'k = 2 'initialize value of counter k the value needed is 2 because the loop does not handle the first element, this is hard coded *** no longer needed with new formatting code
Dim c As Variant 'this variable holds the column numbers to be copied
c = Array(1, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 16, 17, 24, 25)
Dim lNumElements As Long ' this varibale will hold the number of elements in array c
lNumElements = UBound(c) - LBound(c) + 1 'this is a formula for the number of elemnts in variable c
Dim NAME As String
NAME = InputBox("Please name the sheet") 'here the user can choose the name of the new worksheet that they wish to write the new table to
Dim ws As Worksheet 'declare a new worksheet to me made
Set ws = ThisWorkbook.Sheets.Add(After:= _
ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count)) 'code used to add a new work sheet
ws.NAME = NAME 'use the name from the user input to rename the worksheet
Worksheets("Database").Select 'select the database worksheet
Worksheets("Database").Range("A1").Activate 'place the curser on the A1 range of database
'Sheets("Database").Columns(1).Copy Destination:=Sheets(ws.NAME).Columns(1) ' copy from database sheet and paste to new sheet hard coded for column 1 as the for loop did not like having column one in it as well *** no longer needed with new code
Sheets("Database").Columns(1).Copy 'copy the first column ( column A)
Worksheets(NAME).Select 'choose where you want to copy the data to on the new page
Worksheets(NAME).Range("A1").Activate 'activate the section you choose to copy to in the previous line of code
Selection.PasteSpecial Paste:=xlPasteValues 'paste the values of the code you wanted
Selection.PasteSpecial Paste:=xlPasteFormats 'keep the formating of the code you pasted
For i = 1 To lNumElements - 1 'this for loop will cycle through the number of elements in array c except for the first element
'Sheets("Database").Columns(c(i)).Copy Destination:=Sheets(ws.NAME).Columns(k) ' copy from database sheet and paste to new sheet excluding element 1). Paste information starting in column 2 (column 1 is hard coded above)
Worksheets("Database").Select
Columns(c(i)).Activate
Sheets("Database").Columns(c(i)).Copy
Worksheets(NAME).Select
Columns(i + 1).Activate
Selection.PasteSpecial Paste:=xlPasteValues
Selection.PasteSpecial Paste:=xlPasteFormats
k = k + 1
Next i
End Sub