+ Reply to Thread
Results 1 to 7 of 7

Macro to format only cells with data

  1. #1
    Registered User
    Join Date
    09-29-2010
    Location
    U.S.
    MS-Off Ver
    Excel 2007
    Posts
    10

    Talking Macro to format only cells with data

    I have a macro that retrieves data from multiple sheets into a summary sheet. I tried using the macro recorder to get code for formatting my data. But the number of rows in my results varies and I need to add borders to only the rows that have data in them. How do I get the formatting to apply to only the cells with data in them?

    Let me know if you need more info.
    Last edited by sparkle; 10-12-2010 at 03:22 PM.

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

    Re: Macro to format only cells with data

    The answer might vary a little from command to command depending on the formatting. It would be safer if you mocked up a 10-20 row BEFORE/AFTER sample so we can see that you really mean what you say mean, people often say one thing and mean something slightly different when you look at it.

    Please Login or Register  to view this content.


    Or:
    Please Login or Register  to view this content.
    _________________
    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!)

  3. #3
    Registered User
    Join Date
    09-29-2010
    Location
    U.S.
    MS-Off Ver
    Excel 2007
    Posts
    10

    Smile Re: Macro to format only cells with data

    basically I just want top and bottom borders on each row.

    see attached.
    Attached Files Attached Files
    Last edited by sparkle; 10-12-2010 at 03:00 PM.

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

    Re: Macro to format only cells with data

    See what I mean? There's no data in column B but you added borders to it...

    Based on what you actually said:
    Please Login or Register  to view this content.

    Based on what you showed:
    Please Login or Register  to view this content.

  5. #5
    Registered User
    Join Date
    09-29-2010
    Location
    U.S.
    MS-Off Ver
    Excel 2007
    Posts
    10

    Talking Re: Macro to format only cells with data

    Thanks for giving me the start. I figured it out using macro recorder and what you gave me. I don't know if all the code is necessary but it works so i'm happy.

    Please Login or Register  to view this content.

  6. #6
    Registered User
    Join Date
    09-29-2010
    Location
    U.S.
    MS-Off Ver
    Excel 2007
    Posts
    10

    Talking Re: Macro to format only cells with data

    Either way is fine actually. I was just leaving some cells blank to see what would happen when I tried to figure it out on my own with test data. The actual data has no blank cells so it's a mute point. But I like learning the difference and thank you for that input.
    Last edited by sparkle; 10-12-2010 at 03:31 PM.

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

    Re: Macro to format only cells with data

    Your recorded example and the one I gave show how much can be taken out of recorded macros. Your recorded version is setting xlNone on every parameter for probably no reason. If there really ARE preexisting borders to be removed, you can still do that with one line of code.

    Also, you don't really need to set the LineStyle to xlContinuous since that is the default, just applying a weight will do that by default.

    Please Login or Register  to view this content.


    Or:
    Please Login or Register  to view this content.

+ 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