0

My worksheet is basically a metrics worksheet that has multiple columns that rely on a date entered into one column, but each of these later columns then rely on each other in sequence for setting dates and time periods.

I'm trying to find a way to update certain columns within the same row when that first value is changed; through the Worksheet_SelectionChange event if possible (or some other way if the result is the same).

What I want to happen: I enter a "start" date in column A. I need excel to then update column B of the same row based on the value in column A. Then I need column C (same row) to update based on the new value in column B; then I need column D to update based on the value in column C; and so on. Does this make sense?

2
  • Yes it does make sense :) But instead of using worksheet_selectionchange use the worksheet_change event :) Give it a try and if you are still stuck then post the code that you tried :) Mar 20, 2012 at 17:35
  • Good call. I'm greedy for reputation, for sure...
    – Rob I
    Mar 20, 2012 at 17:53

3 Answers 3

1

You seem to really want VBA, so I'll post another answer (quite different from my other one).

Here I use the Worksheet_Change event. It calls a function that returns an array of type Variant containing dates. This array can then be written to the sheet simply using =.

I find this slightly more complicated than just using formulas in cells, but it has the advantage of a cleaner front-end, which you seem to want.

In the sheet module:

Private Sub Worksheet_Change(ByVal Target As Range)

    If Not Intersect(Target, Range("A:A")) Is Nothing Then
        Target.Offset(0, 1).Resize(1, 3) = RelevantDates(Target.Value)
    End If

End Sub

In a code module:

Function RelevantDates(startDate As Date) As Variant
    Dim v As Variant
    ' Adapt to your needs:
    ReDim v(1 To 3)
    v(1) = startDate + 1 ' add one day
    v(2) = DateSerial(Year(v(1)), Month(v(1)), Day(v(1)) + 7) ' add one more week
    v(3) = DateSerial(Year(v(2)), Month(v(2)) + 1, Day(v(2))) ' add one month
    RelevantDates = v
End Function

Of course the above only returns 3 rather trivial dates, but you can customise this as you see fit.

enter image description here

1
  • Oops, I just realised the column headers are wrong in the picture. Should read: Enter your date here, A day later, A week after that, A month later. Too lazy to fix the picture. Dec 6, 2013 at 8:25
1

I don't see why any VBA would be required for this. Just write formulas in your cells. It's quite straightforward, really.

Here's an example where a date entered in column A changes the value in column B, which in turn changes the value in column C, and so on.

enter image description here

Row 2 shows the result of the formulas, while row 4 shows the formulas themselves (apologies for the poor formatting of these rather long formulas).

2
  • Well, that's what I was trying, but for some reason it was not working. I started with a date in Column C, which updated a date in Column E. This worked fine and always updated Column E, but the Column afterwards that depended on Column E or other columns generated off of Column E woul dnot update in sequence when the date in Column C changed. On top of that, when the latter columns were blank they displayed errors - easy enough fix to hide from view with conditional formatting, but I'd just as soon keep the worksheet "clean" and have the recalcs done behind everything in VBA
    – Jon
    Mar 21, 2012 at 12:10
  • 1
    Not working?! Is Calculation set to Automatic in your Excel options? Mar 21, 2012 at 13:20
0

Ideally, your later columns would just have formulas in them based on the previous columns. Something like =A2*100 for column B2, and =B2+88 for column C2.

If that's not easy because the calculations are difficult, consider worksheet functions, like =ComplicatedFunction(A2) for column B2.

Function ComplicatedFunction(depended_on As Range)

    If depended_on.Value <> 0 Then
        PAYEES = 1
    Else
        PAYEES = 2
    End If
End Function

If that's not possible, then you're right - a worksheet changed event could help, perhaps something like:

Public Sub Worksheet_Change(ByVal Target As Range)

    If Target.Column = 1 Then
        Debug.Print "change to column A encountered"
        Range(2,Target.Row).Value = Target.Value + 2
    ElseIf Target.Column = 2 Then
        Debug.Print "change to column B encountered"
        Range(3,Target.Row).Value = Target.Value + 99
    ElseIf Target.Column = 3 Then
        Debug.Print "change to column C encountered"
        Range(4,Target.Row).Value = Target.Value * 7
    End If

End Sub

I believe the Worksheet_Change calls will be cascaded after you update values within the sub itself.

4
  • There is nothing like WorksheetChanged ;) Mar 20, 2012 at 17:41
  • I know, it's ugly as sin. I usually try to keep from hard-coding all those rows and columns, instead using constants (amount_column) to keep it a little cleaner, or at least more readable...
    – Rob I
    Mar 20, 2012 at 17:44
  • No I didn't mean that :) What I meant was there is no event which is called WorksheetChanged :) Mar 20, 2012 at 17:46
  • Doh! Of course you're right. I was copying some code from a module, in which I had cleverly used camelcase instead of matching the form method that called it. :(
    – Rob I
    Mar 20, 2012 at 17:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.