写了一个有意义的VBA脚本,但仍然不起作用

嗨,所以我试图运行下面的脚本,但它所做的是把一个列中的“N / A”,只是循环通过下面的所有其他单元格,而不做任何事情。

Sub NA() Range("E1").Select Do Until IsEmpty(ActiveCell) If Range("E1") = "password_update" Then If Range("G1") = Range("J1") Then Range("B1") = "N/A" ActiveCell.Offset(1, 0).Select Loop End Sub 

我要做的是检查单元格"E1"是否有值“password_update”,如果是,那么也检查单元格"G1"和单元格"J1"是否有匹配的内容,如果所有这些标准匹配键入“N / A”到单元格"B1"

我也想让这个脚本循环遍历所有后续的行( E1然后E2然后E3等等)。 现在我不是VBA专家,但是我写了这个脚本,不pipe我看起来有多less次对我来说都合情合理,但是仍然不起作用。 有任何想法吗? 帮助将不胜感激。

尝试这个。 尽可能避免在代码中使用select语句。

 Sub NA() lastrow = Cells(Rows.Count, "E").End(xlUp).Row For X = 1 To lastrow If Range("E" & X).Value = "password_update" And Range("G" & X) = Range("J" & X) Then Range("B" & X) = "N/A" Next X End Sub 

类似于上面的代码,但是如果你想要更清楚地发现正在发生的事情和你正在使用的工作表(如果你在错误的工作表中运行,例如考虑在下面放置类似的东西)。 这与您使用的逻辑相同

 Sub UpdateColBwithNA() 'Place in a standard module Dim wb As Workbook Dim ws As Worksheet Dim LastRow As Long Dim currentRow As Long Set wb = ThisWorkbook Set ws = wb.Sheets("Sheet1") 'Change to name of sheet you are running code against With ws LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row .Range("E1").Activate For currentRow = 1 To LastRow If .Cells(currentRow, "E") = "password_update" And _ (.Cells(currentRow, "G") = .Cells(currentRow, "J")) Then .Cells(currentRow, "B") = "N/A" End If Next currentRow End With End Sub 

With语句意味着您不必去表单。