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

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
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,214,976
Messages
6,122,541
Members
449,089
Latest member
davidcom

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