根据两行中的值删除列

时间:2017-04-28 11:24:39

标签: excel vba

以下代码应删除两行中的值与条件匹配的列。

没有错误,也没有错误。

<h3>{{slides[slidePosition].title}}</h3>

1 个答案:

答案 0 :(得分:1)

我认为问题出在您的IF上。您可能希望以这种方式尝试:

If Not (findteamname Is Nothing) And Not (findresourcename Is Nothing) Then
    findresourcename.EntireColumn.Delete
End If

如果还有其他问题,请在此处添加一些调试的完整版本。 Debug.Print的输出显示在&#34;立即&#34;窗口,所以确保在VBE中可见(视图&gt;立即窗口,或 CTRL + G

Private Sub deleteresourcebutton_Click()
    'A line of code for debugging purposes
    Debug.Print "deleteresourcebutton_Click is running"

    Dim teamname As String
    Dim resourcename As String
    Dim findteamname As Range
    Dim findresourcename As Range

    teamname = teamnametextbox.Text
    resourcename = resourcenametextbox.Text

    'Two lines of code for debugging purposes
    Debug.Print "Searching for Team: " & teamname
    Debug.Print "Searching for Resource: " & resourcename

    Set findteamname = Sheets("Sheet1").Range("7:7").Find(what:=teamname, MatchCase:=False, lookat:=xlWhole)
    Set findresourcename = Sheets("Sheet1").Range("8:8").Find(what:=resourcename, MatchCase:=False, lookat:=xlWhole)

    'Five lines of code for debugging purposes
    If Not (findteamname Is Nothing) Then
    Debug.Print "Team found: " & findteamname.Address
    Else
    Debug.Print "Team not found"
    End If

    If Not (findteamname Is Nothing) And Not (findresourcename Is Nothing) Then
        findresourcename.EntireColumn.Delete
    End If

    'A line of code for debugging purposes
    Debug.Print "deleteresourcebutton_Click has finished running"

End Sub