如果在连接公式中为空,则忽略字符串

时间:2016-02-26 10:10:30

标签: excel vba excel-vba concatenation

我有一个公式,用于连接不同列中的字​​符串。当每个列中都有数据但是如果一列是空白时,它会很有效,我会得到一个错误"无效的过程调用或参数"对于由空列形成的字符串。是否有一个我可以添加到我的代码中的子句,如果它是空的则忽略该字符串?

Sub Concatenation_for_the_nation()

'Range("H2").End(xlDown).Select
Cells(rows.Count, "H").End(xlUp).Select
For i = 1 To ActiveCell.Row
    Range("H" & i).Select
    StrStrONE = StrStrONE & "" & Selection
Next i
Cells(1, 1).Select

'Range("I2").End(xlDown).Select
Cells(rows.Count, "I").End(xlUp).Select
For j = 1 To ActiveCell.Row
    Range("I" & j).Select
    StrStrTWO = StrStrTWO & "" & Selection
Next j
Cells(1, 1).Select

'Range("J2").End(xlDown).Select
Cells(rows.Count, "J").End(xlUp).Select
For k = 1 To ActiveCell.Row
    Range("J" & k).Select
    StrStrTHREE = StrStrTHREE & "" & Selection
Next k
Cells(1, 1).Select

'Range("K2").End(xlDown).Select
Cells(rows.Count, "K").End(xlUp).Select
For l = 1 To ActiveCell.Row
    Range("K" & l).Select
    StrStrFOUR = StrStrFOUR & "" & Selection
Next l
Cells(1, 1).Select

StrStrONE = Trim(StrStrONE)
StrStrTWO = Trim(StrStrTWO)
StrStrTHREE = Trim(StrStrTHREE)
StrStrTHREE = Left(StrStrTHREE, Len(StrStrTHREE) - 3)
StrStrFOUR = Trim(StrStrFOUR)
StrStrFOUR = Left(StrStrFOUR, Len(StrStrFOUR) - 3)

Cells(14, 7) = "(ISAV(" & StrStrONE & " " & StrStrTWO & " " & StrStrTHREE & ")=1 OR (" & StrStrFOUR & ")=1)=1"


Cells(14, 7).Select



End Sub

2 个答案:

答案 0 :(得分:0)

您可以使用 ISBLANK()功能检查列是否为空

答案 1 :(得分:0)

正如user2471313所说的使用了ISBLANK()函数,或者我会添加这样的东西来检查字符串:

If StrStrONE<>"" and StrStrTWO<>"" and StrStrTHREE<>"" and StrStrFOUR<>"" then
    StrStrONE = Trim(StrStrONE)
''''your code until end
End if