VBA - Copy columns to a new spreadsheet based on date filter

WilliamA

New Member
Joined
Aug 30, 2022
Messages
9
Office Version
  1. 365
Platform
  1. Windows
Hello,

I am using VBA code from helpful information online to copy x number of columns from spreadsheet A to a new spreadsheet. The code I have provides the user with prompts to select the original spreadsheet, choose the number of columns to copy, select the respective columns, and save the final product.

I'd like to add the following
1. Delete the first 9 rows of what gets pasted (it is mostly empty space and unnecessary)
2. A date filter (what needs to get copied will differ depending on the work and will cross different ranges of dates)
3. Make the code work across tabs (the original spreadsheet where the data gets copied from has many tabs, and while all of the columns I want to copy are from the same tab, the code won't allow me to switch to another tab to copy the columns - I haven't figured out how the current code doesn't allow this)


I tried adding this line of code:

VBA Code:
Rows(9).EntireRow.Delete

to delete the first 9 rows however it did not work.

This is the working code

VBA Code:
Sub copy_data()

Dim data_wb As Workbook
Dim target_wb As Workbook
Dim file_name As Variant
Dim header_range(100) As Range
Dim last_row As Long
Dim col_number As Long
Dim col_letter As String
Dim counter As Long
Dim quantity As Long

'select workbook
file_name = Application.GetOpenFilename(Title:="Choose a target Workbook")

If file_name <> False Then

    'create a new target workbook
    Set target_wb = Application.Workbooks.Add

    'open workbook with the data
    Set data_wb = Application.Workbooks.Open(file_name)
    
    'get quantity to create loop
    quantity = _
    InputBox("How many columns do you want to copy?")
    
    'loop
    For counter = 1 To quantity
    
        'select header range
        Set header_range(counter) = _
        Application.InputBox("Select the HEADER of the " & counter & "º column you want to copy", Type:=8)
        
        'get last row and column letter
        col_number = header_range(counter).Column
        last_row = Cells(Rows.Count, col_number).End(xlUp).Row
        col_letter = Split(Cells(1, col_number).Address(True, False), "$")(0)

        'copy from original workbook
        Range(header_range(counter), Range(col_letter & last_row)).Copy
        
        'paste in target workbook
        target_wb.Sheets("Sheet1").Cells(1, counter).PasteSpecial xlPasteValues
        
        'delete the first nine rows
        Rows(9).EntireRow.Delete
        
    Next counter
    
    data_wb.Close
    
    'prompt user to save the file
    If Not target_wb.Saved Then
        If MsgBox("Do you want to save the file?", vbYesNo, "Save?") = vbYes Then
            target_wb.Save
        End If
    End If

End If

target_wb.Close

End Sub

I was trying something with input boxes to enter the date based on whichever column the user selects but I haven't been able to figure this out.

VBA Code:
    'Prompt the user to input the start date
    StartDate = InputBox("Please enter the start date")
    
    'Prompt the user to input the end date
    EndDate = InputBox("Please enter the end date")

And filter the data to those within the start and end dates

VBA Code:
        .AutoFilter Field:=lngDateCol, _
                    Criteria1:=">=" & StartDate, _
                    Criteria2:="<=" & EndDate


I hope this explains what I'm after and I appreciate the help.
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.

Forum statistics

Threads
1,224,823
Messages
6,181,177
Members
453,021
Latest member
Justyna P

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