Share via

Excel VBA code - HELP

Anonymous
2020-03-12T14:10:23+00:00

Okay, first of all - I am not an expert in VBA codes, so it is already a challenge for me. 

Let's say, I have 2 cells A1 and B1

I want to make it that:

If in A1 appears "TRUE" - B1 - shows the date and time of input 

If in A1 appears "FALSE" - B1 - shows "not yet" 

I want it to be automated (without  ActiveX/Form Controls) - so you enter the text and it is automatically works.

My code currently is:

Private Sub Worksheet_Change(ByVal Target As Range)

    If Target.Cells.Count > 1 Then Exit Sub

    If Not Intersect(Target, Range("A1:A15")) Is Nothing Then

        With Target(1, 2)

        .Value = Date & " " & Time

        .EntireColumn.AutoFit

        End With

    End If

End Sub

I took it from tutorial and I am not sure, that it works properly, but it changes the cell as I want it (if I input something next to it), but it doesn't react if the cell "True" changes to "False" and, unfortunately, I am failed to figure out how to make it work.

Microsoft 365 and Office | Excel | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

Answer accepted by question author

HansV 462.6K Reputation points
2020-03-12T15:21:24+00:00

Try this version:

Private Sub Worksheet_Change(ByVal Target As Range)

    Dim rng As Range

    If Not Intersect(Target, Range("A1:A15")) Is Nothing Then

        Application.ScreenUpdating = False

        Application.EnableEvents = False

        For Each rng In Intersect(Target, Range("A1:A15"))

            If rng.Value = True Then

                If rng.Offset(0, 1).Value = "" Or LCase(rng.Offset(0, 1).Value) = "not yet" Then

                    rng.Offset(0, 1).Value = Now

                Else

                    ' Value had already been set, so leave it alone

                End If

            Else

                rng.Offset(0, 1).Value = "not yet"

            End If

        Next rng

        Application.EnableEvents = True

        Application.ScreenUpdating = True

    End If

End Sub

Was this answer helpful?

1 person found this answer helpful.
0 comments No comments

5 additional answers

Sort by: Most helpful
  1. HansV 462.6K Reputation points
    2020-03-13T12:29:12+00:00

    Do you mean that the cells in A1:A15 contain formulas? The Worksheet_Change event procedure does indeed ignore formulas. Instead of referring to A1:A15 in the code, you could refer to the cells that are edited by the user and that determine the return value of the formulas in A1:A15.

    Was this answer helpful?

    0 comments No comments
  2. Anonymous
    2020-03-13T11:43:24+00:00

    Everything was beautiful and worked properly, unless... 

    This "TRUE" and "FALSE" option are changed automatically and script, unfortunately, doesn't react on it. 

    The code only works, if I enter text manually, but if it happens manually it doesn't understand it all and nothing changes...

    Was this answer helpful?

    0 comments No comments
  3. HansV 462.6K Reputation points
    2020-03-13T09:55:04+00:00

    If you want to apply this to A1:A15 and K1:K15 (setting the time in B1:B15 and K1:K15, respectively), change both occurrences of Range("A1:A15") in the code to Range("A1:A15,K1:K15").

    If you want something else, please explain.

    Was this answer helpful?

    0 comments No comments
  4. Anonymous
    2020-03-13T06:55:58+00:00

    Thank you very much! 

    That is exactly, what I wanted. 

    Short additional question: if I want to apply it to one more column, where should  I put the additional range?

    Was this answer helpful?

    0 comments No comments