elself looping to next row

Jenn_1214

New Member
Joined
Jan 8, 2019
Messages
2
This code works perfectly in the the first row only. I tried to add loop but keep getting errors. I need this move down a row and continue moving down. This is my first time doing this and I have been researching for hours. I have been using a function but i have to many conditions to list.
Sub test()
Dim Vendor As String, account As String
Vendor = Range("A2")
If Vendor = "ABC" Then
account = "Statement A"
ElseIf Vendor = "DEF" Then
account = "Statement B"
ElseIf Vendor = "GHI" Then
account = "Statement C"
Else
account = "statement c"
End If
Range("F2") = account
End Sub
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
Welcome to the Board!

Here is code that will do what you want. Just enter in the range that you want. We can make it dynamic too, if you do not know the end row.
I also used a CASE statement, instead of IF/ELSEIF/ELSE statements. They are much easier to read, IMO (see: https://www.techonthenet.com/excel/formulas/case.php)
Code:
Sub test()

    Dim VendorRange As Range
    Dim cell As Range
    Dim Vendor As String, account As String

'   Set the range you want to look through
    Set VendorRange = Range("A2:A10")
    
'   Loop through range
    For Each cell In VendorRange
        Vendor = cell.Value
        Select Case Vendor
            Case "ABC"
                account = "Statement A"
            Case "DEF"
                account = "Statement B"
            Case "GHI"
                account = "Statement C"
            Case Else
                account = "statement c"
        End Select
'       Update column F
        Range("F" & cell.Row) = account
    Next cell

End Sub
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,176
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top