+ Reply to Thread
Results 1 to 4 of 4

simple macro to format numbers doesnt work

Hybrid View

  1. #1
    Registered User
    Join Date
    10-29-2017
    Location
    Miami, FL
    MS-Off Ver
    2010
    Posts
    76

    simple macro to format numbers doesnt work

    i have the below code but it doesnt work for some reason! the code runs without breaking but it does not format at all. after running the code, if you look at the conditional formatting button in the ribbon, it shows that the formatting is there but i just doesnt apply it to any cells. all i want if for number greater than one to have 1 decimal place and number less than or equal to one to have two decimal places. please advise!!! thank you!!!!

    Sub recent()
       
        sorter_col = Cells.Find("Spread", [A1], , , xlByColumns, xlPrevious).Column
      
        Dim rg As Range
        Dim cond1 As FormatCondition, cond2 As FormatCondition
        Set rg = Cells.Columns(sorter_col)
        Cells.Columns(sorter_col).Select
       
        
        'clear any existing conditional formatting
        rg.FormatConditions.Delete
     
        'define the rule for each conditional format
        Set cond1 = rg.FormatConditions.Add(xlCellValue, xlGreater, "=1")
        Set cond2 = rg.FormatConditions.Add(xlCellValue, xlLessEqual, "=1")
     
        'define the format applied for each conditional format
        With cond1
        NumberFormat = "0.0"
        End With
     
        With cond2
        NumberFormat = "0.00"
        End With
       
    End Sub

  2. #2
    Forum Expert
    Join Date
    11-26-2013
    Location
    Colac, Victoria, Australia
    MS-Off Ver
    Excel 2016
    Posts
    1,309

    Re: simple macro to format numbers doesnt work

    Try this:

    Sub recent()
       
        sorter_col = Cells.Find("Spread", [A1], , , xlByColumns, xlPrevious).Column
      
        Dim rg As Range
        Dim cond1 As FormatCondition, cond2 As FormatCondition
        Set rg = Cells.Columns(sorter_col)
        Cells.Columns(sorter_col).Select
       
        
        'clear any existing conditional formatting
        rg.FormatConditions.Delete
     
        'define the rule for each conditional format
        Set cond1 = rg.FormatConditions.Add(xlCellValue, xlGreater, "=1")
        Set cond2 = rg.FormatConditions.Add(xlCellValue, xlLessEqual, "=1")
        rg.FormatConditions(1).NumberFormat = "0.0"
        rg.FormatConditions(2).NumberFormat = "0.00"
        
        End With
       
    End Sub
    I hope this helps, please let me know!

    Regards,

    David

    If this has been helpful
    - Please click on the *Add Reputation button at the bottom left.

    Please mark your thread as SOLVED:
    - Click Thread Tools above your first post, select "Mark your thread as Solved".


  3. #3
    Registered User
    Join Date
    10-29-2017
    Location
    Miami, FL
    MS-Off Ver
    2010
    Posts
    76
    Works. Thank you!!!

    Quote Originally Posted by David A Coop View Post
    Try this:

    Sub recent()
       
        sorter_col = Cells.Find("Spread", [A1], , , xlByColumns, xlPrevious).Column
      
        Dim rg As Range
        Dim cond1 As FormatCondition, cond2 As FormatCondition
        Set rg = Cells.Columns(sorter_col)
        Cells.Columns(sorter_col).Select
       
        
        'clear any existing conditional formatting
        rg.FormatConditions.Delete
     
        'define the rule for each conditional format
        Set cond1 = rg.FormatConditions.Add(xlCellValue, xlGreater, "=1")
        Set cond2 = rg.FormatConditions.Add(xlCellValue, xlLessEqual, "=1")
        rg.FormatConditions(1).NumberFormat = "0.0"
        rg.FormatConditions(2).NumberFormat = "0.00"
        
        End With
       
    End Sub
    I hope this helps, please let me know!

    Regards,

    David

    If this has been helpful
    - Please click on the *Add Reputation button at the bottom left.

    Please mark your thread as SOLVED:
    - Click Thread Tools above your first post, select "Mark your thread as Solved".


  4. #4
    Forum Expert
    Join Date
    11-26-2013
    Location
    Colac, Victoria, Australia
    MS-Off Ver
    Excel 2016
    Posts
    1,309

    Re: simple macro to format numbers doesnt work

    Quote Originally Posted by fruitloop44 View Post
    Works. Thank you!!!
    Good to hear!

    Please don't forget to mark as SOLVED etc.

    I hope this helps, please let me know!

    Regards,

    David

    If this has been helpful
    - Please click on the *Add Reputation button at the bottom left.

    Please mark your thread as SOLVED:
    - Click Thread Tools above your first post, select "Mark your thread as Solved".


+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Macro which used to work, now doesnt -
    By Spyros13 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 10-25-2016, 02:08 PM
  2. Macro copy doesnt work right
    By vindalloo77 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 08-23-2016, 08:34 AM
  3. the code doesnt work with macro event
    By samisamih in forum Excel Programming / VBA / Macros
    Replies: 16
    Last Post: 04-21-2015, 03:41 AM
  4. Powerpoint macro to paste and format doesnt work anymore
    By mikeTRON in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 07-17-2013, 07:44 PM
  5. My loop macro doesnt work
    By matdog in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 04-08-2010, 02:32 AM
  6. some functions doesnt work in macro?
    By ExcelPower in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 02-20-2006, 03:40 AM
  7. Please help : Macro doesnt work on other PCs
    By Jason in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 12-26-2005, 05:55 AM

Tags for this Thread

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