Search code examples
excelvbatextadditioncheckmark

Using a checkmark to add value to a cell


im trying to create a macro for adding text to a field i want when i click the checkmark to add a specific text to a case i have 8 checkbox each one will add a different text to the same case right now my code look like this

Private Sub CheckBox1_Click()
If Me.CheckBox1.Value = True Then
    Range("A56").Value = "Test"
    
Else
    Range("A56").Value = " "
    End If
End Sub

the problem is if i check 2 checkmark it always replace the text and i want it to add the text not replace it. and also when i uncheck the checkmark it remove everything in the case

the reason why i need it is to save time not having to write them manualy each time

i am very new to excel coding i apreciated any help you guys can give me thanks a lot for ur time


Solution

  • So the first part:

    Range("A56").value = Range("A56").value & " Text"
    

    The second part I'm assuming the real entries are more substantial than "Test" otherwise you might get some false positives, but this should work pretty well:

    
    Private Sub CheckBox1_Click()
        With Sheet1 'Change this to the proper sheet codename
            If Me.CheckBox1.Value = True Then
                .Cells(56, 1).Value = .Cells(56, 1).Value & " Testing"
            Else
                If InStr(1, .Cells(56, 1).Value, " Testing") Then
                    Dim splitarr As Variant
                    splitarr = Split(.Cells(56, 1).Value, " Testing")
                    Dim element As Variant
                    .Cells(56, 1).Value = ""
                    For Each element In splitarr
                        .Cells(56, 1).Value = .Cells(56, 1).Value & element
                    Next element
                End If
            End If
        End With
    End Sub
    

    You might want to make the entered or deleted string a variable so it is easier to change.