删除第一个空行
Delete first blank line
我制作了一个简单的用户表单,根据是否选中某些点击框来插入某些文本。下面是简化版。问题是,按照我当前编写的代码,如果未单击 cbx1 但单击了 cbx2,则粘贴到的字符串会以新行开头。有没有一种简单的方法可以在末尾添加类似“If [first line of strNewText] = vbNewLine[?] Then [delete first line of strNewText]”之类的东西?更好的解决方案?谢谢!
Private Sub cmdInsert_Click()
Application.ScreenUpdating = False
Dim strToAdd As String
Dim text1 As String
Dim text2 As String
Dim text2 As String
text1 = "This is Text 1."
text2 = "This is Text 2."
text3 = "This is Text 3."
If cbx1 = True Then strToAdd = text1
If cbx2 = True Then strToAdd = strToAdd & vbNewLine & text2
If cbx3 = True Then strToAdd = strToAdd & vbNewLine & text3
With ActiveDocument
Selection.Text = strToAdd
Selection.Collapse
End With
Application.ScreenUpdating = True
End Sub
您可以使用 Iif
函数在添加新行之前测试 strToAdd
是否包含任何文本。
Private Sub cmdInsert_Click()
Application.ScreenUpdating = False
Dim strToAdd As String
Dim text1 As String
Dim text2 As String
Dim text3 As String
text1 = "This is Text 1."
text2 = "This is Text 2."
text3 = "This is Text 3."
If cbx1 = True Then strToAdd = text1
If cbx2 = True Then strToAdd = strToAdd & IIf(strToAdd = vbNullString, "", vbNewLine) & text2
If cbx3 = True Then strToAdd = strToAdd & IIf(strToAdd = vbNullString, "", vbNewLine) & text3
With ActiveDocument
Selection.Text = strToAdd
Selection.Collapse
End With
Application.ScreenUpdating = True
End Sub
我会稍微翻转逻辑并在末尾添加 vbNewLine。这样,无论复选标记的组合如何,我都知道字符串总是以 vbNewLine 结尾。然后删除尾随的 vbNewLine 是一件简单的事情。这种方法还使代码更简洁:
If cbx1 = True Then strToAdd = strToAdd & text1 & vbNewLine
If cbx2 = True Then strToAdd = strToAdd & text2 & vbNewLine
If cbx3 = True Then strToAdd = strToAdd & text3 & vbNewLine
If Len(strToAdd) > 0 Then strToAdd = Mid(strToAdd, 1, Len(strToAdd) - 2)
我制作了一个简单的用户表单,根据是否选中某些点击框来插入某些文本。下面是简化版。问题是,按照我当前编写的代码,如果未单击 cbx1 但单击了 cbx2,则粘贴到的字符串会以新行开头。有没有一种简单的方法可以在末尾添加类似“If [first line of strNewText] = vbNewLine[?] Then [delete first line of strNewText]”之类的东西?更好的解决方案?谢谢!
Private Sub cmdInsert_Click()
Application.ScreenUpdating = False
Dim strToAdd As String
Dim text1 As String
Dim text2 As String
Dim text2 As String
text1 = "This is Text 1."
text2 = "This is Text 2."
text3 = "This is Text 3."
If cbx1 = True Then strToAdd = text1
If cbx2 = True Then strToAdd = strToAdd & vbNewLine & text2
If cbx3 = True Then strToAdd = strToAdd & vbNewLine & text3
With ActiveDocument
Selection.Text = strToAdd
Selection.Collapse
End With
Application.ScreenUpdating = True
End Sub
您可以使用 Iif
函数在添加新行之前测试 strToAdd
是否包含任何文本。
Private Sub cmdInsert_Click()
Application.ScreenUpdating = False
Dim strToAdd As String
Dim text1 As String
Dim text2 As String
Dim text3 As String
text1 = "This is Text 1."
text2 = "This is Text 2."
text3 = "This is Text 3."
If cbx1 = True Then strToAdd = text1
If cbx2 = True Then strToAdd = strToAdd & IIf(strToAdd = vbNullString, "", vbNewLine) & text2
If cbx3 = True Then strToAdd = strToAdd & IIf(strToAdd = vbNullString, "", vbNewLine) & text3
With ActiveDocument
Selection.Text = strToAdd
Selection.Collapse
End With
Application.ScreenUpdating = True
End Sub
我会稍微翻转逻辑并在末尾添加 vbNewLine。这样,无论复选标记的组合如何,我都知道字符串总是以 vbNewLine 结尾。然后删除尾随的 vbNewLine 是一件简单的事情。这种方法还使代码更简洁:
If cbx1 = True Then strToAdd = strToAdd & text1 & vbNewLine
If cbx2 = True Then strToAdd = strToAdd & text2 & vbNewLine
If cbx3 = True Then strToAdd = strToAdd & text3 & vbNewLine
If Len(strToAdd) > 0 Then strToAdd = Mid(strToAdd, 1, Len(strToAdd) - 2)