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