Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/25.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 如何使用其他列中的文本参数填充同一列?_Excel_Vba - Fatal编程技术网

Excel 如何使用其他列中的文本参数填充同一列?

Excel 如何使用其他列中的文本参数填充同一列?,excel,vba,Excel,Vba,我正在创建一个宏文档,它从多个列中提取信息,并将这些信息放在一个特定列中指定的存储桶中 我让代码的第一部分工作,它在所选列中填充了不需要恢复的内容,但我认为它没有准确地填充结果,并且我无法运行第二个if语句 Sub DecisionTree() Dim cell As Range Dim Member_state As String Dim NO_DR As String NO_DR = "No Recovery Required" Dim i As Integer For i = 1

我正在创建一个宏文档,它从多个列中提取信息,并将这些信息放在一个特定列中指定的存储桶中

我让代码的第一部分工作,它在所选列中填充了不需要恢复的内容,但我认为它没有准确地填充结果,并且我无法运行第二个if语句

Sub DecisionTree()


Dim cell As Range
Dim Member_state As String
Dim NO_DR As String

NO_DR = "No Recovery Required"


Dim i As Integer
For i = 1 To 14000 'ActiveSheet.Rows.Count
    If ActiveSheet.Cells(RowIndex:=i, ColumnIndex:="D").Value = "Arkansas" Then
    ActiveSheet.Cells(RowIndex:=i, ColumnIndex:="K").Value = NO_DR
    Else
        If ActiveSheet.Cells(RowIndex:=i, ColumnIndex:="E").Value = 1 Then
            ActiveSheet.Cells(RowIndex:=i, ColumnIndex:="K").Value = "One"
        End If

    End If
Next

End Sub


I would like answers for why my if statements are not properly calculating and how I can add other if statements to populate the same column

据我所知,我只是对你的代码做了一点修改,并使用了
ElseIf
,但你的代码应该可以工作。这就是你如何升级你的标准:

Option Explicit
Sub DecisionTree()

    Dim cell As Range
    Dim Member_state As String
    Dim NO_DR As String
    Dim ws As Worksheet 'Declare and use worksheet/workbook references

    Set ws = ThisWorkbook.Sheets("SheetName") 'change this to the name of the worksheet you are working on

    NO_DR = "No Recovery Required"

    Dim i As Long, LastRow As Long 'always long, in case your data exceeds 32k rows and anyways working with 32bits will force it to long.
    With ws 'this will allow you to use the reference without writting it
        LastRow = .Cells(.Rows.Count, 1).End(xlUp).Row 'this calculate the last row with data, in this case on column A (1)
        For i = 1 To LastRow
            If .Cells(i, "D") = "Arkansas" Then 'You don't need to write rowindex and column index, just their values.
                .Cells(i, "K") = NO_DR 'also, you don't need to specify .Value to change it's value
            ElseIf .Cells(i, "E") = 1 Then 'You can use ElseIf statement instead using else, so you can use as many as you need
                .Cells(i, "K") = "One"
            ElseIf .Cells(i, "J") = "Cat" Then 'another statement
                .Cells(i, "K") = "Cat"'Your code
            End If
        Next
    End With

End Sub

据我所知,我只是对你的代码做了一点修改,并使用了
ElseIf
,但你的代码应该可以工作。这就是你如何升级你的标准:

Option Explicit
Sub DecisionTree()

    Dim cell As Range
    Dim Member_state As String
    Dim NO_DR As String
    Dim ws As Worksheet 'Declare and use worksheet/workbook references

    Set ws = ThisWorkbook.Sheets("SheetName") 'change this to the name of the worksheet you are working on

    NO_DR = "No Recovery Required"

    Dim i As Long, LastRow As Long 'always long, in case your data exceeds 32k rows and anyways working with 32bits will force it to long.
    With ws 'this will allow you to use the reference without writting it
        LastRow = .Cells(.Rows.Count, 1).End(xlUp).Row 'this calculate the last row with data, in this case on column A (1)
        For i = 1 To LastRow
            If .Cells(i, "D") = "Arkansas" Then 'You don't need to write rowindex and column index, just their values.
                .Cells(i, "K") = NO_DR 'also, you don't need to specify .Value to change it's value
            ElseIf .Cells(i, "E") = 1 Then 'You can use ElseIf statement instead using else, so you can use as many as you need
                .Cells(i, "K") = "One"
            ElseIf .Cells(i, "J") = "Cat" Then 'another statement
                .Cells(i, "K") = "Cat"'Your code
            End If
        Next
    End With

End Sub

它抛出一个错误,对象范围在更改某些内容后失败,并且类型不匹配为“是”。ElseIf.Cells(i,“EP”)=True,则可以使用ElseIf语句(问题行)

子决策树()


End Sub

它抛出一个错误,即在更改某些内容后对象范围失败,并且类型不匹配为“是”。ElseIf.Cells(i,“EP”)=True,则可以使用ElseIf语句(问题行)

子决策树()



End Sub

很高兴它帮助了@RyanColeman如果你需要进一步的帮助,尽管问。如果这对你有用,考虑把这个标记为一个答案,其他人会发现它。我写的只是一个例子…你可以用它来满足你的所有需要。你能编辑你的答案并给我看你的新代码@RyanColeman吗?我把它作为一个答案添加到这篇文章中。我为这些来来回回的事情道歉。很高兴它对@RyanColeman有所帮助。如果你需要进一步的帮助,尽管问吧。如果这对你有用,考虑把这个标记为一个答案,其他人会发现它。我写的只是一个例子…你可以用它来满足你的所有需要。你能编辑你的答案并给我看你的新代码@RyanColeman吗?我把它作为一个答案添加到这篇文章中。我为所有的来回表示歉意。这里有很多错误,您没有使用变量
YES
您正在询问exel
.Cells(I,“EP”)
是否为
True
。True对于布尔型,您需要检查单元格是否具有值“True”,因此需要使用双引号
.Cells(i,“EP”)=“True”
。此外,要检查单个语句中的多个单词,您需要遵循以下逻辑:
ElseIf.Cells(i,“EF”)=“Boulder”或.Cells(i,“EF”)=“Silver”…
有更好的方法,但您需要更改它才能使其正常工作。我最初在没有布尔变量的情况下尝试了它,但仍然收到一个错误。最初我有ElseIf.Cells(I,“EP”)=“Yes”,但仍然收到一个类型不匹配。如果单元格有错误,这将抛出一个错误,请检查I值以查看在哪一行,以及该单元格是否有一些#N/a或#DIV0Ah OK。是的,这个专栏确实没有礼物。它出现在if语句的其余两列中。但是这会导致类型不匹配错误吗?有很多错误,您没有使用变量
YES
您正在询问exel
.Cells(i,“EP”)
是否为
True
。True对于布尔型,您需要检查单元格是否具有值“True”,因此需要使用双引号
.Cells(i,“EP”)=“True”
。此外,要检查单个语句中的多个单词,您需要遵循以下逻辑:
ElseIf.Cells(i,“EF”)=“Boulder”或.Cells(i,“EF”)=“Silver”…
有更好的方法,但您需要更改它才能使其正常工作。我最初在没有布尔变量的情况下尝试了它,但仍然收到一个错误。最初我有ElseIf.Cells(I,“EP”)=“Yes”,但仍然收到一个类型不匹配。如果单元格有错误,这将抛出一个错误,请检查I值以查看在哪一行,以及该单元格是否有一些#N/a或#DIV0Ah OK。是的,这个专栏确实没有礼物。它出现在if语句的其余两列中。但这会导致类型不匹配错误吗?