简体   繁体   English

基于单元格内容的excel vba宏删除列

[英]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. 您的代码在我的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. 对于该错误,MSDN网站建议您使用For Next代替Do While,以免指定索引元素。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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