简体   繁体   English

Excel VBA-如果列B包含任何值,则使用值更新列A。 如果列B不包含任何值,则不要运行宏

[英]Excel VBA - Update column A with a value if column B contains any value. If column B contains no values then do not run the macro

In my scenario I have four columns, columns AD. 在我的场景中,我有四个列,即AD列。 If column B contains any value whatsoever then the matching row in column A must be updated to contain a predetermined value. 如果B列包含任何值,则A列中的匹配行必须更新为包含预定值。 The same macro is applied for columns C and D. I have code right now that achieves that result: 相同的宏应用于C和D列。我现在有代码可以实现该结果:

Sub Update_Column_Based_On_Column_Value1()
On Error Resume Next
    Dim ws As Worksheet
    Dim lRow As Long

    Set ws = ThisWorkbook.Sheets("Sheet1")

    With ws
        lRow = .Range("B" & .Rows.Count).End(xlUp).Row

        .Range("A1:A" & lRow).SpecialCells(xlCellTypeBlanks).Formula = "=If(B1<>"""",""PREDETERMINED VALUE"","""")"
        .Range("A1:A" & lRow).Value = .Range("A1:A" & lRow).Value
    End With
End Sub

When column B contains a value the macro will write "PREDETERMINED VALUE" in the corresponding cell in column A. 当B列包含一个值时,宏将在A列的相应单元格中写入“ PREDETERMINED VALUE”。

An issue occurs when a column does not contain any values at all. 当列完全不包含任何值时,就会发生问题。 What happens is the macro will write my new value to nearly all of the blank cells in the entire data-set. 发生的是宏将把我的新值写入整个数据集中几乎所有的空白单元格。

Thank you in advance for your time! 预先感谢您的宝贵时间! I apologize if my question is noobish, I am still very new to VBA. 我很抱歉,如果我的问题有点笨拙,那么我对VBA还是很陌生。

The use of If WorksheetFunction.CountA(ws.Range("B:B")) = 1 in the comment section to avoid the problem is a good attempt but there can be exceptions as discussed below. 在注释部分中使用If WorksheetFunction.CountA(ws.Range("B:B")) = 1来避免该问题是一个很好的尝试,但是可能会有如下所述的异常。 Test it several times using various scenarios (especially using blank range) to see if you are getting the desired result every single time. 使用各种方案(尤其是使用空白范围)对它进行多次测试,以查看您是否每次都获得期望的结果。

.SpecialCells attempts to simplify the codes, however sometime the .SpecialCells(xlCellTypeBlanks) VBA function does not work as expected in Excel . .SpecialCells尝试简化代码,但是有时.SpecialCells(xlCellTypeBlanks) VBA函数无法按预期在Excel中工作

Also, the statement On Error Resume Next shouldn't be used as far as practicable. 另外,不应尽可能使用On Error Resume Next语句。 But if you must, be sure to insert the On Error GoTo 0 statement ASAP as you don't want to mask other errors. 但是,如果必须,请确保尽快插入On Error GoTo 0语句,因为您不想掩盖其他错误。

Instead of .SpecialCells , you may use For Each loop to avoid this problem. 可以使用For Each循环代替.SpecialCells来避免此问题。 So let's see how it looks: 因此,让我们看一下它的外观:

Sub Update_Column_Based_On_Column_Value1()
    Dim ws As Worksheet, lRow As Long, r As Range
    Set ws = ThisWorkbook.Sheets("Sheet1")
    With ws
        lRow = .Range("B" & .Rows.Count).End(xlUp).Row
        For Each r In .Range("A1:A" & lRow)
            If IsEmpty(r) Then
                r.Formula = "=If(B" & r.Row & "<>"""",""PREDETERMINED VALUE"","""")"
                r = r.Value
            End If
        Next
    End With
End Sub

Here is the answer everyone! 这是大家的答案!

Sub Update_Column_Based_On_Column_Value_1()
    On Error Resume Next
        Dim ws As Worksheet
        Dim lRow As Long

        Set ws = ThisWorkbook.Sheets("Sheet1")

        If WorksheetFunction.CountA(ws.Range("B:B")) = 1 Then

        Else

            With ws
                lRow = .Range("B" & .Rows.Count).End(xlUp).Row
                .Range("A1:A" & lRow).SpecialCells(xlCellTypeBlanks).FormulaR1C1 = "=If(LEN(RC2),""NEW TEXT HERE"", TEXT(,))"
                .Range("A1:A" & lRow).Value = .Range("A1:A" & lRow).Value
            End With
        End If
    End Sub

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Excel VBA-如果B列中的单元格包含一个值,则A列等于“值”,但不要覆盖现有的A列数据 - Excel VBA - If a cell in column B contains a value then column A equals “value” BUT do not overwrite existing column A data Excel VBA如果B列包含一个值,然后将该值复制到A列,则不会覆盖现有的A列数据 - Excel VBA if Column B contains a value then copy that value to Column A do not overwrite existing Column A data EXCEL 如何检查A列是否包含特定值,B列是否包含2个特定值? - EXCEL How to check if Column A contains specific value and Column B contains 2 specific values? 如果列B在Excel中包含正确的值,则突出显示列A中的单元格 - Highlighting cells in column A if column B contains the correct value in Excel Excel条件格式,如果列A包含任何值高亮显示列B中的相邻单元格 - Excel Conditional Formatting If Column A Contains ANY Value Highlight Adjacent Cell In Column B 如果B列的单元格值包含A单元格值 - If column B cell value contains A cell value Excel宏:如果B列包含12位数字,则C列等于3吗? - Excel Macro: If Column B contains 12 digits then column C equals 3? 如果A列包含x AND B列包含y然后添加值 - If column A contains x AND column B contains y THEN add value 查找A列中的值是否包含B列的值? - Find if value in column A contains value from column B? Excel VBA —查找列A中任何值的第一个出现位置,然后将列B的值插入列C(同一行) - Excel VBA — Find first occurrence of any value in column A, then insert value of column B into column C (same row)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM