我正在尝试删除包含文字"标题"的列。来自所有工作表。 (可能是标题A,标题B等) 我写了下面的内容,但它没有用......请赐教。
Dim wsh As Worksheet
Dim A As Range
For Each wsh In ActiveWorkbook.Worksheets
Do
Set A = Rows(1).Find(What:="Title", LookIn:=xlValues, lookat:=xlPart)
If A Is Nothing Then Exit Do
A.EntireColumn.Delete
Loop
Next wsh
答案 0 :(得分:2)
因为你正在寻找"标题A" (仅限#34;标题"),您可以通过两种方式使用Find
:
*
通配符添加到搜索到的字符串中,并在第三个参数中添加xlWhole Find(What:="Title*", LookIn:=xlValues, lookat:=xlWhole)
*
通配符,并使用xlPart .Find(What:="Title", LookIn:=xlValues, lookat:=xlPart)
<强>代码强>
Option Explicit
Sub RemoveTitle()
Dim wsh As Worksheet
Dim A As Range
For Each wsh In ActiveWorkbook.Worksheets
Do
Set A = wsh.Rows(1).Find(What:="Title", LookIn:=xlValues, lookat:=xlPart)
If Not A Is Nothing Then
A.EntireColumn.Delete
End If
Loop While Not A Is Nothing
Next wsh
End Sub
答案 1 :(得分:0)
您没有使用工作表限定范围,因此只搜索活动工作表
Sub x()
Dim wsh As Worksheet
Dim A As Range
For Each wsh In ActiveWorkbook.Worksheets
Do
Set A = wsh.Rows(1).Find(What:="Title", LookIn:=xlValues, lookat:=xlPart)
If A Is Nothing Then Exit Do
A.EntireColumn.Delete
Loop
Next wsh
End Sub
答案 2 :(得分:0)
您需要指定使用Find
的工作表
并添加*
(通配符)以包含所有可能性
Dim wsh As Worksheet
Dim A As Range
For Each wsh In ActiveWorkbook.Worksheets
Do
Set A = wsh.Rows(1).Find(What:="Title*", LookIn:=xlValues, lookat:=xlPart)
If A Is Nothing Then Exit Do
A.EntireColumn.Delete
Loop
Next wsh
答案 3 :(得分:0)
您可能想要一次性删除所有列
Option Explicit
Sub main()
Dim wsh As Worksheet
Dim A As Range, foundTitle As Range
Dim firstAddress As String
For Each wsh In ActiveWorkbook.Worksheets
With wsh.Range("A1", wsh.Cells(1, wsh.Cells.Columns.Count).End(xlToLeft))
Set foundTitle = .Offset(1).Resize(1, 1)
Set A = .Find(What:="Title", LookIn:=xlValues, lookat:=xlPart)
If Not A Is Nothing Then
firstAddress = A.Address
Do
Set foundTitle = Union(foundTitle, A)
Set A = .FindNext(A)
Loop While A.Address <> firstAddress
End If
Set foundTitle = Intersect(foundTitle, .Cells)
If Not foundTitle Is Nothing Then foundTitle.EntireColumn.Delete
End With
Next wsh
End Sub