+ Reply to Thread
Results 1 to 10 of 10

change number format based on another cell?

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    09-16-2009
    Location
    Glasgow, Scotland
    MS-Off Ver
    Excel XP, 2003, 2010
    Posts
    125

    change number format based on another cell?

    Hi Everyone,

    I'm new to this and also VBA so please show me some 'nab patience'
    I have used this forum periodically and it has saved my bacon on numerous occasions so I know someone can help with this!

    I created a workbook for staff planning and it's becoming a behemoth!

    I'm wanting a range of cells within a row to change the number format to "[hh]:mm" based on a value in the 2nd column of that row.
    I have played about with a few things and can't seem to get it to work. What I have so far is this:

    Private Sub Worksheet_Change(ByVal Target As Range)
    Dim myCell As Range
    If Intersect(Target, Range("B:B")) Is Nothing Then Exit Sub
    For Each myCell In Intersect(Target, Range("B:B"))
    If UCase(myCell.Value) = "P/T" Then
    Cells(myCell.Row, 3).NumberFormat = "[hh]:mm"
    End If
    If UCase(myCell.Value) = "F/T" Then
    Cells(myCell.Row, 3).NumberFormat = general
    End If
    Next myCell
    End Sub
    Sample data:

    A B C D E
    Staff member 1 F/T 27 3 24
    Staff member 2 F/T 20 3 17
    Staff member 3 P/T 176:00 3 4.333333333
    Satff member 4 P/T 170:00 3 4.083333333
    Staff member 5 F/T 20 3 17

    Column C has an 'IF' nested with 'vlookup' to reference from another sheet based on column B value

    Column D has the total leave taken so far in the year

    Column E is 'C' minus 'D' = leave remaining.

    So, I want D and E to have the same number format as C.

    I can't do this manually as this sheet auto updates the staff data as new staff join and staff leaving which would mean these manually formatted cells would be out of sync as rows are auto inserted/deleted.

    I have tried to be as precise as I can but if I missed anything out, please ask.

    Thanks in advance!
    Last edited by Fidd$; 09-18-2009 at 09:55 AM. Reason: update info

  2. #2
    Forum Contributor vijay2482's Avatar
    Join Date
    03-03-2009
    Location
    Paris,France
    MS-Off Ver
    Excel 2003
    Posts
    263

    Re: change number format based on another cell?

    I'm not sure what I understood is correct.
    Anyways , my suggestion is to set the columns C,D and E to the same formate in the IF statement.
    Something like this:
    If UCase(myCell.Value) = "P/T" Then
    Cells(myCell.Row, 3).NumberFormat = "[hh]:mm"
    Cells(myCell.Row, 4).NumberFormat = "[hh]:mm"
    Cells(myCell.Row, 5).NumberFormat = "[hh]:mm"
    End If
    Try your code whith this change.
    Hope it does some change in your output.

  3. #3
    Forum Contributor
    Join Date
    09-16-2009
    Location
    Glasgow, Scotland
    MS-Off Ver
    Excel XP, 2003, 2010
    Posts
    125

    Re: change number format based on another cell?

    Thanks for that Vijay! What a quick reply!

    It works now, thanks!

    BUT.....is there a way of it calculating automatically instead of having to press F2 and return?

  4. #4
    Forum Contributor vijay2482's Avatar
    Join Date
    03-03-2009
    Location
    Paris,France
    MS-Off Ver
    Excel 2003
    Posts
    263

    Re: change number format based on another cell?

    I dont get you, why do you press F2?

  5. #5
    Forum Contributor vijay2482's Avatar
    Join Date
    03-03-2009
    Location
    Paris,France
    MS-Off Ver
    Excel 2003
    Posts
    263

    Re: change number format based on another cell?

    Fidd, I definetly agree with you that Donkey's code is compact, because they are experts and I'm a beginner in VBA.
    Anyways, happy that even my suggestion produced a correct output.

  6. #6
    Forum Contributor
    Join Date
    09-16-2009
    Location
    Glasgow, Scotland
    MS-Off Ver
    Excel XP, 2003, 2010
    Posts
    125

    Re: change number format based on another cell?

    Donkey's code is much more compact and works just as well so I may use that instead, sorry Vijay

  7. #7
    Forum Contributor
    Join Date
    09-16-2009
    Location
    Glasgow, Scotland
    MS-Off Ver
    Excel XP, 2003, 2010
    Posts
    125

    Re: change number format based on another cell?

    Vijay,

    Column B has the "P/T" or "F/T" value already in it ref'd from another sheet. I have to go to that cell and 'go into' (edit) the cell and out again for the code to work, otherwise it won't pick it up and change the number format accordingly.
    It's not a biggie but I want the sheet to be as automated as possible because from experience, even the most simplest of manual tasks can be input incorrectly after repeated attempts to explain to staff what to do!

  8. #8
    Forum Contributor vijay2482's Avatar
    Join Date
    03-03-2009
    Location
    Paris,France
    MS-Off Ver
    Excel 2003
    Posts
    263

    Re: change number format based on another cell?

    why dont you try to have a listbox, and get the value from the other sheet?
    and based on the selection, the numberformat can be fixed.

  9. #9
    Forum Contributor
    Join Date
    09-16-2009
    Location
    Glasgow, Scotland
    MS-Off Ver
    Excel XP, 2003, 2010
    Posts
    125

    Re: change number format based on another cell?

    Aaaahhh, Vijay, it's always the simple solutions that evades me lol
    I seem to make things harder for myself!
    Thanks for all the help guys

  10. #10
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: change number format based on another cell?

    Do you mean ?

    Private Sub Worksheet_Change(ByVal Target As Range)
    Dim myCell As Range
    If Intersect(Target, Range("B:B")) Is Nothing Then Exit Sub
    For Each myCell In Intersect(Target, Range("B:B"))
        myCell.Offset(,1).Resize(,3).NumberFormat = IIF(UCase(Target.Value)="P/T","[hh]:mm","General")
    Next myCell
    End Sub

+ 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