微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

使用 VLOOKUP VBA 删除单元格值

如何解决使用 VLOOKUP VBA 删除单元格值

我正在尝试根据 VLOOKUP 搜索删除一个单元格值,但它向我抛出错误“对象的方法范围 _global 失败 1004” 当我尝试删除用户名时出现该错误

Private Sub Delete_Click()
Dim sht As String
Dim username as String

'Selects the worksheet based on comboBox value.
sht = Me.ComboBox1.Value
'Use a VLOOKUP to Search for the username on a worksheet prevIoUsly selected on
'sht variable. 
username = Application.WorksheetFunction.VLookup(Me.textBox1.Value,Worksheets(sht).Range("A:F"),1,False)
'Delete the username found on the VLOOKUP
Range(username).Clear

解决方法

你的语法有很多错误。请试试这个代码。我认为它实现了你的想法。

Private Sub Delete_Click()

    Dim Sht             As Worksheet
    Dim UserName        As String
    Dim R               As Long
    
    ' Sets the worksheet based on Combobox value.
    Set Sht = Worksheets(Me.Combobox1.Value)
    UserName = Me.TextBox1.Value

    ' Use MATCH to Search for the username on worksheet Sht
    On Error Resume Next
    R = Application.WorksheetFunction.Match( _
                    UserName,Sht.Columns("A"),0)
    If Err Then
        MsgBox """" & UserName & """ wasn't found."
    Else
        'Delete the username found by the MATCH function
        Sht.Cells(R,"A").ClearContents
    End If
End Sub

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。