简体   繁体   English

如何使用VBA将多个字符串值添加到Excel工作表中的单元格

[英]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.

相关问题 如何使用VBA在Excel中添加新工作表? - How to add a new sheet in excel using VBA? 如何使用VBA将多个Excel工作表合并为一个Excel工作表 - How to combine multiple Excel sheet into one Excel sheet using VBA 如何使用 VBA 将单元格值(不是范围)从 Excel 工作表插入到现有的 csv 文件中 - How to I insert cell values (not a range) from an Excel sheet to an existing csv file using VBA 如何使用 VBA 代码根据单元格值隐藏 Excel 表 - How to hide Excel Sheet based on cell value using VBA Code 如何使用excel vba将“₹”添加到单元格中(使用货币符号“₹”格式化单元格) - How to add a '₹' into a cell using excel vba (format a cell with a Currency symbol '₹' ) 如何使用 POI 将单元格注释添加到 Excel 工作表? - How to add Cell Comments to Excel sheet using POI? 如何使用VBA在Excel 2010工作表中添加选项按钮进行分组? - How to add option buttons to group in Excel 2010 sheet using VBA? Excel VBA-根据图纸上的单元格值从阵列中粘贴值 - Excel VBA - Paste values from array based on cell values on the sheet 使用VBA在Excel中工作表处于保护模式时无法更改单元格值 - Unable to change the cell values when work sheet is in protected mode in excel using vba 使用VBA将值粘贴到Excel中的工作表 - Paste values to a sheet in excel using VBA
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM