Click here to Skip to main content
15,917,538 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi - Am trying to update some column values based on value of one column value by using this VBA code.

But am getting error that it's not properly ended.

Could you please help me to fix it?

What I have tried:

Sub Test()
    Dim LastRow As Long
    Dim i As Long
    LastRow = Range("AY" & Rows.Count).End(xlUp).Row
    For i = 2 To LastRow
    If Range("AY" & i).Value = "1478" and "1476" Then
            Range("BL" & i).Value = " XYZ "
        End If
           If Range("AY" & i).Value = "1589" And "1595" Then
            Range("BL" & i).Value = "ABC"
        End If
        If Range("AY" & i).Value = "484" and "1447" and "1695" Then
            Range("BL" & i).Value = "XZ"
        End If
        If Range("AY" & i).Value = "1447" Then
             Range("BM" & i).Value = "AZ" and Range("BL" & i).Value = "SPT" 
        End If        
    Next i
End Sub
Posted
Updated 26-Mar-18 9:32am

1 solution

This piece of code will never get true:
If Range("AY" & i).Value = "1478" and "1476" Then

A cell can not have both values in the same time! By the way: why do you think that value is string based?

[EDIT#1]

I'd change multiple If's into Select Case[^]:
VB
Select Case Range("AY" & i).Value
    Case 1476, 1478
            Range("BL" & i).Value = " XYZ "
    Case 1589, 1595
            Range("BL" & i).Value = "ABC"
    Case 484, 1447, 1695
            Range("BL" & i).Value = "XZ"
    Case 1447
             Range("BM" & i).Value = "AZ" 
             Range("BL" & i).Value = "SPT" 
End Select

which is equivalent to:
VB
If Range("AY" & i).Value = 1478 OR Range("AY" & i).Value = 1476 Then
        Range("BL" & i).Value = " XYZ "
End If
'and so on...


Do you see the difference?

[EDIT#2]
Another way is to use Dictionary object[^].

VB
'needs reference to MS Scripting Runtime
    Dim myDictionary As Dictionary
    
    Set myDictionary = New Dictionary
    With myDictionary
        .Add 1476, "XYZ"
        .Add 1478, "XYZ"
        .Add 1589, "ABC"
        .Add 1595, "ABC"
        '...
    End With
    'later...
    For i = 2 To LastRow
        Range("BL" & i).Value = myDictionary(Range("AY" & i).Value)
    Next
 
Share this answer
 
v5
Comments
GJSS 26-Mar-18 15:37pm    
Here am trying to update column BL with value "XYZ" based on the column AY with values 1478 and 1476
Maciej Los 26-Mar-18 15:52pm    
No, you don't. You're checking if cell contains value of...
GJSS 26-Mar-18 16:16pm    
Yes..if the value of AY = "1478" and "1476", then i want to update the value of column BL with "XYZ"
Maciej Los 26-Mar-18 16:23pm    
As i mentioned... a cell can not have the same value in the same time... If there is a "1478", how does "1476" can be?
Think of it!
GJSS 26-Mar-18 16:27pm    
Those are all column values where it has multiple values but here am considering two column values based on these values am trying to update column BY
AY
147839
147839
147697
147697
147697

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900