Macro copying Column Range instead of individual Column

GuardianEnzo

New Member
Joined
Jun 27, 2017
Messages
11
I've written a basic macro with extracts records based on two dates:


Code:
Sub extractDataBasedOnDate()


Dim lastrow As Long, erow As Long, i As Long
Dim mydate As Date
Dim StartDate As Date
Dim EndDate As Date


StartDate = Worksheets("Expense Report").Cells(3, 1).Value
EndDate = Worksheets("Expense Report").Cells(3, 2).Value
lastrow = Worksheets("Expenses").Cells(Rows.Count, 1).End(xlUp).Row


Worksheets("Expense Report").Rows(7 & ":" & Sheet2.Rows.Count).Delete


Worksheets("Expenses").Activate
Worksheets("Expenses").Range("A1").Select


For i = 2 To lastrow


    mydate = Cells(i, 6)
    If mydate >= StartDate And mydate <= EndDate Then
    erow = Sheet2.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
    Range(Cells(i, 2), Cells(i, 6)).Copy Destination:=Sheets("Expense Report").Cells(erow, 1)
    End If
Next i


End Sub


The problem I am having is that it is returning every column between Cells(i, 2) to Cells(i , 6).
I had intended to only copy the two columns (Column B and F), how can I achieve this?
Do I need to do a separate line for each column?


Thanks!
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
Try this:
Code:
Sub extractDataBasedOnDate()

Dim lastrow As Long, erow As Long, i As Long
Dim mydate As Date
Dim StartDate As Date
Dim EndDate As Date

StartDate = Worksheets("Expense Report").Cells(3, 1).Value
EndDate = Worksheets("Expense Report").Cells(3, 2).Value
lastrow = Worksheets("Expenses").Cells(Rows.Count, 1).End(xlUp).Row

Worksheets("Expense Report").Rows(7 & ":" & Sheet2.Rows.Count).Delete

Worksheets("Expenses").Activate
Worksheets("Expenses").Range("A1").Select

For i = 2 To lastrow

    mydate = Cells(i, 6)
    If mydate >= StartDate And mydate <= EndDate Then
    erow = Sheet2.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
    'New
    
    Application.Union(Cells(i, 2), Cells(i, 6)).Copy Destination:=Sheets("Expense Report").Cells(erow, 1)
    End If
Next i

End Sub
 
Upvote 0

Forum statistics

Threads
1,214,641
Messages
6,120,694
Members
448,979
Latest member
DET4492

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