VB code to automate a function for a variable range

Ziggy8

New Member
Joined
Nov 12, 2018
Messages
1
VB code to go to the end of the column:
In a Sheet called “Student data”, I have a column called “DOB” which is populated with a variable range of dates of birth of students. I’m unable to predict the number of populated cells in this column as the number of students in the capture will vary.


VB code to extract and paste extraction:
I need to extract just the month number of each date of birth held in the column “DOB”
=MONTH(DOB)
and paste these numbers into a column called “MONTH_No” on a new sheet called “Analysis”


VB code to automate a function down a column until blank value reached:
Once the MONTH column on the “Analysis” sheet is correctly populated I need to automate a function in the next column called "TOB" (term of birth) to interpret each Month number and trigger seasons such as Autumn, Spring or Summer.

Where MONTH column on Analysis sheet is (A2)
=IF(MONTH(A2)<=3,"Spring",IF(MONTH(A2)<=8,"Summer","Autumn"))

In VB how do I make the formula automatically travel down the MONTH column for each cell which holds a value, so as to return a season in the TOB column?
Any help would be much appreciated. Thanks in anticipation.
 

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
Assuming "DOB" is in column A, try:
Code:
Sub Ziggy8()
    Application.ScreenUpdating = False
    Dim LastRow1 As Long, LastRow2 As Long
    LastRow1 = Sheets("Student data").Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    Dim rng As Range
    For Each rng In Sheets("Student data").Range("A2:A" & LastRow1)
        Sheets("Analysis").Cells(Rows.Count, "A").End(xlUp).Offset(1, 0) = Month(rng)
    Next rng
    LastRow2 = Sheets("Analysis").Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    Sheets("Analysis").Range("B2:B" & LastRow2).Formula = "=IF(RC[-1]<=3,""Spring"",IF(RC[-1]<=8,""Summer"",""Autumn""))"
    Application.ScreenUpdating = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,952
Messages
6,175,596
Members
452,658
Latest member
GStorm

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