Is it possible for Excel to do this. If I enter the value 'Calculated' in a cell in…
- User
Is it possible for Excel to do this. If I enter the value 'Calculated' in a cell in column O the cell address is determined and the formula '=SUM(JX:J206)' is written to Offset(1, 1) where X is the numeric value of the cell address that was determined.
- Assistant
Yes, it is possible for Excel to perform the task you described using VBA (Visual Basic for Applications) macros. You can use the Worksheet_Change event to automatically trigger the code whenever a cell in column O is changed.
Here's an example of how you can achieve this:
- Open the Excel file and press ALT + F11 to open the Visual Basic Editor.
- Insert a new module by clicking on Insert > Module.
- In the module, paste the following code:
Private Sub Worksheet_Change(ByVal Target As Range) Dim rng As Range Dim cell As Range Dim formulaRange As Range Dim addressToWrite As String Set rng = Intersect(Target, Me.Columns("O")) If rng Is Nothing Then Exit Sub Application.EnableEvents = False For Each cell In rng If cell.Value = "Calculated" Then addressToWrite = "J" & cell.Row + 1 Set formulaRange = Me.Range(addressToWrite) formulaRange.Offset(1, 1).Formula = "=SUM(" & addressToWrite & ":J206)" End If Next cell Application.EnableEvents = True End Sub - User
In the three stage formula below, I am expecting it to do the the following; It should write the first formula to P6 It should write the second formula to P7 It should write the third formula to P8 But this is what it is doing; It is writing the first formula to K7 It is writing the second formula to M8 It is writing the third formula to O9 Here is the code: Application.EnableEvents = False Set Orng = Intersect(Target, Me.Columns("O")) If Orng Is Nothing Then Exit Sub For Each cell In Orng If cell.Value = "Calculated" Then addressToWrite = "J" & cell.Row + 1 Set formulaRange = Me.Range(addressToWrite) formulaRange.Offset(1, 1).Formula = "=SUM(" & addressToWrite & ":J206)" End If Next cell
Set Arng = Intersect(Target, Me.Columns("O")) If Arng Is Nothing Then Exit Sub For Each cell In Arng If cell.Value = "Calculated" Then addressToWrite = "L" & cell.Row + 1 Set formulaRange = Me.Range(addressToWrite) formulaRange.Offset(2, 1).Formula = "=SUM(" & addressToWrite & ":L206)" End If Next cell
Set Hrng = Intersect(Target, Me.Columns("O")) If Hrng Is Nothing Then Exit Sub For Each cell In Hrng If cell.Value = "Calculated" Then addressToWrite = "N" & cell.Row + 1 Set formulaRange = Me.Range(addressToWrite) formulaRange.Offset(3, 1).Formula = "=SUM(" & addressToWrite & ":N206)" End If Next cell Application.EnableEvents = True
- Assistant
The issue is with the offsets used in each iteration of the loop. When using the
.Offsetmethod, the numbers inside the parentheses indicate the number of rows and columns to offset from the initial range.In your code, you are offsetting 1 row down and 1 column to the right for the first formula, 2 rows down and 1 column to the right for the second formula, and 3 rows down and 1 column to the right for the third formula. This is causing the formulas to be written to the wrong cells.
To fix it, you can adjust the offsets as follows:
- User
With your ammended code, this is what it is doing; It is writing the first formula to K6 It is writing the second formula to M7 It is writing the third formula to O8 In this instance, I am entering the Value 'Calculated' into O5 so I am expecting it to do the the following; It should write the first formula to P6 It should write the second formula to P7 It should write the third formula to P8
- Assistant
Apologies for the confusion. To achieve the desired outcome, you need to adjust the offset for the formula range. Here's the modified code:
Application.EnableEvents = False Set Orng = Intersect(Target, Me.Columns("O")) If Orng Is Nothing Then Exit Sub For Each cell In Orng If cell.Value = "Calculated" Then addressToWrite = "J" & cell.Row Set formulaRange = Me.Range(addressToWrite) formulaRange.Offset(1, 1).Formula = "=SUM(" & addressToWrite & ":J206)" End If Next cell Set Arng = Intersect(Target, Me.Columns("O")) If Arng Is Nothing Then Exit Sub For Each cell In Arng If cell.Value = "Calculated" Then addressToWrite = "L" & cell.Row Set formulaRange = Me.Range(addressToWrite) formulaRange.Offset(1, 1).Formula = "=SUM(" & addressToWrite & ":L206)" End If Next cell Set Hrng = Intersect(Target, Me.Columns("O")) If Hrng Is Nothing Then Exit Sub For Each cell In Hrng If cell.Value = "Calculated" Then addressToWrite = "N" & cell.Row Set formulaRange = Me.Range(addressToWrite) formulaRange.Offset(1, 1).Formula = "=SUM(" & addressToWrite & ":N206)" End If Next cell Application.EnableEvents = True - User
In excel what is the key code to select all the cell downwards
- Assistant
The key code to select all the cells downwards in Excel is "Ctrl + Shift + Arrow Down". When you press these keys together, Excel will select all the contiguous cells from the current active cell down to the last non-empty cell in the column.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.