如果不满足条件,如何进入VBA的下一步

不要在这里,不知道我的头衔是否有意义,所以我会在这里给它更好的一击。 我有一个用户表单,我有一堆checkbox。我想将某些选中的框分组到一个string,并将它们输出到Excel中的单元格(我已经find了这个部分)。 发生什么事情是,当我没有任何框在用户表单中检查它错误了。 我有一个想法,为什么(删除我使用的最后一个逗号代码,我认为是问题),但是我不能弄清楚的是,如果string的条件为空,我怎样才能“跳过”删除逗号部分并转到下一节。 我正在考虑使用GoTo函数,但不知道这是我应该使用还是最合适的函数…我将包括我的代码来帮助。 感谢您的帮助,我确信有足够的代码pipe理我可以做…

Dim luLi As String Dim kdBl As String 'Gather check box information for the luLi Symptoms If Chk_allergies.Value = True Then luLi = "Allergies, " End If If Chk_asthma.Value = True Then luLi = luLi & "Asthma, " End If If Chk_freqcolds.Value = True Then luLi = luLi & "Frequent Colds, " End If If Chk_persiscough.Value = True Then luLi = luLi & "Persistant Cough, " End If If Chk_sinus.Value = True Then luLi = luLi & "Sinus Problems, " End If ' If luLi = "" Then GoTo <-- Couldn't figure out how to use this... ' End If 'deleting comma and space, the 2 characters at the end of luLi luLi = Left(luLi, Len(luLi) - 2) Sheet1.Cells(lRow, 20) = luLi 'Gather check box information for the kiBl Symptoms If Chk_arthritis.Value = True Then kdBl = "Arthritis, " End If If Chk_asthma.Value = True Then kdBl = "Asthma, " End If 'If kdBl = "" Then GoTo <-- Not sure if this is the best option... ' End If 'deleting comma and space, the 2 characters at the end of kdBl kdBl = Left(kdBl, Len(kdBl) - 2) Sheet1.Cells(lRow, 21) = kdBl 

你非常接近。 你需要做的只是删除最后一个逗号,如果string不为空:

 If luLi <> "" Then luLi = Left(luLi, Len(luLi) - 2) End If 

如果没有答案被检查, luLikdBl将是NULLLeft他们将导致错误。 尝试设置luLi=", "之前,所以Left仍然可以评估它(和kdbl一样)。

如果这不起作用,设置luLi=", " (两个空格),这样左边就可以返回一些东西。