Copy data based on month?


S

Sophie

I have a spreadsheet(sheet1) that data is inserted throughout the year. I
need a formula or vb code so that by entering a month in cell A1, it will
copy all entries in that month and paste it onto another sheet(sheet2).

Data col= A to E (Col A= date)

Thank you
 
Ad

Advertisements

G

Gary''s Student

Enter the following event macro in the worksheet code area:

Private Sub Worksheet_Change(ByVal Target As Range)
Set a1 = Range("A1")
If Intersect(Target, a1) Is Nothing Then Exit Sub
Application.EnableEvents = False
Dim s1 As Worksheet, s2 As Worksheet
Set s1 = Sheets("Sheet1")
Set s2 = Sheets("Sheet2")
s2.Cells.Clear
k = 1
n = Cells(Rows.Count, "A").End(xlUp).Row
v = a1.Value
For i = 1 To n
If Month(Cells(i, 1).Value) = v Then
Cells(i, 1).EntireRow.Copy s2.Cells(k, 1)
k = k + 1
End If
Next
Application.EnableEvents = True
End Sub

After the installation, type a month (as a number) in A1 (4 for March) and
the March data will be copied to Sheet2.

Type a new value, and the data on Sheet2 will be refreshed.


Because it is worksheet code, it is very easy to install and use:

1. right-click the tab name near the bottom of the window
2. select View Code - this brings up a VBE window
3. paste the stuff in and close the VBE window

If you save the workbook, the macro will be saved with it.

To remove the macro:

1. bring up the VBE windows as above
2. clear the code out
3. close the VBE window

To learn more about macros in general, see:

http://www.mvps.org/dmcritchie/excel/getstarted.htm

To learn more about Event Macros (worksheet code), see:

http://www.mvps.org/dmcritchie/excel/event.htm
 
Ad

Advertisements

S

Sophie

Thanks very much it works.
--
Sophie


Gary''s Student said:
Enter the following event macro in the worksheet code area:

Private Sub Worksheet_Change(ByVal Target As Range)
Set a1 = Range("A1")
If Intersect(Target, a1) Is Nothing Then Exit Sub
Application.EnableEvents = False
Dim s1 As Worksheet, s2 As Worksheet
Set s1 = Sheets("Sheet1")
Set s2 = Sheets("Sheet2")
s2.Cells.Clear
k = 1
n = Cells(Rows.Count, "A").End(xlUp).Row
v = a1.Value
For i = 1 To n
If Month(Cells(i, 1).Value) = v Then
Cells(i, 1).EntireRow.Copy s2.Cells(k, 1)
k = k + 1
End If
Next
Application.EnableEvents = True
End Sub

After the installation, type a month (as a number) in A1 (4 for March) and
the March data will be copied to Sheet2.

Type a new value, and the data on Sheet2 will be refreshed.


Because it is worksheet code, it is very easy to install and use:

1. right-click the tab name near the bottom of the window
2. select View Code - this brings up a VBE window
3. paste the stuff in and close the VBE window

If you save the workbook, the macro will be saved with it.

To remove the macro:

1. bring up the VBE windows as above
2. clear the code out
3. close the VBE window

To learn more about macros in general, see:

http://www.mvps.org/dmcritchie/excel/getstarted.htm

To learn more about Event Macros (worksheet code), see:

http://www.mvps.org/dmcritchie/excel/event.htm
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top