I am new to VBA so I'm not sure what is wrong with my code or if this is the best way to do what I want. I have a gigantic raw data sheet titled "Data" that has lots of columns, one of them being the date. First I want the macro to create four new worksheets titled spring, summer, fall and winter and place them at the end of the sheet count. Then I want to search through the date column in "Data" for a month, and copy that corresponding row into the season worksheet that corresponds to that month. This is what I have so far- I am getting an error with my if then statements. I defined the date column as an array but I am not sure if I even need to define it at all. However when this macro will be used the number of rows in "data" will not be constant. Thanks.
Sub Copy_Sorted_Data()
Worksheets.Add(After:=Worksheets(Worksheets.Count)).Name = "Spring"
Worksheets.Add(After:=Worksheets(Worksheets.Count)).Name = "Summer"
Worksheets.Add(After:=Worksheets(Worksheets.Count)).Name = "Fall"
Worksheets.Add(After:=Worksheets(Worksheets.Count)).Name = "Winter"
Dim Data As Worksheet
Dim Spring As Worksheet
Dim Summer As Worksheet
Dim Fall As Worksheet
Dim Winter As Worksheet
Dim Entered() As Date
Dim size As Integer
Dim i As Integer
size = WorksheetFunction.CountA(Worksheets("Data").Columns(1))
ReDim numbers(size)
For i = 1 To size
numbers(i) = Cells(i, 9).Value
Next i
If Entered = March Or April Or May Then ActiveSheet.Row.Value.Copy Destination:=Worksheets("Spring")
If Entered = June Or July Or August Then ActiveSheet.Row.Value.Copy Destination:=Worksheets("Summer")
If Entered = September Or October Or November Then ActiveSheet.Row.Value.Copy Destination:=Worksheets("Fall")
If Entered = December Or January Or February Then ActiveSheet.Row.Value.Copy Destination:=Worksheets("Winter")
End If
End If
End If
End If
End Sub
1st. Remove these codes:
If Entered = March Or April Or May Then ActiveSheet.Row.Value.Copy Destination:=Worksheets("Spring")
If Entered = June Or July Or August Then ActiveSheet.Row.Value.Copy Destination:=Worksheets("Summer")
If Entered = September Or October Or November Then ActiveSheet.Row.Value.Copy Destination:=Worksheets("Fall")
If Entered = December Or January Or February Then ActiveSheet.Row.Value.Copy Destination:=Worksheets("Winter")
End If
End If
End If
End If
2nd. When you want to read data from column 9 then find size
from that column:
size = WorksheetFunction.CountA(Worksheets("Data").Columns(9))
3rd. When you want to Find your result:
Dim SpringRows as long
Dim Summer as long
Dim Fall as Long
Dim WinterRows as Long
For i = 1 To size
numbers(i) = Cells(i, 9).Value
'working with month numbers is better
If (Month(numbers(i)) = 3) OR (Month(numbers(i)) = 4) OR (Month(numbers(i)) = 5) then
SpringRows = SpringRows + 1
Worksheets("Spring").Cells(SpringRows, 1).Value = numbers(i)
End If
' ... and so on, for others
Next i
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.