[Re-post - apologies for not having included the 'code' tags first time]
As promised, a further question! I suspect I am forgetting or missing a simple/’elegant’ way of programmatically emulating a ‘Fill Right’ operation across a range of rows and columns. To illustrate what I’m talking about, I’m playing with a highly simplified fictional version of the sort of situation I normally have - usually with many cells/variables and complex IF...THEN...ELSE constructs or other conditional logic. In it’s simple ‘1 column’ form, my experimental code looks like this ....
Dim CellF9 As Range
Dim Age As Single
Dim Gender As String
Dim Weight As Single
Private Sub Worksheet_Change(ByVal Target As Range)
Application.EnableEvents = False
Set CellF9 = Worksheets(1).Range("F9")
Age = Worksheets(1).Range("F12").Value
Gender = Worksheets(1).Range("F13").Value
Weight = Worksheets(1).Range("F14").Value
If Age > 30 And Gender = "male" And Weight < 80 Then
CellF9.Value = "Group 1"
ElseIf Age > 20 And Age < 49 And Gender = "female" And Weight > 85 Then
CellF9.Value = "Group 2"
Else
CellF9.Value = "Unclassified"
End If
Application.EnableEvents = True
End Sub
... what I want to do it to extend that from column F to columns G, H.... etc., just as one would do with a ‘Fill Right’. In other words, for the G column, the value of G9 would be determined by the values in G12, G13 and G14, and so on.
When I’m faced with such requirements these days, I generally use a ‘brute force’ approach with arrays and loops etc., but that can get fairly tedious when there are lots of variables/cells, and I seem to recall (and strongly suspect) that there is a much simpler, more efficient and more elegant way of doing it. Can anyone give me some pointers - either to refresh my dusty memories or teach me things that I never knew? Thanks!!
Kind Regards, John
Bookmarks