【发布时间】:2020-12-12 14:29:10
【问题描述】:
我正在尝试从 B 列开始删除第一行中名为“delete”的列。第一列的名称不同,“隐藏”,因为在这里我放置了一些对行执行更改的命令(未包含在下面的代码中),因此开始从 B 中删除列很重要。
我的代码删除列如下:
A(删除必须保留)、B(已删除)、C(未删除)、D(已删除)、E(未删除),尽管我用“删除”命名了从 B 到 E 的所有内容。
Sub deleting()
Dim lcolumn As Long ' column
Dim j As Long
Dim sh As Worksheet, shs As Sheets
Dim wkbTarget As Workbook
For Each sh In ActiveWorkbook.Worksheets ' here's a loop through each sheet
sh.Select
With ActiveSheet
lcolumn = .Cells(1, .Columns.Count).End(xlToLeft).Column
For j = 1 To lcolumn
If .Cells(1, j).Value = "delete" Then 'here is something wrong...
sh.Columns(j).Delete Shift:=xlShiftToLeft
End If
Next j
End With
Next sh
End Sub
【问题讨论】:
-
1. 删除行
sh.Select2. 将With ActiveSheet更改为With sh3. 更改For j = 1 To lcolumn到For j = lcolumn To 1 Step -14. 将If .Cells(1, j).Value = "delete"更改为If Ucase(Trim(.Cells(1, j).Value)) = "DELETE"现在试试吧 -
对不起.. 第 3 点应该是 3。 将
For j = 1 To lcolumn更改为For j = lcolumn To 2 Step -1