简体   繁体   English

Excel VBA ElseIf 循环

[英]Excel VBA ElseIf Loop

I am very very new to VBA, and hoping I can get some help.我对 VBA 非常陌生,希望能得到一些帮助。 I have a worksheet that has the below two columns beginning at cells G:5 and H:5.我有一个工作表,其中包含以下两列,从单元格 G:5 和 H:5 开始。 I am needing to update column H based on the values from G but getting stuck.我需要根据 G 中的值更新 H 列但卡住了。 I need to go down the whole column of G and H.我需要 go 向下 G 和 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.我有下面的代码,并尝试根据我的需要对其进行修改,但它并没有像我需要的那样填充 H 中的每个单元格。 Can anyone please look at my code and see where I am going wrong.任何人都可以看看我的代码,看看我哪里出错了。 I have my ifelse statements that seem to be correct.我的 ifelse 陈述似乎是正确的。

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:我首先在没有循环的情况下进行了刺伤,它可以工作,但我再次需要在整个专栏中使用 go:

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 

Would this formula serve your purpose?这个公式能达到你的目的吗? Enter it in G5 and copy down as needed.在 G5 中输入并根据需要复制下来。

=IFERROR(INDEX({"F&C","Credit Op","Deposit","Bankwide"},MATCH(G5,{"Bank Op","MDS","MM"," "},0)),G5)

