如何将相似的代码行合并为一个

时间:2019-03-19 09:11:27

标签: excel

您好,谢谢您的宝贵时间,我的这段代码运行得很好,我只需要了解两件事:

1-有没有一种方法可以将所有这些合并为一行? 类似于如果.Cells(K,“ D”)。Value =“ L”或“ RF”或“ F”或“ Dewat *”然后

2-我如何告诉if语句搜索“ Dewat *”,由于拼写错误,用户可能会输入脱水,脱水,脱水等等。

If .Cells(K, "D").Value = "DT" Then
    J = J + 1
    .Rows(K).EntireRow.Copy Destination:=Worksheets("Trailers").Range("A" & J + 1)
    .Rows(K).EntireRow.Delete
End If

    If .Cells(K, "D").Value = "L" Then
    J = J + 1
    .Rows(K).EntireRow.Copy Destination:=Worksheets("Trailers").Range("A" & J + 1)
    .Rows(K).EntireRow.Delete
End If

    If .Cells(K, "D").Value = "RF" Then
    J = J + 1
    .Rows(K).EntireRow.Copy Destination:=Worksheets("Trailers").Range("A" & J + 1)
    .Rows(K).EntireRow.Delete
End If

    If .Cells(K, "D").Value = "F" Then
    J = J + 1
    .Rows(K).EntireRow.Copy Destination:=Worksheets("Trailers").Range("A" & J + 1)
    .Rows(K).EntireRow.Delete
End If

2 个答案:

答案 0 :(得分:2)

也许这行得通...

If ((.Cells(K, "D").Value = "DT") Or (.Cells(K, "D").Value = "L") Or (.Cells(K, "D").Value = "RF") Or (.Cells(K, "D").Value = "F")) Then
    J = J + 1
    .Rows(K).EntireRow.Copy Destination:=Worksheets("Trailers").Range("A" & J + 1)
    .Rows(K).EntireRow.Delete
End If

答案 1 :(得分:0)

案例1

Option Explicit

Sub test()

    If .Cells(K, "D").Value = "DT" Or .Cells(K, "D").Value = "L" Or .Cells(K, "D").Value = "RF" Or .Cells(K, "D").Value = "F" Then
        J = J + 1
        .Rows(K).EntireRow.Copy Destination:=Worksheets("Trailers").Range("A" & J + 1)
        .Rows(K).EntireRow.Delete
    End If

End Sub

案例2

Option Explicit

Sub test1()

    Dim UserInput As String, SearchInString As String

    With ThisWorkbook.Worksheets("Sheet1")

        UserInput = .Range("A2").Value
        SearchInString = .Range("E1").Value

        If InStr(1, UserInput, SearchInString, vbTextCompare) > 0 Then '<- Check if User Input appears in the search string & if the value is greater than 0 means that appears in the string
            MsgBox "Appears"
        End If

    End With

End Sub