+ Reply to Thread
Results 1 to 4 of 4

Clear data not format with Macro

  1. #1
    Forum Contributor
    Join Date
    09-09-2009
    Location
    Columbus. Ohio
    MS-Off Ver
    Excel 2000
    Posts
    199

    Clear data not format with Macro

    I have a worksheet (“A”) that is populated from a second sheet (“B”). The data on “A” changes as necessary using another Macro. Most of the data on sheet “B” is numeric and comes from various formulas and results in numbers with too may digits after the decimal point. I can format sheet “A” to turn 58.22222222 into 58.2 but when I run a Macro to clear the data I lose the formatting. I need to find a way to clear only the data and not the format, or find a way to add a mask on specific columns in the macro that copies and pastes.

    Here is the Macro to clear the data.


    Please Login or Register  to view this content.
    Last edited by Jogier505; 11-07-2009 at 04:57 PM.

  2. #2
    Registered User
    Join Date
    11-06-2009
    Location
    California
    MS-Off Ver
    Excel 2007
    Posts
    39

    Re: Clear data not format with Macro

    One way is to add a formatting macro section after you clear the contents. Use the Record feature, start recording and set the formatting of the datasheet as you want to to look like (including formulas) and then stop the macro. Then go to the new module and copy that data and paste it in your other module just after you clear the contents of the page. This will clear your page and re-enter your formatting data. Be sure to remove any unnecessary lines of code where you clicked randomly or hit incorrect buttons.
    Last edited by Brane Ded; 11-07-2009 at 04:42 PM. Reason: typo

  3. #3
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Clear data not format with Macro

    A couple of things.

    First, be sure to edit out the "selecting" from your recorded macros. The recorder records YOU selecting cells then doing something to them because that's what you did, but it's not necessary for macros per se. They can simply refer to the cells and action all in one step with no "selecting" in advance.

    Second, there's a difference in VBA between .Clear and .ClearContents. You want to use the latter to keep formatting in the cell.

    Please Login or Register  to view this content.
    Last edited by JBeaucaire; 11-07-2009 at 05:50 PM.
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    ?None of us is as good as all of us? - Ray Kroc
    ?Actually, I *am* a rocket scientist.? - JB (little ones count!)

  4. #4
    Forum Contributor
    Join Date
    09-09-2009
    Location
    Columbus. Ohio
    MS-Off Ver
    Excel 2000
    Posts
    199

    Re: Clear data not format with Macro

    Thank you both.

    That appears to get the job done.

    Jim O

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1