避免多余的空白VBA

时间:2018-08-14 04:05:04

标签: vba excel-vba userform

我有一个用户表单,可以将数据输入到另一个用户表单中,每次提交时都在新行中输入数据。我遇到了一个问题,其中第一个输入的数据跳过了用户表单上的一行。我该如何调整代码,以避免开头出现多余的白线,这是我的代码:

Private Sub CommandButton1_Click()
 opsvision.opsfinding.Value = opsvision.opsfinding.Value & vbNewLine & "Employees" & "---" & generalbuilder.employees.Value & " -" & Space(2) & Space(1) & """" & Me.findings.Value & """" & Space(5) & "----" & Space(3) & "Finding Conducted by: " & Worksheets("userform").Range("B3") & vbNewLine

Unload Me
End Sub
  

红线在文本框顶部显示多余的空白   The red line shows the extra white space at the top of the text box

1 个答案:

答案 0 :(得分:2)

先测试一下是否为空。否则,您将vbNewLine连接到空的起始值:

Private Sub CommandButton1_Click()
    Dim line As String

    line = "Employees" & "---" & generalbuilder.employees.Value & " -" & Space(2) & _
        Space(1) & """" & Me.findings.Value & """" & Space(5) & "----" & Space(3) & _
        "Finding Conducted by: " & Worksheets("userform").Range("B3") & vbNewLine

    If opsvision.opsfinding.Value = vbNullString Then
        opsvision.opsfinding.Value = line
    Else
        opsvision.opsfinding.Value = opsvision.opsfinding.Value & vbNewLine & line
    End If

    Unload Me
End Sub