+ Reply to Thread
Results 1 to 12 of 12

Thread: Macro to format columns based on cell value

  1. #1
    Registered User
    Join Date
    03-04-2010
    Location
    Wisconsin
    MS-Off Ver
    Excel 2007, Excel 2010, Excel 2003
    Posts
    64

    Talking Macro to format columns based on cell value

    If I were working on Excel 2007 I think I would know how to do this but since I am on 2003 I am hoping someone can tell me if this can even be done.

    Premise:
    I have a "Value" Sheet and depending on whether the currency value is USD or GBP I would like Columns W thru BF, BI,BK,BM,BO,BQ,BS,BU to reflect the appropriate symbol.

    USD = $
    GBP = £

    The cell that dictates what the results will be is $I$3

    Can this be done?

    My VBA skills are improving but as my moniker suggests, I am "Guru in Training" aka "VBA Newbie"!

    Also, can anyone tell me why all of a sudden I can't assign Macros to "buttons"? Is there some sort of option that needs to be reset?

    All help is greatly appreciated!
    Attached Files Attached Files
    Last edited by dawnmau; 10-29-2010 at 10:33 AM. Reason: Resolved
    Dawn - Guru in Training

  2. #2
    Valued Forum Contributor blane245's Avatar
    Join Date
    02-20-2009
    Location
    Melbourne, FL
    MS-Off Ver
    Excel 2010
    Posts
    641

    Re: Macro to format columns based on cell value

    This workshett Change event handler will change the formats of your columns whenever the contents of I3 is changed. Note it only works for the values you provided. The format strings I got from Format->Cells Number:Accounting. I recorded a macro to reveal them.
    Private Sub Worksheet_Change(ByVal Target As Range)
    
    If (Target.Address = "$I$3") Then
        If (Target.Value = "GBP") Then
            Range("W:BF,BI:BI,BK:BK,BM:BM,BO:BO,BQ:BQ,BS:BS,BU:BU").Cells.NumberFormat = _
                "_-[$£-809]* #,##0_-;-[$£-809]* #,##0_-;_-[$£-809]* ""-""_-;_-@_-"
        ElseIf (Target.Value = "USD") Then
            Range("W:BF,BI:BI,BK:BK,BM:BM,BO:BO,BQ:BQ,BS:BS,BU:BU").Cells.NumberFormat = _
                "_($* #,##0_);_($* (#,##0);_($* ""-""_);_(@_)"
        End If
    End If
        
    End Sub
    Bob
    Click my star if my answer helped you. Mark the thread as [SOLVED] if it has been.

  3. #3
    Registered User
    Join Date
    03-04-2010
    Location
    Wisconsin
    MS-Off Ver
    Excel 2007, Excel 2010, Excel 2003
    Posts
    64

    Re: Macro to format columns based on cell value

    Thank you for helping with this, I copied/pasted the code but when I try to run it I get nothing and I can't even find the macro in the list. And for some reason, I used to be able to assign macros to buttons but now I can't. Is there a bigger issue that I need to resolve in order to go forward?
    Dawn - Guru in Training

  4. #4
    Valued Forum Contributor blane245's Avatar
    Join Date
    02-20-2009
    Location
    Melbourne, FL
    MS-Off Ver
    Excel 2010
    Posts
    641

    Re: Macro to format columns based on cell value

    This is not a macro in the sense of executing it by name or button. It is an event handler for the worksheet. It should be place in the Value sheet object in VBA. Each time you make a change on that worksheet, this module will run. It only affects the worksheet when you change the currency field on the Value sheet. If you put it in Module1 of the workbook, move it to the Value worksheet object.
    Bob
    Click my star if my answer helped you. Mark the thread as [SOLVED] if it has been.

  5. #5
    Registered User
    Join Date
    03-04-2010
    Location
    Wisconsin
    MS-Off Ver
    Excel 2007, Excel 2010, Excel 2003
    Posts
    64

    Re: Macro to format columns based on cell value

    Does it matter that $I$3 is a formula cell that derives it's information from another page?
    Dawn - Guru in Training

  6. #6
    Valued Forum Contributor blane245's Avatar
    Join Date
    02-20-2009
    Location
    Melbourne, FL
    MS-Off Ver
    Excel 2010
    Posts
    641

    Re: Macro to format columns based on cell value

    It shouldn't. The Change event handler is invoked whenever calculationis required. Are you using automatic calculation? If you can, send me your workbook with the forumal and the the event handler in and I'll take a look.
    Bob
    Click my star if my answer helped you. Mark the thread as [SOLVED] if it has been.

  7. #7
    Registered User
    Join Date
    03-04-2010
    Location
    Wisconsin
    MS-Off Ver
    Excel 2007, Excel 2010, Excel 2003
    Posts
    64

    Re: Macro to format columns based on cell value

    Ok Newbie alert! Ummmm. how do I "move it to the Value worksheet object". I know how to add modules but I am not sure how to do what you suggested. I have attached file. I learn by example but am primarily a visual picture kind of gal. Word problems suck for me.
    Attached Files Attached Files
    Dawn - Guru in Training

  8. #8
    Valued Forum Contributor blane245's Avatar
    Join Date
    02-20-2009
    Location
    Melbourne, FL
    MS-Off Ver
    Excel 2010
    Posts
    641

    Re: Macro to format columns based on cell value

    Sorry to confuse you. I have made the move in the attached file. When you are in VBA you will notice in the Project Browser where you have added Module1. Well above that are the Microsoft Excel Objects. If you double click on the one labeled Sheet3 (Value) you will see where I put the routine. When you are working with worksheet event handlers, that is where they go. Each worksheet has its one event handlers. The same is true of thisWorkBook, which should contain workbook event handlers (like Open).

    If you add a new worksheet to your workbook while in Excel you will see that a new Sheet object is created in the Microsoft Excel Objects.

    Keep using your Guru skills at learning
    Full%20Scope%20Template(1).xls
    Bob
    Click my star if my answer helped you. Mark the thread as [SOLVED] if it has been.

  9. #9
    Registered User
    Join Date
    03-04-2010
    Location
    Wisconsin
    MS-Off Ver
    Excel 2007, Excel 2010, Excel 2003
    Posts
    64

    Re: Macro to format columns based on cell value

    Awesome and I double checked to see how I would do it and works perfectly! Yay!! Learned a cool new toy! You wouldn't by any chance know why I all of sudden can't add buttons and assign macros would you?
    Dawn - Guru in Training

  10. #10
    Valued Forum Contributor blane245's Avatar
    Join Date
    02-20-2009
    Location
    Melbourne, FL
    MS-Off Ver
    Excel 2010
    Posts
    641

    Re: Macro to format columns based on cell value

    What kind of buttons are you talking about? Toolbar buttons, or Control buttons (created from the Control Toolbox). You can also assign macros to shapes.
    Bob
    Click my star if my answer helped you. Mark the thread as [SOLVED] if it has been.

  11. #11
    Registered User
    Join Date
    03-04-2010
    Location
    Wisconsin
    MS-Off Ver
    Excel 2007, Excel 2010, Excel 2003
    Posts
    64

    Re: Macro to format columns based on cell value

    I had been using the "buttons" from the Control Toolbox. I select the Command Button, place it on the worksheet and then try to "Assign Macro" but the option is not available. I can't even seem to rename it! I could have sworn that was what I was doing previously and never had a problem before. I have a couple of macros I want to assign to the Template once information has been populated and don't know if I "switched" something off by accident?
    Dawn - Guru in Training

  12. #12
    Registered User
    Join Date
    03-04-2010
    Location
    Wisconsin
    MS-Off Ver
    Excel 2007, Excel 2010, Excel 2003
    Posts
    64

    Re: Macro to format columns based on cell value

    Figured out what my problem was!! I need to use the "FORMS" Toolbar, NOT the Control Toolbox. Thanks so much for your awesome help 5 gold stars for you!!
    Dawn - Guru in Training

+ 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.2.0