Separate individual actions in VBA Code

  • Hello everyone,


    I am not very familiar with VBA coding and somehow managed to create the following code to receive a popup message as soon as one of four dates gets selected more than two times. (E1-E4 are the cells in which there is a sum formula to count the dates in a column)

    Now my issue is that as soon as one of the "If" conditions are met, the message of the condition met will appear for every entry after that regardless if it's a different date.


    Does someone know how i have to change the code so it works?


    Code:


    Thanks a lot for any help!!

  • Welcome to the Forum. Please read the Forum Rules to understand how the Forum works and why I have added Code Tags to your post


    All VBA code posted in the forum must be wrapped in code tags, which you omitted, including single-line code snippets.Be sure to use them in future posts.


    How to use code tags

    Note: no apostrophe in the tags, just used for demonstration here.

    ['code]


    your code goes between these tags


    ['/code]


    Or, just highlight all of the code and press the <> in the post menu above button to add the code tags.


    Thanks.

  • You don't set a Range for Target in Event Code. Target refers to the selection in such code.


    Try the calculate event


    Code
    1. Private Sub Worksheet_Calculate()
    2. If Me.Range("E1").Value > 2 Then MsgBox "MI 14.10.2020 / 0900-1000 bereits ausgebucht! Bitte wählen Sie ein anderes Datum!"
    3. If Me.Range("E2").Value > 2 Then MsgBox "FR 16.10.2020 / 1100-1200 bereits ausgebucht! Bitte wählen Sie ein anderes Datum!"
    4. If Me.Range("E3").Value > 2 Then MsgBox "DI 20.10.2020 / 1600-1700 bereits ausgebucht! Bitte wählen Sie ein anderes Datum!"
    5. If Me.Range("E4").Value > 2 Then MsgBox "DO 22.10.2020 / 1330-1430 bereits ausgebucht! Bitte wählen Sie ein anderes Datum!"
    6. End Sub