We’d like to remind Forumites to please avoid political debate on the Forum.

This is to keep it a safe and useful space for MoneySaving discussions. Threads that are – or become – political in nature may be removed in line with the Forum’s rules. Thank you for your understanding.

📨 Have you signed up to the Forum's new Email Digest yet? Get a selection of trending threads sent straight to your inbox daily, weekly or monthly!

Excel Help

Hi,
I'm struggling with an excel spreadsheet. I need to run a macro automatically, when the figures change in a cell. I can write the macro but can't get it to run without physically selecting it first. Any help or vba code would be muchly appreciated

It's for this year's world cup, to update tables, points, play-offs who plays who etc.
I just enter the scores and it does the rest, all except sort the groups into point order, which I have to do manually.
If anyone wants a copy, pm me

Comments

  • andydiysaver
    andydiysaver Posts: 424 Forumite
    hi
    I do developing in my spare time to keep the cash high -




    within the SHEET CODE you need this


    Private Sub Worksheet_Change(ByVal Target As Range)
    Application.EnableEvents = False
    'don't loop it, do one operation for one change in events!


    If Target.Range = Sheet1.Range ("your cell ref")
    'changing cell


    'YOUR MACRO HERE BASED ON TARGET CHANGING




    Application.EnableEvents = True
    'turn it back on

    End Sub




    sheet2 if it's sheet 2 etc.
  • nigel3a
    nigel3a Posts: 11 Forumite
    Hi,
    Thanks for your replies.

    I can assign a macro to a button, but I'm not sure if setting out the code as below is what you mean. If you PM me your email, I'll send it you, might be easier to understand.
    Nigel

    Private Sub Worksheet_Change(ByVal Target As Range)
    Application.EnableEvents = False
    'don't loop it, do one operation for one change in events!


    If Target.Range = Groups.Range("P4:P7") Then
    'changing cell


    Range("H3:P7").Select
    ActiveWorkbook.Worksheets("Groups").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("Groups").Sort.SortFields.Add Key:=Range("P4:P7"), _
    SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal
    ActiveWorkbook.Worksheets("Groups").Sort.SortFields.Add Key:=Range("O4:O7"), _
    SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal
    With ActiveWorkbook.Worksheets("Groups").Sort
    .SetRange Range("H3:P7")
    .Header = xlYes
    .MatchCase = False
    .Orientation = xlTopToBottom
    .SortMethod = xlPinYin
    .Apply
    End With

    Application.EnableEvents = True


    If Target.Range = Groups.Range("P12:P15") Then
    Range("H11:P15").Select
    ActiveWorkbook.Worksheets("Groups").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("Groups").Sort.SortFields.Add Key:=Range("P12:P15") _
    , SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal
    ActiveWorkbook.Worksheets("Groups").Sort.SortFields.Add Key:=Range("O12:O15") _
    , SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal
    With ActiveWorkbook.Worksheets("Groups").Sort
    .SetRange Range("H11:P15")
    .Header = xlYes
    .MatchCase = False
    .Orientation = xlTopToBottom
    .SortMethod = xlPinYin
    .Apply
    End With
  • nigel3a
    nigel3a Posts: 11 Forumite
    JohnMC

    I want it to sort the order, not necessarily show a message, which I think is what the link is telling me
  • Bigphil1474
    Bigphil1474 Posts: 3,706 Forumite
    Part of the Furniture 1,000 Posts Photogenic Name Dropper
    nigel, I've done a similar thing.

    Basically, I have all the fixtures with cells for the scores. When I enter the scores it automatically updates the tables with the teams goals for/against (based on cell content), and win/lose/draw based on the 'IF' function i.e. IF d2>d3'w' etc. Multiply the number of W's by 3, add number of D's to get total points. To rank the teams I used the following function [=RANK.EQ(S3,S3:S6,0)] which ranks them based on total points (points in S3 to S6). Only fall down is that teams with the same points have the same rank. Couldn't be bothered sorting that out, but it does the trick for me, to manually transfer teams to second round. It doesn't re-order the teams within the table either, but not a big issue for me.
    Currently being used to work out predictions for the final, and so far I've predicted the full set of fixtures 10 different times, and got Argentina v. Brazil to play each other in the final each time, even when throwing in the odd highly unlikely scenario such as England making it to the semis.
This discussion has been closed.
Meet your Ambassadors

🚀 Getting Started

Hi new member!

Our Getting Started Guide will help you get the most out of the Forum

Categories

  • All Categories
  • 352.1K Banking & Borrowing
  • 253.5K Reduce Debt & Boost Income
  • 454.2K Spending & Discounts
  • 245.1K Work, Benefits & Business
  • 600.7K Mortgages, Homes & Bills
  • 177.5K Life & Family
  • 258.9K Travel & Transport
  • 1.5M Hobbies & Leisure
  • 16.2K Discuss & Feedback
  • 37.6K Read-Only Boards

Is this how you want to be seen?

We see you are using a default avatar. It takes only a few seconds to pick a picture.