[英]How to add multiple string values to a cell in a excel sheet using vba
My code below check if column S and T cells are empty or not. 我下面的代码检查S和T列的单元格是否为空。 If the cells are empty a text goes into column U saying it cannot be blank.
如果单元格为空,则文本将进入U列,说明该文本不能为空。 My Problem is the cell can take one string at a time, i am looking for a way to concatenate the string in a single cell.
我的问题是该单元一次只能容纳一个字符串,我正在寻找一种将字符串连接到单个单元中的方法。 Please help.
请帮忙。 Thanks.
谢谢。
My Code: 我的代码:
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
Store the string in a string variable, rather than writing it directly into the cell. 将字符串存储在字符串变量中,而不是将其直接写入单元格中。 Something like:
就像是:
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
You can use a String
to store the type of errors you want to write in Column "U": 您可以使用
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.