Share via

VBA code to clear contents

Anonymous
2023-11-20T15:41:11+00:00

I have the following code in a procedure:

Sheets("Lists").Range("B2:C2,B3:C3,B6:C6").ClearContents

It does not work and the only way to use the clearcontents command, so far as I've found,

is to activate the sheet first. Is there code to do a clearcontents w/o having to activate the sheet?

Microsoft 365 and Office | Excel | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

6 answers

Sort by: Most helpful
  1. triptotokyo-5840 36,686 Reputation points Volunteer Moderator
    2023-11-20T18:17:12+00:00

    This code:-

    Worksheets("Lists").Range("B2:C2,B3:C3,B6:C6").Clear

     - will only clear the named ranges of the Worksheet called Lists and nothing else.

    This code:-

    ActiveSheet.Range("B2:C2,B3:C3,B6:C6").ClearContents

     - will clear the named ranges of the Active Sheet (i.e. the only one that’s currently selected by the user) so it can apply to multiple Worksheets.

    For this part:-

    Activating the sheet always works:

    Sub ClearSummaryDataAll()

    ActiveSheet.Range("B2:C2,B3:C3,B6:C6").ClearContents

    End Sub

     - you’re not activating the Worksheet but referring to the currently selected Worksheet (can you see the difference?) and this is why this code’ll apply to the named ranges of the Active Sheet.

    Was this answer helpful?

    0 comments No comments
  2. Anonymous
    2023-11-20T17:37:25+00:00

    No results. No error message. (Thanks for getting back to me so quickly!)

    Was this answer helpful?

    0 comments No comments
  3. Anonymous
    2023-11-20T17:34:48+00:00

    Thanks for your quick reply!

    I tried your code suggestion (below) but I get nothing. No results, not even an error message.

    Sub FromMsftComm()

    Worksheets("Lists").Range("B2:C2,B3:C3,B6:C6").Clear

    End Sub

    Activating the sheet always works:

    Sub ClearSummaryDataAll()

    ActiveSheet.Range("B2:C2,B3:C3,B6:C6").ClearContents

    End Sub

    I guess can I activate the Lists sheet and then go back to my original position.

    That wouldn't be the worse thing I could do. My objective is to keep things simple & efficient.

    Was this answer helpful?

    0 comments No comments
  4. triptotokyo-5840 36,686 Reputation points Volunteer Moderator
    2023-11-20T16:00:45+00:00

    I have the following code in a procedure:

    Sheets("Lists").Range("B2:C2,B3:C3,B6:C6").ClearContents

    It does not work and the only way to use the clearcontents command, so far as I've found,

    is to activate the sheet first. Is there code to do a clearcontents w/o having to activate the sheet?

    This works for me with no errors reported:-

    Worksheets("Lists").Range("B2:C2,B3:C3,B6:C6").Clear

    OR - depending upon your exact requirements:-

    Contents and styles:-

    Worksheets("Lists").Range("B2:C2,B3:C3,B6:C6").Clear

    Content only:-

    Worksheets("Lists").Range("B2:C2,B3:C3,B6:C6").ClearContents

    Was this answer helpful?

    0 comments No comments
  5. Rory Archibald 18,885 Reputation points Volunteer Moderator
    2023-11-20T15:55:04+00:00

    That code should work without having to activate the sheet. What error do you get?

    Was this answer helpful?

    0 comments No comments