Search code examples
excelvbauserform

Printing a string in Excel VBA after selecting multiple checkboxes


I am using a userform on excel VBA to enter data into a different sheet but I have not found a way to concatenate multiple values together and put a comma in between each input. An example using the attached picture would be if Dusty Dry and Static were all selected the value that would be entered into the cell would be "Dusty, Dry, Static". Is there a way to do something like:

Range("R4").Value = If Dusty.Value = true then "Dusty"

enter image description here


Solution

  • Try,

    Range("R4").Value = iif(dusty, "Dusty ", "") & _
                        iif(dry, "Dry ", "") & _
                        iif(damp, "Damp", "") & _
                        iif(static, "Static", "") & _
                        iif(abrasive, "Abrasive ", "") & _
                        iif(cohesive, "Cohesive ", "") & _
                        iif(hygroscopic, "Hygroscopic ", "") & _
                        iif(friable, "Friable ", ""))
    Range("R4").Value = application.trim(Range("R4").Value)
    Range("R4").Value = replace(Range("R4").Value, " ", ", ")