Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel VBA“;如果是“那么”;语句来查找列中的特定值,如果为true,则会在另一列中更改值_Excel_Vba - Fatal编程技术网

Excel VBA“;如果是“那么”;语句来查找列中的特定值,如果为true,则会在另一列中更改值

Excel VBA“;如果是“那么”;语句来查找列中的特定值,如果为true,则会在另一列中更改值,excel,vba,Excel,Vba,我正在尝试使用VBA代码在Excel中创建一个按钮。该按钮将运行VBA代码,以查找D列中带有“Y”的所有单元格和E列中带有“Y”的所有单元格。如果此语句为真,则列-J中的所有对应单元格将替换为“Y”。我尝试了以下方法,但没有成功: Sub Simple_If() If Range("D:D").Value = "Y" And Range("E:E").Value = "N" Then Ran

我正在尝试使用VBA代码在Excel中创建一个按钮。该按钮将运行VBA代码,以查找D列中带有“Y”的所有单元格和E列中带有“Y”的所有单元格。如果此语句为真,则列-J中的所有对应单元格将替换为“Y”。我尝试了以下方法,但没有成功:

Sub Simple_If() 
    If Range("D:D").Value = "Y" And Range("E:E").Value = "N" Then
        Range("J:J").Value = "Y"
    End If 
End Sub
我现在尝试了以下代码:

Dim x As Integer 
With Sheets("Sheet2")
LastRow = .Range("D" & .Rows.Count).End(xlUp).Row
For x = 2 To LastRow
    If Cells(x, 4).Value = "Y" And Cells(x, 5).Value = "N" Then
    Cells(x, 10).Value = "Y"
    
Else
    Cells(x, 10).Value = "N"

End If
Next x
End Sub

新代码不会根据调用的条件编辑工作表,但我没有得到错误?这里有什么问题?谢谢您的帮助和指导。

我假设没有空白单元格

将此宏指定给按钮

我希望能帮助你。。。祝你好运

Sub CustomSub()

    'nRows will count rows with data
    'the formula compares if D and E are equal to "Y"
    'if you  dont use headers, replace "J2" with "J1" in code

    Dim nRows As Double
    'First count nRows with data, the last Row
    nRows = ThisWorkbook.Application.WorksheetFunction.CountA(Range("D:D"))
    ' we will write the formula in first cell in column J, it should be J2 if you use headers or J1 if dont
    Range("J2").Select
    'This formula compare both columns vs Y
    ActiveCell.FormulaR1C1 = "=IF(AND(RC[-5]=""Y"",RC[-6]=""Y""),""Y"",""N"")"
    'Finally we will copy this formula in all other cell using the nRows value
    ActiveCell.Copy
    Range("J2:J" & nRows).Select
    ActiveSheet.Paste
    Application.CutCopyMode = False


    'if you need just  go to the first row
    Range("J1").Select
End Sub

多亏了@BigBen,我才能够修复我的代码。工作代码见下文

Sub If_else_using_For()
    Dim x As Long, LastRow As Long
    With Sheets("Sheet name")
        LastRow = .Range("D" & .Rows.Count).End(xlUp).Row

        For x = 2 To LastRow
            If .Cells(x,4).Value = "Y" And .Cells(x,5).Value = "N" Then
                .Cells(x,10).Value = "Y"
            Else
                .Cells(x,10).Value = "N"
            End If
        Next x
    End With
End Sub

通常的做法是循环。谢谢你的评论。我将阅读一些关于“循环”的内容。@BigBen:我尝试了以下循环,但没有成功:“Sub If_else_使用_For()Dim x作为整数Dim nRows作为Double”首先使用数据计算nRows,最后一行nRows=thiswoolk.Application.WorksheetFunction.CountA(范围(“D:D”):x=nRows到nRows,如果单元格(x,4)。Value=“Y”和单元格(x,5)。Value=“N”然后是单元格(x,10)。Value=“Y”Else Cells(x,10)。Value=“N”End如果下一个x End Sub”我希望循环自动计算数据行,因为Excel工作表不是静态的,每天都会添加更多数据。在所有
单元格
调用之前添加一个
,以便它们引用带有工作表的
(“Sheet2”)
.Cells(x,4).Value
单元格(x,5).Value
等等。@BigBen啊哈!修好了。现在它就像一个符咒。向你致敬,大本。谢谢你的帮助。有些单元格有空格。。。对不起,我本该透露的。我假设这会改变建议的代码?我尝试了以下循环,但没有成功:“Sub If_else_使用_For()Dim x作为整数Dim nRows作为Double”首先用数据计算nRows,最后一行nRows=ThisWorkbook.Application.WorksheetFunction.CountA(Range(“D:D”):x=nRows到nRows如果单元格(x,4)。Value=“Y”和单元格(x,5)。Value=“N”然后是单元格(x,10)。Value=“Y”Else单元格(x,10)。Value=“N”End如果下一个x End Sub”我希望循环自动计算数据行数,因为Excel工作表不是静态的,每天会添加更多数据。