【发布时间】:2017-09-25 12:08:52
【问题描述】:
我有以下宏,我正在尝试实现以下目标:
- 删除前 3 行
- 自动调整每一列
- 重命名一些列并删除其余列。
它似乎只运行删除前 3 行的第一个例程,没有别的。我对 Macro 很陌生,所以我似乎错过了运行所有 Subs 的重要内容?
选项显式
Sub sbDeleteARowMulti()
Rows("1:3").Delete
End Sub
Sub sbChangeColumnWidthMulti()
Columns("A:Z").AutoFit
End Sub
Sub RenDelCols()
Dim vCols As Variant
Dim vNames As Variant
Dim iCols As Integer
Dim iCol As Integer
Dim wks As Worksheet
Dim i As Integer
'define the worksheet
Set wks = Worksheets("timesheets2")
'Cols in Receiving to be renamed
vCols = Array(2, 9, 14, 15, 16, 19) 'Cols A,L,N,Q,X,Y
'Names from Order to rename them to
vNames = Array("Project", "Supervisor", "Employee", "Status", "Date", "Time")
With wks
iCols = .Cells(1, .Columns.Count).End(xlToLeft).Column
For iCol = iCols To 1 Step -1
i = 0
'check if col number is one to change
On Error Resume Next
i = Application.WorksheetFunction.Match(iCol, vCols, 0)
On Error GoTo 0
If i = 0 Then
'column is not in list, delete it
.Columns(iCol).EntireColumn.Delete
Else
'col is in list, rename it
.Cells(1, iCol).Value = vNames(i - 1)
End If
Next
End With
End Sub
【问题讨论】:
-
另外两个 subs 永远不会被调用,所以它们当然不会被执行。
-
你是如何运行你的代码的?.. 把所有的 sub 合二为一,然后试试。