excel - 尝试插入或删除列
问题描述
我正进入(状态:
Range 类的运行时错误“1004”小计方法失败
我录制了一个宏(并手动添加了一些代码)来复制数据、排序、小计以及格式化一些小计行。将行添加到原始数据表(“StaffingProjected”)时,宏工作正常,但我无法插入或删除列。当我这样做时,我得到了标题中描述的错误。
Sub Project()
'
' Project Macro
'
'
Application.ScreenUpdating = False
Sheets("Project").Cells.Clear
Sheets("Project").Cells.RemoveSubtotal
Sheets("Project").Cells.ClearFormats
Sheets("Project").Cells.RowHeight = 13.5
Sheets("StaffingProjected").Activate
ActiveSheet.Cells.Select
Selection.Copy
Sheets("Project").Select
Sheets("Project").Range("A1").Select
ActiveSheet.Paste
Cells.Select
Application.CutCopyMode = False
ActiveWorkbook.Worksheets("Project").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("Project").Sort.SortFields.Add Key:=Range("B2:B58") _
, SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("Project").Sort
.SetRange Range("A1:HC58")
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
Range("A1").Select
Selection.Subtotal GroupBy:=2, Function:=xlSum, TotalList:=Array(6, 7, 8, 9, _
10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, _
36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, _
62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, _
88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110 _
, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, _
130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149 _
, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168, _
169, 170, 171, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188 _
, 189, 190, 191, 192, 193), Replace:=True, PageBreaks:=False, SummaryBelowData _
:=True
ActiveSheet.Outline.ShowLevels RowLevels:=2
Cells.Select
Selection.SpecialCells(xlCellTypeVisible).Select
Selection.Font.Bold = True
With Selection.Interior
.Pattern = xlNone
.TintAndShade = 0
.PatternTintAndShade = 0
End With
Selection.RowHeight = 20
ActiveSheet.Outline.ShowLevels RowLevels:=3
Range("A1").Select
Application.ScreenUpdating = True
End Sub
调试说问题在“Selection.Subtotal”块内。确切的问题是什么,我可以更改什么以便我可以在我的原始数据表(“StaffingProjected”)中删除/插入列?
解决方案
在我看来,当您需要一些代码时,录制的 marco 很有用。你应该尽可能避免它。您的代码将更易于阅读和改进。
您可以尝试将其添加到模块中的某个位置:
Sub deleteColumnNo(No As Integer)
Columns(No).EntireColumn.Delete
End Sub
Sub AddColumn(Letter As String)
Columns(Letter + ":" + Letter ).Select
Selection.Insert Shift:=xlToRight
End Sub
然后,介绍您需要删除/添加列的那些行:
Sub Project()
'[...]
CleanColumnNo 1 'delete the first column
CleanColumnNo 2 'delete the second column
AddColumn "B" 'add a column between A and B
'[...]
end sub
无论如何,下次不要犹豫添加其他信息,您将有更好的答案。
祝你今天过得愉快!
推荐阅读
- html - spaCy 是否处理下标和上标?
- asp.net - 注销/超时重定向 ASP.net Core
- ios - SceneKit 模型木制材质在 App 中看起来像青铜
- javascript - 使用 tempauth 下载 URL 在 Microsoft 图形中引发 403 错误
- php - 本地主机网站重定向到开发中的实时站点
- swift - 在 ui 集合视图控制器中使用后关闭键盘
- java - 从 android 客户端到 servlet 的连接
- azure - Azure 应用服务应用程序设置
- c# - 在 Visual Studio 2015 扩展的项目中更改文件 ProjectItem 的内容?
- angular - 在 Varnish 代理后面的 Angular 5.6.0 中设置文档根目录