Noob here, not sure if my title makes sense so I'll give it a better shot here. I have a userform that I have a bunch of check boxes in. I want to group certain checked boxes in to a string and output them to a cell in excel (I have that part figured out). What is happening is that when I don't have any boxes checked in the userform it errors out. I have an idea why (the delete last comma code I'm using I think is the problem), but what I can't figure out is how can I just "skip" the remove comma part if the condition of the string is empty and go to the next section. I was thinking of using the GoTo function but not sure if it is what I should be using or is the most appropriate function... I'll include my code to help out too. Thanks for any help, and I'm sure that there is plenty of code housekeeping I can do...
Noob在这里,不确定我的头衔是否合理,所以我会在这里给它一个更好的镜头。我有一个用户表单,我有一堆复选框。我想将某些复选框分组为一个字符串,然后将它们输出到excel中的一个单元格(我已经找到了那个部分)。发生的事情是,当我在userform中没有选中任何框时,它会出错。我知道为什么(我正在使用的删除最后一个逗号代码我认为是问题),但我无法弄清楚如果字符串的条件为空,我怎么能“跳过”删除逗号部分并转到下一部分。我正在考虑使用GoTo功能,但不确定它是否应该是我应该使用的或者是最合适的功能......我将包括我的代码以帮助解决问题。感谢您的帮助,我确信我可以做很多代码管理工作......
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
2 个解决方案
#1
You're very close. What you need to do is just delete the last comma if the string is not empty:
你很近。你需要做的就是删除最后一个逗号,如果字符串不为空:
If luLi <> "" Then
luLi = Left(luLi, Len(luLi) - 2)
End If
#2
If no answers are checked, luLi
or kdBl
will be NULL
and Left
'ing them will cause errors. Try setting luLi=", "
before anything else, so that the Left
can still evaluate it.(and the same for kdbl as well.)
如果没有检查答案,则luLi或kdBl将为NULL并且左键将导致错误。尝试在其他任何事情之前设置luLi =“,”,这样左派仍然可以评估它。(对于kdbl也是如此。)
If that doesn't work set luLi=", "
(two spaces) so that left can return something.
如果这不起作用设置luLi =“,”(两个空格),以便左边可以返回一些东西。
#1
You're very close. What you need to do is just delete the last comma if the string is not empty:
你很近。你需要做的就是删除最后一个逗号,如果字符串不为空:
If luLi <> "" Then
luLi = Left(luLi, Len(luLi) - 2)
End If
#2
If no answers are checked, luLi
or kdBl
will be NULL
and Left
'ing them will cause errors. Try setting luLi=", "
before anything else, so that the Left
can still evaluate it.(and the same for kdbl as well.)
如果没有检查答案,则luLi或kdBl将为NULL并且左键将导致错误。尝试在其他任何事情之前设置luLi =“,”,这样左派仍然可以评估它。(对于kdbl也是如此。)
If that doesn't work set luLi=", "
(two spaces) so that left can return something.
如果这不起作用设置luLi =“,”(两个空格),以便左边可以返回一些东西。