Forum Discussion

Manuelalk's avatar
Manuelalk
Copper Contributor
Apr 07, 2025

Help with code for "division of work"

I do need a bit of help on this code. I am sorry if i do something wrong with showing you the code and so on. Its my first time here. 

Private Sub Worksheet_Change(ByVal Target As Range)
    Dim Oldvalue As String
    Dim Newvalue As String
    Dim Arr() As String
    Dim Temp As String
    Dim i As Integer
    Dim gefunden As Boolean

    ' Fehlerbehandlung aktivieren, falls etwas schief geht
    On Error GoTo Exitsub

    ' Überprüfen, ob mehr als eine Zelle bearbeitet wurde; falls ja, beenden
    If Target.Cells.Count > 1 Then Exit Sub

    ' Überprüfen, ob sich die bearbeitete Zelle in einem bestimmten Spaltenbereich befindet (Spalten 2 bis 8)
    If Target.Column >= 2 And Target.Column <= 8 Then
        
        ' Überprüfen, ob die Zelle eine Dropdown-Liste hat
        If Target.Validation.Type = 3 Then
            
            ' Verhindert, dass die Zellenereignisse wieder ausgelöst werden, während der Code läuft
            Application.EnableEvents = False

            ' Speichern des neuen Werts (der gerade eingegeben wurde) und des alten Werts (vor der Änderung)
            Newvalue = Target.Value
            Application.Undo  ' Rückgängig machen der letzten Aktion
            Oldvalue = Target.Value

            ' Wenn der Benutzer aus der Dropdown-Liste ein leeres Feld gewählt hat, soll die Zelle gelöscht werden
            If Trim(Newvalue) = "" Then
                Target.Value = ""  ' Zelle leeren
                GoTo Exitsub  ' Verlassen des Codes, wenn das Feld leer ist
            End If

            ' Logik, um bestehende Werte zu aktualisieren, wenn ein Name aus der Dropdown-Liste gewählt wird
            Arr = Split(Oldvalue, ", ")  ' Aufteilen des alten Wertes in ein Array von Namen
            Temp = ""  ' Temporäre Variable für das Ergebnis
            gefunden = False  ' Flag, um zu überprüfen, ob der neue Wert bereits im alten Wert enthalten ist

            ' Schleife durch das Array und prüft, ob der neue Name schon vorhanden ist
            For i = LBound(Arr) To UBound(Arr)
                If Trim(Arr(i)) = Trim(Newvalue) Then
                    gefunden = True  ' Name wurde gefunden, keine weiteren Änderungen notwendig
                Else
                    ' Wenn der Name noch nicht in Temp ist, füge ihn hinzu
                    If Temp = "" Then
                        Temp = Arr(i)
                    Else
                        Temp = Temp & ", " & Arr(i)
                    End If
                End If
            Next i

            ' Wenn der neue Wert nicht gefunden wurde, wird er zum bestehenden Text hinzugefügt
            If Not gefunden Then
                If Oldvalue = "" Then
                    Temp = Newvalue  ' Wenn der alte Wert leer ist, nur den neuen Wert setzen
                Else
                    Temp = Oldvalue & ", " & Newvalue  ' Wenn der alte Wert nicht leer ist, den neuen Wert hinzufügen
                End If
            End If

            ' Setze den Wert der Zelle auf den zusammengesetzten Text
            Target.Value = Temp
        End If
    End If

Exitsub:
    ' Fehlerbehandlung - sicherstellen, dass Ereignisse wieder aktiviert werden
    Application.EnableEvents = True
End Sub

 

This code is part of a scheduling system in my department. It is designed to make scheduling easier by using dropdown menus so that not all names have to be entered manually each time. The cells are populated based on the list of workers so that all workers in this dropdown list appear. An empty field has also been included to essentially allow for cell clearing with the mouse, without the need for the keyboard.
The first function is to assign two workers to a job site or cell, which are separated by a comma and appear consecutively after both are selected one after the other. - It works.

The second function is to also allow names to be removed, either by selecting the empty cell in the dropdown menu or by clicking on the name already in the cell to delete it. An example would be: A, B --> One clicks on A again --> only B remains in the cell. Conversely, if you click on B in the dropdown, only A should remain in the cell. - This also works.

But there is a problem. When I manually type into this field using the keyboard, an error occurs. What does work is that if the field is empty, I can type anything into it and it will be saved in the cell exactly as I typed it. However, if the field was previously filled using the dropdown (e.g., A), and I type A bis 9 Uhr, the resulting value in the cell becomes A, A bis 9 Uhr instead of just A bis 9 Uhr. The distinction between manual input (which should overwrite the entire cell even if a value from the dropdown was there before) doesn’t work.

Just for context: the original code comes from a YouTube tutorial and was slightly modified with help from other tutorials, etc. After that, it was adjusted with the help of ChatGPT and explanations were added. The problem is that 1. I’m not a programmer and only understand a little of the code, and 2. I spent several hours trying to fix the issue with ChatGPT. But none of the changes had any effect or they ended up breaking the original functionality of the code.

Some attempted solutions included comparing the new text based on length, but that didn’t work because longer names in the dropdown also caused existing values to be deleted.
The manualinput function simply didn’t work — I have no idea where ChatGPT integrated the code incorrectly.

So I’d appreciate it if someone could maybe implement this small change or explain where and how to do it so it works. I’ll now show some images as examples.

This is how it works:
In the first step, I select "Bachmann" from the dropdown list – that works fine.
Next, I want to add "Glanzer", and that also works as expected – see the third picture.

However, the issue arises when "Bachmann" is already in the cell (first picture), and I manually type something like "Bachmann until 10 o'clock". Once I press Enter, the text is added again, resulting in something like the last picture: "Bachmann, Bachmann until 10 o'clock".

The fourth picture shows the state before hitting Enter.

 

 

can anyone help me here? 

Resources