如何改进我的“if if else if”VBA?码

时间:2017-08-08 08:37:04

标签: vba excel-vba excel

我试图理解if和else if在VBA但我的代码不起作用。你能告诉我我做错了吗?

Sub columnA()
Dim r As Range
Dim src As Worksheet
Dim copyRange As Range
Dim pasteRange As Range
Set src = ThisWorkbook.Sheets("report")

lastRow = src.Range("D" & src.Rows.Count).End(xlUp).Row
Set copyRange = src.Range("D3:D" & lastRow)
Set pasteRange = src.Range("A3:A" & lastRow)

If copyRange = "Updates" Then
pasteRange = "Post-Edit"
ElseIf copyRange = "New Product Translations" Then
pasteRange = "Post-Edit"
ElseIf copyRange = "Misc" Then
pasteRange = "Human"
ElseIf copyRange Is Nothing Then Exit Sub
End If

End Sub

我想要做的是在D列中插入Post-Edit或Human,如果D列中的文字符合条件,那么D列中的文字是更新新的新产品翻译< / strong>那么A列中的单元格应该是编辑后,如果D列中的单元格包含其他,那么我想在列中插入人类

3 个答案:

答案 0 :(得分:3)

您可以从If切换到Select Case,请参阅以下代码:

Select Case copyRange
    Case "Updates", "New Product Translations"
        pasteRange = "Post-Edit"

    Case "Misc"
        pasteRange = "Human"

End Select

编辑1 :已编辑完整代码

Option Explicit

Sub columnA()

Dim R As Range
Dim src As Worksheet
Dim copyRange As Range
Dim pasteRange As Range
Dim LastRow As Long

Set src = ThisWorkbook.Sheets("report")
With src
    LastRow = .Range("D" & .Rows.Count).End(xlUp).Row
    Set copyRange = .Range("D3:D" & LastRow)
    Set pasteRange = .Range("A3:A" & LastRow)
End With

If Not copyRange Is Nothing Then
    For Each R In copyRange
        Select Case R.Value
            Case "Updates", "New Product Translations"
                R.Offset(, -3).Value = "Post-Edit" ' use offset of 3 columns to put the value in column "A"

            Case "Misc"
                R.Offset(, -3).Value = "Human"

        End Select
    Next R
End If

End Sub

答案 1 :(得分:0)

您可以在If中添加布尔条件(或),如下所示:

If copyRange = "Updates" Or copyRange = "New Product Translations" Then
    pasteRange = "Post-Edit"
ElseIf copyRange = "Misc" Then
    pasteRange = "Human"
ElseIf copyRange Is Nothing Then
    Exit Sub
End If

如果单元格区域由一个单元格组成,则上述代码将起作用。对于倍数,您应该考虑业务逻辑,您想要比较什么。但是,如果您决定仅比较第一个,那么这将完成工作:

If CopyRange.Cells(1,1) = "Updates" 

确保相应地更改它。 pasteRange = "Something"没问题。

关于你做错了什么 - copyRange不能没什么,你把它设置在一个范围之上。但是,它的细胞可能是空的。

要检查此项,请尝试以下操作: ElseIf copyRange.Cells(1,1) = vbNullString

因此,您将检查范围中的第一个单元格是否为空。

答案 2 :(得分:0)

您可以使用公式而不是If..ElseSelect Case 如果直接输入公式,则为=IF(OR($D3="Updates",$D3="New Product Translations"),"Post-Edit",IF($D3="Misc","Human",""))

在您的代码中,它将是:

Sub columnA()

    Dim src As Worksheet
    Dim formulaRange As Range
    Dim lastrow As Long

    Set src = ThisWorkbook.Sheets("report")
    lastrow = src.Range("D" & src.Rows.Count).End(xlUp).Row
    Set formulaRange = src.Range("A3:A" & lastrow)

    formulaRange.FormulaR1C1 = _
        "=IF(OR(RC4=""Updates"",RC4=""New Product Translations""),""Post-Edit"",IF(RC4=""Misc"",""Human"",""""))"

    'Replace formula with values.
    formulaRange = formulaRange.Value

End Sub  

如果您在表格中有查询,例如要在H1:H3中找到的值以及I1:I3中的查找值,则可以使用=IFERROR(VLOOKUP(D3,$H$1:$I$3,2,FALSE),"")

VBA中的公式为:

formulaRange.FormulaR1C1 = _
    "=IFERROR(VLOOKUP(RC4,R1C8:R3C9,2,FALSE),"""")"