简体   繁体   中英

excel vba macro delete column based on cell content

I'm trying to write a macro to delete columns from a spreadsheet if they have certain content. All the data is in the first row, of variable length. I think the problem may have to do with my range selection. I keep getting subscript out of range when I try to do the search. Any advice would be appreciated. :)

Sub Disk_Firmware()
   Dim c As Range
   Dim SrchRng As Range
   Dim SrchStr As String
   Set SrchRng = ActiveSheet.Range("A1").EntireRow
   SrchStr = InputBox("Please enter a search string")
   Do
      Set c = SrchRng.Find(SrchStr, LookIn:=xlvalues)
      If Not c Is Nothing Then c.EntireColumn.Delete
   Loop While Not c Is Nothing
End Sub

Your code works absolutely fine in my VBE. I just added the following:

SrchStr = Trim(SrchStr)

For that error, the MSDN website suggests that you use a For Next instead of a Do While, so as not to specify index elements.

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