当变量为空时,将字符串组合为不带分隔符

时间:2017-01-23 21:05:21

标签: excel excel-vba vba

我正在尝试将字符串组合到电子表格中的单行中。我使用逗号作为我的分隔符。如果变量为空白而不使用分隔符,我怎么能这样做呢?这是我的代码

FETCH_HEAD

以下是返回结果的示例。

Runtime.getRuntime().addShutdownHook(new Thread() {
    public void run() {
        // shutdown logic
    }
});

我有var1,var2,var3 .....

我想知道如果var1 =空白,var2 =空白等我如何摆脱逗号和空格。

2 个答案:

答案 0 :(得分:0)

只有5个变量,你可以轻易地强制它:

Sub test()
Dim concat$, delimit$
Dim v1, v2, v3, v4, v5, val
v1 = ""
v2 = "adfadf"
v3 = "Eligibility"
v4 = ""
v5 = "floerg"

delimit = " ,"

For Each val in Array(v1,v2,v3,v4,v5)
    If val <> "" Then
        c = c & delimit & val
    End If
Next
c = Right(c, Len(c) - Len(delimit))
Debug.Print c

End Sub

答案 1 :(得分:0)

谢谢你的回答!我稍微修改了你的代码并找到了解决方案。 v1 - v5在一个循环中被“回答”。

   Dim v1 As String, v2 As String, v3 As String, v4 As String, v5 As String, delimit As String
        Dim val As Variant
        x = ""
        v1 = ""
        v2 = ""
        v3 = ""
        v4 = ""
        v5 = ""
        delimit = ","

For Each val In Array(v1, v2, v3, v4, v5)
        If val <> "" Then
            x = x & delimit & val
        End If
    Next

    If x <> "" Then
        x = Right(x, Len(x) - Len(delimit))
        ws.Range("CU" & i).Value2 = x
    End If