macro create age bands
The UK's Number 1 for Microsoft Office Training Add this page to your favourites/bookmarksBookmark page
 
View printable version of pagePrintable version
Plus One Google
Customer: Sign in
Delegate: Sign in
Trainer: Log in

Forum home » Delegate support and help forum » Microsoft Excel VBA Training and help » Macro to create age bands from an age field

Macro to create age bands from an age field

resolvedResolved · Low Priority · Version 2007

replyReply Mon 16 May 2011, 12:29Delegate Sarah said...

Sarah has attended:
Excel VBA Intro Intermediate course
Access Advanced course
Access Intermediate course

Macro to create age bands from an age field

I want create a macro that I can keep in my personal workbook to create 5 year age bands from a field that has the age. So I'd want ages 0-4, 5-9,...,80-84, 85+.

I have to do this calculation an awful lot so it would be handy to have this macro ready and waiting but the field with the ages in won't necessarily be the same column all the time.

Can this be done?

For upcoming training course dates see: Pricing & availability

replyReply Mon 16 May 2011, 15:20Trainer Anthony said...

RE: Macro to create age bands from an age field

Hi Sarah, thanks for your query. It's a little difficult to create something specific without seeing your source data, but the following code should give you a few pointers on how to achieve the results you want.

First of all, select the cells with the ages in, then run the code. It will loop through the selected cells, categorising them and writing the categories into Column A of Sheet2. I've only put two categories into the code, you will have to complete the rest. It's very rough and ready, and I would probably develop this by switching to using Select Case. I would also customise to your data by writing the categories into the specific column and worksheet you want. Anyway, here is the code:

*****

Sub categorise_me()

Dim curcell As Range
Dim cellcount As Integer
cellcount = 1


For Each curcell In Selection

If curcell.Value > 0 And curcell.Value < 5 Then

Sheets("Sheet2").Range("a1").Cells(cellcount, 1).Value = "0-4"
cellcount = cellcount + 1

End If


If curcell.Value > 5 And curcell.Value < 10 Then

Sheets("Sheet2").Range("a1").Cells(cellcount, 1).Value = "5-10"
cellcount = cellcount + 1

End If



Next curcell

End Sub

*****

Hope this helps,

Anthony

 

Please browse our web site to find out more about
microsoft excel courses london and other Microsoft training courses.

Excel tip:

Display Functions on Worksheets

Functions in Excel can be difficult to recall their format/syntax

For example, you want to use the =PMT function.

Enter =PMT, then select keystroke, CTRL+SHIFT+A.

This usful memory jog, will display the arguments of a function on a worksheet, allowing the user to proceed with the generation of the function



View all Excel hints and tips


Microsoft Certified Partner Accredited Training Provider: Institute of IT Training Institute of Leadership and Management - Certified Courses Security Seal verified by visa, mastercard securecard