2017-01-23 82 views
0

我想將字符串組合成電子表格中的一行。我使用逗號作爲我的分隔符。我怎麼能這樣,如果一個變量是空白不使用分隔符。這裏是我的代碼當變量爲空時組合字符串不帶分隔符

Concat = Join(Array(var1, var2, var3,` var4, var5), " ,") 
ws.Range("CU" & i).Value2 = Concat 

這裏是返回結果的樣本。

, , , , 
, ,Eligibility date , , 

我有VAR1,VAR2,VAR3 .....

我想知道我是如何擺脫逗號和空格,如果VAR1 =空白,VAR2 =空白等

+0

你可以爲For/Each邏輯代替'Join'函數。此外,它是「分隔符」而不是「deliminator」:) –

+0

您期望的結果是什麼? –

+0

謝謝。我希望它讀取第二個條目的「資格日期」 – Chris2015

回答

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 
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 
相關問題