简体   繁体   English

VBA Excel删除多行

[英]VBA Excel delete multiple rows

This works 这有效

  mySheet.Rows(CStr(activeRow) & ":" & CStr(activeRow + deletedRowAmount)).Delete

This does not work 这行不通

mySheet.Columns(CStr(columDelete) & ":" & CStr(columDelete + deletedRowAmount)).Delete

What I am missing here? 我在这里想念的是什么?

ERROR is VBA Runtime Error 1004 “Application-defined or Object-defined error” 错误是VBA运行时错误1004“应用程序定义的错误或对象定义的错误”

Columns are A,B,C,... when used in a string as you are using them. 当您在使用字符串时,列为A,B,C,...。 If they are numbers you will need to do it slightly different: 如果它们是数字,则您需要做一些稍有不同的操作:

With mySheet    
    .Range(.cells(1,columDelete), .cells(1,columDelete + deletedRowAmount)).EntireColumn.Delete
End With

In general, what Scott Craner says is quite enough, but here is a way to go around it and make it easier: 通常, Scott Craner所说的已经足够了,但是这里有一种解决方法,使它变得更容易:

Sub TestMe()

    Columns(GetColumnRange(2, 5)).Delete

End Sub

Public Function GetColumnRange(colStart As Long, colEnd) As String

    GetColumnRange = Split(Cells(1, colStart).Address(True, False), "$")(0) & _
              ":" & Split(Cells(1, colEnd).Address(True, False), "$")(0)

End Function

Try the TestMe function, it should delete the columns from 2(B) to 5(E) . 尝试使用TestMe函数,它应该将列从2(B)删除到5(E)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM