如何删除Excel中的特定列
我试图删除Excel中的多个列的Excel。 我从伊利诺伊州统计分析中心下载关于毒品逮捕的数据。 http://www.icjia.org/public/sac/index.cfm?metasection=forms&metapage=rawMetadata&k=170
我想要删除的每一列都是三列相隔。
例如:
亚当斯县伊利诺伊州香槟县伊利诺伊州估计|百分比| 误差的边际百分比|估计保证金| 估计|百分比|错误的百分比
d | E | F | G | H | I |Ĵ
我只想删除所有列的错误百分比
这是我的微:
Sub deleteCol() Columns("H,J").Delete End Sub
我不断收到运行时间'13:types不匹配错误
有什么build议么?
你说你想删除标题为“Error Margin of Error”的任何列,所以让我们尝试使这个dynamic而不是直接命名列。
Sub deleteCol() On Error Resume Next Dim wbCurrent As Workbook Dim wsCurrent As Worksheet Dim nLastCol, i As Integer Set wbCurrent = ActiveWorkbook Set wsCurrent = wbCurrent.ActiveSheet 'This next variable will get the column number of the very last column that has data in it, so we can use it in a loop later nLastCol = wsCurrent.Cells.Find("*", LookIn:=xlValues, SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column 'This loop will go through each column header and delete the column if the header contains "Percent Margin of Error" For i = nLastCol To 1 Step -1 If InStr(1, wsCurrent.Cells(1, i).Value, "Percent Margin of Error", vbTextCompare) > 0 Then wsCurrent.Columns(i).Delete Shift:=xlShiftToLeft End If Next i End Sub
有了这个,只要列标题在第一行,就不用担心数据粘贴/导入的位置。
编辑:如果您的标题不在第一行,这将是一个非常简单的更改。 在这部分代码中: If InStr(1, wsCurrent.Cells(1, i).Value, "Percent Margin of Error", vbTextCompare)
将Cells(1, i)
的“1”在。
编辑2:更改代码的部分来说明完全为空的列。
你只是缺less列声明的后半部分,告诉它删除整个列,因为大多数正常的范围以列字母开头,它正在寻找一个数字,并没有得到一个。 “:”获取整列或行。
我想你在你的范围内寻找的是这样的:
Range("C:C,F:F,I:I,L:L,O:O,R:R").Delete
只需更改列字母以符合您的需求。