简体   繁体   中英

clearcontents range of cells

I'm new to VBA and trying to run a very simple subroutine, but am getting an error. I want to clear a range of cells from K7 to an indefinite number of rows and column V. Here is the code:

Sub UpdateMatrix()

    Worksheets("Prioritization Matrix").Range("K7", Cells(Row.Count, 22)).ClearContents

End Sub

Here is the error:

vba runtime error 424 object required

All the examples I've seen lead me to believe that this code should work, but it doesn't. Suggestions?

Thanks!

  • First, if you're running your code from another sheet than the Prioritization Matrix, you're creating a 3d range, which fails, you need to make your reference to Cells absolute, not relative.
  • Second, the Count applies to Rows , not Row .

     Worksheets("Prioritization Matrix").Range("K7", Worksheets("Prioritization Matrix").Cells(Worksheets("Prioritization Matrix").Rows.Count, 22)).ClearContents 
  • Finally, you're clearing a very wide range, you can restrict the clearing by using Used Range :

     Sub UpdateMatrix() Dim sht As Worksheet Set sht = Worksheets("Prioritization Matrix") Intersect(sht.Range("K7", sht.Cells(Rows.Count, 22)), sht.UsedRange).ClearContents End Sub 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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