如何使用来自其他列的文本参数填充同一列



我正在创建一个宏文档,该宏文档从几个列中摘取信息,并将其在一个特定列中分配的存储桶中的信息中获取。

我完成了代码工作的第一部分,其中它填充了所选列中不需要恢复,但我认为它没有准确填充结果,我无法获得第二个IF 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

它在更改某些内容和类型不匹配的" yes"之后丢失了一个错误。elseif .cells(i," ep"(= true然后'您可以使用Elseif语句(问题行(

sub decisiptree((

Dim cell As Range
Dim NO_DR As String
Dim YES As Boolean
Dim ws As Worksheet 'Declare and use worksheet/workbook references
YES = True
Set ws = ThisWorkbook.Sheets("Napa Data") '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, "J") = "8-RTO No Recovery,RPO No Recovery" Then 'You don't need to write rowindex and column index, just their values.
            .Cells(i, "FI") = NO_DR 'also, you don't need to specify .Value to change it's value
        ElseIf .Cells(i, "EP") = True Then 'You can use ElseIf statement instead using else, so you can use as many as you need
            .Cells(i, "J") = "Vendor"
        ElseIf .Cells(i, "EF") = "Boulder" Or "Silver" Or "Nap" Or "Irma" Or "Budlign" Or "Sheffield" Then 'another statement I have to add an if statement consisting of like 6 factors
            .Cells(i, "J") = "Enabled/Present" 'Your code
        End If
    Next
End With

结束sub

最新更新