简体   繁体   中英

Excel VBA - Loop & IF statement results not static and updating on each new run

I have racked my brain for to long on this simple problem that I cannot figure it out at this point. The situation: I have 2 columns, D and I. Column D is filled down to X# of rows. I need to search for a string on each cell in D:X cell and based on the IF loop assign a value to I:X cell

The problem: With each loop the value that is stored in the cells I-1 through IX is updating with the most current value. So at the end of the third loop the values in I1-I3 are all Unknown. Any help is appreciated. Old Code

 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

New Code, Now the values are changing but its only being assigned to the initial cell I:2. But if I add +1 to the cellvalue like the previous codethen it still overwrites the previous values.

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

just remove the Do...Loop because the For...Next is already doing the job for you. And, besides, it is better to do lastrow2 < Lastrow + 1 instead of lastrow2 <> Lastrow

I'm so dumb.

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

Just noticed that you got your answer but since I have already worked on, this is for your reference.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM