How to use vba code to clear specified cell?

Anonymous
2021-01-10T23:29:42+00:00

Hi

How to use vba code to clear specified cell contents automatically when I opened the spreadsheet?

For example, when I click to open the spreadsheet , once the spreadsheet opened, then the specified cell contents have been deleted automatically.

Many Thanks

Fj

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
{count} votes
Answer accepted by question author
  1. Anonymous
    2021-01-11T00:05:57+00:00

    Hi FJ

    If you want to run the macro when you open your workbook (file). 

    1- The code goes on the Thisworkbook module Procedure as shown in the picture below

    2- Specify the sheet/range or sheets/ranges you want to delete contents

    Image

    Private Sub Workbook_Open()

    With Sheets("Sheet1")

            .Range("A1").ClearContents  ''' Deletes contents but not cell format

            .Range("A1").Value = ""     ''' Deletes contents but not cell format

            .Range("A1").Clear        ''' Deletes all

    End With

    End Sub

    '''''******************************************************************************************

    If you have the workbook (file) opened and you want to run the macro once you activate the specific sheet then

    The code below goes to the Worksheet event panel

    Choose the command you prefer.

    Image

    Private Sub Worksheet_Activate()

    Range("A1").ClearContents  ''' Deletes contents but not cell format

    Range("A1").Value = ""     ''' Deletes contents but not cell format

    Range("A1").Clear        ''' Deletes all

    End Sub

    '''********************************************************************

    If you need more help, please,

    Be more specific and give us more details of your scenario and goals.

    0 comments No comments

0 additional answers

Sort by: Most helpful