[英]How to add multiple string values to a cell in a excel sheet using vba
我下面的代码检查S和T列的单元格是否为空。 如果单元格为空,则文本将进入U列,说明该文本不能为空。 我的问题是该单元一次只能容纳一个字符串,我正在寻找一种将字符串连接到单个单元中的方法。 请帮忙。 谢谢。
我的代码:
For pos2 = 1 To .UsedRange.Rows.Count + 1 Step 1
If (IsEmpty(.Cells(pos2, "S").Value) = True) Then
.Cells(pos2, "U").Value = "Description can't be blank"
End If
If (IsEmpty(.Cells(pos2, "T").Value) = True) Then
.Cells(pos2, "U").Value = "Criteria can't be blank"
End If
Next pos2
将字符串存储在字符串变量中,而不是将其直接写入单元格中。 就像是:
Dim strErrors as string
For pos2 = 1 To .UsedRange.Rows.Count + 1 Step 1
If (IsEmpty(.Cells(pos2, "S").Value) = True) Then
strErrors = "Description can't be blank. "
End If
If (IsEmpty(.Cells(pos2, "T").Value) = True) Then
strErrors = strErrors & "Criteria can't be blank"
End If
.cells(pos2, "U").value = strErrors
Next pos2
您可以使用String
将要写入的错误类型存储在“ U”列中:
Dim ErrStr As String
With Sheets("Sheet1")
For pos2 = 1 To .UsedRange.Rows.Count + 1 Step 1
ErrStr = "" ' reset the error string for each row
If IsEmpty(.Cells(pos2, "S").Value) Then
ErrStr = "Description can't be blank"
End If
If IsEmpty(.Cells(pos2, "T").Value) Then
' just to make it clearer
If ErrStr <> "" Then
ErrStr = ErrStr & " ; "
End If
ErrStr = ErrStr & "Criteria can't be blank"
End If
.Cells(pos2, "U").Value = ErrStr
Next pos2
End With
只需使用&
喜欢
.Cells(pos2, "U").Value = .Cells(pos2, "U").Value & "Criteria can't be blank"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.