简体   繁体   中英

Excel VBA Using Select Case to delete cells in a Range

I have a range of data, and I am programming to remove a names from a range. Here is my code.

Option Explicit

Sub SelectCase()

Dim msg As String
Dim VarCase As Range

Set VarCase = Worksheets("Data").Range("D:D")

Select Case VarCase

    Case VarCase = "Zamora"
    VarCase.Delete

    Case VarCase = "John"
    VarCase.Delete

End Select

End Sub

I am getting an error message Run-time error '13' Type mismatch, is this due to trying to use a string ("Zamora/John") with a range?

Here you go! You might want to add it so it checks to see if John and Zamora aren't all lower case if that is needed.

Sub SelectCase()

Dim msg As String
Dim VarCase As Range

For Each VarCase In ActiveSheet.Range("D:D")
  If VarCase.Value2 = "John" Or VarCase.Value2 = "Zamora" Then
  VarCase.ClearContents
  End If
Next VarCase
End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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