如何使用其他列的text参数填充同一列?

时间:2019-05-07 04:07:31

标签: 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 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

2 个答案:

答案 0 :(得分:0)

据我所知,我只是稍微调整了一下代码并使用了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

答案 1 :(得分:0)

在更改某些内容后,对象范围失败,并且类型为“ YES”不匹配,这引发了错误。 ElseIf .Cells(i,“ EP”)= True然后'您可以使用ElseIf语句(问题行)

Sub DecisionTree()

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

结束子