Here's how it works, so that you can modify it to still better suit your needs.以下是它的工作原理,以便您可以对其进行修改以更好地满足您的需求。

  1. The MATCH function looks for the value from G5 in this array: {"Bank Op","MDS","MM"," "} and returns the element number where it was found. MATCH function 在此数组中查找来自 G5 的值: {"Bank Op","MDS","MM"," "}并返回找到它的元素编号。 For example, the space will be found in 4th position and the MATCH function returns 4.例如,空格将在第 4 个 position 中找到,而 MATCH function 返回 4。
  2. INDEX({"F&C","Credit Op","Deposit","Bankwide"} holds the replacements. In 4th position is "Bankwide" and that will be returned if G5 holds a space. INDEX({"F&C","Credit Op","Deposit","Bankwide"}持有替代品。在第 4 个 position 是“Bankwide”,如果 G5 持有空间,则将返回。
  3. If G5 holds a value that isn't in the MATCH array an error will result.如果 G5 保存的值不在 MATCH 数组中,则会导致错误。 And if an error results the formula returns the original value in G5.如果出现错误,公式将返回 G5 中的原始值。

Unfortunately, your two codes don't cover all possibilities.不幸的是,您的两个代码并未涵盖所有可能性。 For example "Pay" has no counterpart.例如“支付”没有对应物。 Therefore my formula will leave it as it is.因此,我的公式将保持原样。 If you want another result, add it to the MATCH array in 5th position {"Bank Op","MDS","MM"," ","Pay"} and add the counterpart to the INDEX array, also in 5th position, {"F&C","Credit Op","Deposit","Bankwide","Card"} .如果您想要另一个结果,请将其添加到第 5 个 position {"Bank Op","MDS","MM"," ","Pay"}的 MATCH 数组中,并将对应的数组添加到 INDEX 数组中,也在第 5 个 position 中, {"F&C","Credit Op","Deposit","Bankwide","Card"} .

Now, if you prefer to do all that with VBA you would employ the same logic.现在,如果您更喜欢使用 VBA 来完成所有这些操作,您将使用相同的逻辑。 Instead of a series of IFs you would create two arrays, say you name them Original and Replacement .您将创建两个 arrays 而不是一系列 IF,假设您将它们命名为OriginalReplacement You would look for the item in G5 in the Original array and then pick the same element from Replacement .您将在Original数组中查找 G5 中的项目,然后从Replacement中选择相同的元素。 You would do this in a loop, like,您可以循环执行此操作,例如,

For R = 5 To Cells(Rows.Count, "G").End(xlUp).Row
    Cells(R, "H").Value = 0 ' do the conversion here
Next R

It is considered to be a good practice not to mix programming logic and data.不混合编程逻辑和数据被认为是一种很好的做法。

If you write data into your code like below (data is which value matches which other value) that means everytime anything has to be changed in these values you have to touch the code again.如果您将数据写入您的代码,如下所示(数据是哪个值与哪个其他值匹配),这意味着每次必须更改这些值中的任何内容时,您都必须再次触摸代码。

If pH = "Deposit" Then
    Program = "Deposit"

ElseIf pH = "Card" Then
    Program = "Card"

ElseIf pH = "CL" Then
    Program = "CL"

' …

This is why this is a bad practice.这就是为什么这是一个不好的做法。 Good practice is to separate code from any data, so the code logic is generic and can easily be re-used.好的做法是将代码与任何数据分开,因此代码逻辑是通用的并且可以很容易地重用。 And if data changes no programmer is needed.如果数据发生变化,则不需要程序员。 Also this keeps your code slim and better maintainable because it does not get messed up with a huge bunch of data.这也使您的代码保持苗条和更好的可维护性,因为它不会被大量数据弄乱。

The solution would be to write your data into a worksheet (this can be a hidden worksheet so nobody sees it and gets confused).解决方案是将您的数据写入工作表(这可能是一个隐藏的工作表,因此没有人看到它并感到困惑)。

So if you put this into a worksheet called MatchTable因此,如果您将其放入名为MatchTable的工作表中

Lookup抬头 Return返回
Deposit订金 Deposit订金
Card卡片 Card卡片
CL CL CL CL
RE回覆 RE回覆
Bank Op银行业务 F&C F&C
MDS MDS Credit Op信用操作
MM毫米 Deposit订金
=" " =" " Bankwide全行

you can easily use a lookup formula like below in cell H5您可以在单元格 H5 中轻松使用如下查找公式

=INDEX(MatchTable!B:B,MATCH(G5,MatchTable!A:A,0))

to pull your matching data.提取匹配的数据。 This formula can then be copied down to the rest of your cells.然后可以将此公式复制到您的单元格的 rest 中。

Now any time you change something on your MatchTable this gets easily applied to your sheet.现在,每当您更改MatchTable上的某些内容时,这都可以轻松应用于您的工作表。 There is no need to touch any code or to touch any formula ever again.无需再触碰任何代码或触碰任何公式。

Note that the formula will show an #N/A error if no match can be found like for Pay in your example.Pay ,如果在您的示例中找不到匹配项,则公式将显示#N/A错误。 Then you can either use something like below to set a default value:然后你可以使用类似下面的东西来设置一个默认值:

=IFERROR(INDEX(MatchTable!B:B,MATCH(G5,MatchTable!A:A,0)),"(default, no match)")

Or something like或者类似的东西

=IFERROR(INDEX(MatchTable!B:B,MATCH(G5,MatchTable!A:A,0)),G5)

to use the original value Pay if it doesn't match anything.如果不匹配任何内容,则使用原始值Pay

If it has to be done with VBA you can use the same combination of the WorksheetFunction.Index method and the WorksheetFunction.Match method as in the formula above within a loop using the same MatchTable .如果必须使用 VBA 完成,您可以使用相同的WorksheetFunction.Index 方法WorksheetFunction.Match 方法的组合,如上面公式中使用相同的MatchTable

Alternatively you can write the formula with VBA或者,您可以使用 VBA 编写公式

ThisWorkbook.Worksheet("Sheet1").Range("H5:H100").Formula = "=IFERROR(INDEX(MatchTable!B:B,MATCH(G:G,MatchTable!A:A,0)),G:G)"

and if needed convert the formula into values then:如果需要,将公式转换为值,然后:

With ThisWorkbook.Worksheet("Sheet1").Range("H5:H100")
    .Value = .Value
End With

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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