我制作了一個簡單的用戶表單,根據是否選中某些點擊框來插入某些文本。下面的縮短版。問題是,正如我目前撰寫的代碼一樣,如果 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
uj5u.com熱心網友回復:
您可以在添加新行之前使用該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
uj5u.com熱心網友回復:
我會稍微翻轉邏輯并在最后添加 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)
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/437776.html
