Excel VBA - Loop& IF语句结果不是静态的,并且每次新运行都会更新

时间:2017-10-05 15:31:03

标签: excel vba excel-vba

在这个简单的问题上,我已经绞尽脑汁想到这一点,我现在无法弄明白。 情况: 我有2列,D和I.列D填充到X#行。 我需要在D:X单元格中的每个单元格上搜索一个字符串,并根据IF循环为I:X单元格分配一个值

问题: 对于每个循环,存储在单元格I-1到I-X中的值用最新值更新。所以在第三个循环结束时,I1-I3中的值都是未知的。任何帮助表示赞赏。 旧代码

 Sub Country()
    'Variables
    Lastrow = Worksheets("SFDC").UsedRange.Rows.Count
    lastrow2 = Worksheets("SFDC").UsedRange.Rows.Count
    'check the rows for values
    If lastrow2 > 1 Then
        lastrow2 = 0
        Else
    End If
    'Code will run until the last value it reached
    Do While lastrow2 <> Lastrow
        Set Check = Range("D2:D" & Lastrow)
        For Each Cell In Check
            If InStr(Cell, "ANZI-") Then
                Range("I2:I" & cellvalue).Value = "ANZI"
                lastrow2 = lastrow2 + 1
            ElseIf InStr(Cell, "US-") Then
                Range("I2:I" & cellvalue).Value = "US"
                lastrow2 = lastrow2 + 1
            Else
                Range("I2:I" & cellvalue).Value = "Unknown"
                lastrow2 = lastrow2 + 1
            End If
        Next
    Loop
End Sub

新代码,现在值正在改变,但它只被分配给初始单元格I:2。但是,如果我像上一个代码一样向cellvalue添加+1,那么它仍会覆盖以前的值。

Sub Country()
'Variables
lastrow = Worksheets("SFDC").UsedRange.Rows.Count

'Code will run until the last value it reached
Set Check = Range("D2:D" & lastrow)
cellvalue = 2
For Each Cell In Check
    If InStr(Cell, "ANZI-") Then
        Range("I2:I" & cellvalue).Value = "ANZI"
    ElseIf InStr(Cell, "US-") Then
        Range("I2:I" & cellvalue).Value = "US"
    Else
        Range("I2:I" & cellvalue).Value = "Unknown"
    End If
    cellvalue = cellvalue + 1
Next

End Sub

3 个答案:

答案 0 :(得分:0)

只需删除Do...Loop,因为For...Next已经在为您完成工作。而且,最好是lastrow2 < Lastrow + 1而不是lastrow2 <> Lastrow

答案 1 :(得分:0)

我真傻。

Sub Country()
'Get # of rows on worksheet
lastrow = Worksheets("SFDC").UsedRange.Rows.Count
'Setting variable for the For Loop
Set Check = Range("D2:D" & lastrow)
'will be used as a counter for the cell the return will be placed
cellvalue = 2

'Code will run until through each cell until the last value it reached
For Each Cell In Check
    'if the string is in the D cell then the value will be written to the I cell
    If InStr(Cell, "ANZI-") Then
        Cells(cellvalue, "I") = "ANZI"
    ElseIf InStr(Cell, "US-") Then
        Cells(cellvalue, "I") = "US"
    Else
        Cells(cellvalue, "I") = "Unknown"
    End If
    cellvalue = cellvalue + 1
Next

End Sub

答案 2 :(得分:0)

刚刚注意到你得到了答案,但由于我已经做过,这是供你参考的。

Option Explicit

Sub Country()

Dim lastRow As Long
Dim Check As Range
Dim rowNum As Long
Dim cell

'Get # of rows on worksheet
lastRow = Worksheets("SFDC").UsedRange.Rows.Count
'Setting variable for the For Loop
Set Check = Range("D2:D" & lastRow)
'will be used as a counter for the cell the return will be placed

'Code will run until through each cell until the last value it reached
For Each cell In Check
    rowNum = cell.Row()
    'if the string is in the D cell then the value will be written to the I cell
    If InStr(cell, "ANZI") Then
        Range("I" & rowNum) = "ANZI"
    ElseIf InStr(cell, "US") Then
        Range("I" & rowNum) = "US"
    Else
        Range("I" & rowNum) = "Unknown"
    End If
Next

End Sub