2021年3月24日星期三

Excel VBA ElseIf Loop

I am very very new to VBA, and hoping I can get some help. I have a worksheet that has the below two columns beginning at cells G:5 and H:5. I am needing to update column H based on the values from G but getting stuck. I need to go down the whole column of G and H.

pH Program(New Logic)
Deposit
Deposit
Deposit
Deposit
Pay
Card
Pay

I have the below code, and tried to modify it to my needs but it doesn't populate for every cell in H as I need it to. Can anyone please look at my code and see where I am going wrong. I have my ifelse statements that seem to be correct.

Sub Looper()    Dim i As String  Dim Sel As String  Dim MoveDown As String  Dim pH As String  Dim Program As String    i = 2  MoveDown = "YES"    Do Until MoveDown = "DONE"      Sel = "G5" + Replace(Str(i), " ", "")      pH = Range(Sel).Value           If pH = "Deposit" Then          Program = "Deposit"        ElseIf pH = "Card" Then          Program = "Card"        ElseIf pH = "CL" Then          Program = "CL"        ElseIf pH = "RE" Then          Program = "RE"        ElseIf pH = "Bank Op" Then          Program = "F&C"        ElseIf pH = "MDS" Then          Program = "Credit Op"        ElseIf pH = "MM" Then          Program = "Deposit"        ElseIf pH = "" Then          Program = "Bankwide"    End If        Sel = "H5" + Replace(Str(i), " ", "")      Range(Sel).Value = Program      i = i + 1      Sel = "G5" + Replace(Str(i), " ", "")      If Range(Sel).Value = "" Then          MoveDown = "DONE"      End If        Loop  End Sub  

I first took a stab without the loop and it works but again I need this to go down the whole column:

Sub totn()    Dim ProductSF As String  Dim ProductNew As String    ProductSF = Range("G5").Value    If ProductSF = "Deposit" Then  ProductNew = "Deposit"    ElseIf ProductSF = "Card" Then  ProductNew = "Card"    ElseIf ProductSF = "CL" Then  ProductNew = "CL"    ElseIf ProductSF = "RE" Then  ProductNew = "RE"    ElseIf ProductSF = "Bank Op" Then  ProductNew = "F&C"    ElseIf ProductSF = "MDS" Then  ProductNew = "Credit Op"    ElseIf ProductSF = "MM" Then  ProductNew = "Deposit"    ElseIf ProductSF = " " Then  ProductNew = "Bankwide"    End If    Range("H5").Value = ProductNew    End Sub   
https://stackoverflow.com/questions/66792247/excel-vba-elseif-loop March 25, 2021 at 10:32AM

没有评论:

发表评论