+ Reply to Thread
Results 1 to 4 of 4

VBA Code - Taking notice of only the last 5 entries of a certain value in a table

  1. #1
    Registered User
    Join Date
    11-02-2014
    Location
    London
    MS-Off Ver
    Excel For Mac 2011
    Posts
    10

    VBA Code - Taking notice of only the last 5 entries of a certain value in a table

    Hello.

    I've got a table of UK football (soccer for you US-based folk) results which shows Fixture Date, Home Team, Away Team, Home Goals, Away Goals (last two therefore give the final score), and Result (which lists either H, D or A for Home Win, Draw or Away Win).

    I've written a code so that I can check the overall form of every team (by this, I mean it analyses all the results of the team that is entered in a certain cell and delivers how many Wins, Draws and Losses they have had in all their games so far). To do this, I use the code below (thanks to bmouse for helping with this in a different thread):

    ---------

    Dim counterW As Integer
    Dim counterL As Integer
    Dim counterD As Integer
    Dim StartNumber As Integer
    Dim EndNumber As Integer

    EndNumber = 1000
    counterW = 0
    counterL = 0
    counterD = 0

    For StartNumber = 2 To EndNumber

    If Cells(2, "A") = Cells(StartNumber, "J") Then

    If Cells(StartNumber, "N") = "H" Then

    counterW = counterW + 1

    Else

    If Cells(StartNumber, "N") = "D" Then

    counterD = counterD + 1

    Else: counterL = counterL + 1
    End If
    End If
    End If

    Next StartNumber



    For StartNumber = 2 To EndNumber

    If Cells(2, "A") = Cells(StartNumber, "K") Then

    If Cells(StartNumber, "N") = "A" Then

    counterW = counterW + 1

    Else

    If Cells(StartNumber, "N") = "D" Then

    counterD = counterD + 1

    Else: counterL = counterL + 1


    End If
    End If
    End If

    Next StartNumber

    Cells(2, "C").Value = counterW
    Cells(2, "D").Value = counterD
    Cells(2, "E").Value = counterL

    End Sub

    -------

    This works great, but now what I want to do is to do a FORM table; that is, a table which shows their Wins/Draws/Losses in just their last 5 games.

    Is there some kind of VBA code I can use to alter the above in order to take only their last 5 results?

    Thanks in advance!

  2. #2
    Valued Forum Contributor
    Join Date
    08-22-2011
    Location
    Auckland
    MS-Off Ver
    Excel 2019
    Posts
    716

    Re: VBA Code - Taking notice of only the last 5 entries of a certain value in a table

    You're going to be in the naughty book for not wrapping your code in tags # in reply box
    It would be better if you attach a sample workbook so we don't have to muck about trying to recreate your workbook

  3. #3
    Forum Contributor
    Join Date
    08-04-2014
    Location
    Riga
    MS-Off Ver
    2010
    Posts
    579

    Re: VBA Code - Taking notice of only the last 5 entries of a certain value in a table

    My way of doing this would be this. Not very efficient, I know, but nothing better comes to mind yet.

    Put auto-filters on your data range and filter out scores for 1 team as Home Team, copy/paste that data to another sheet. Remove the auto-filter, then put the filter on Away Team and filter data for the same team. Copy/paste the data to the other sheet. Sort the copied data by date (I have game dates in my custom-made list, not sure about your data, though), and keep the data for the last 5 dates, remove the rest.

    After that, I suppose you could tweak the macro you've been using to calculate the necessary results and percentages.

    The problem is that this is time consuming, as you would have to filter out data for each team and keep the data seperate, otherwise the macro will provide inaccurate results.

    I'll think about this some more, for now this is the best advice I can give. Good luck

  4. #4
    Registered User
    Join Date
    11-02-2014
    Location
    London
    MS-Off Ver
    Excel For Mac 2011
    Posts
    10

    Re: VBA Code - Taking notice of only the last 5 entries of a certain value in a table

    Quote Originally Posted by bmouse View Post
    My way of doing this would be this. Not very efficient, I know, but nothing better comes to mind yet.

    Put auto-filters on your data range and filter out scores for 1 team as Home Team, copy/paste that data to another sheet. Remove the auto-filter, then put the filter on Away Team and filter data for the same team. Copy/paste the data to the other sheet. Sort the copied data by date (I have game dates in my custom-made list, not sure about your data, though), and keep the data for the last 5 dates, remove the rest.

    After that, I suppose you could tweak the macro you've been using to calculate the necessary results and percentages.

    The problem is that this is time consuming, as you would have to filter out data for each team and keep the data seperate, otherwise the macro will provide inaccurate results.

    I'll think about this some more, for now this is the best advice I can give. Good luck
    Thanks for the reply.

    Unfortunately I'm looking for something that can be done on a large scale to calculate many teams at once (as you've helped me with previously on Home Form/Overall Form in previous threads ).

    Maybe it isn't possible, in which case I'll tweak my formula to remove the 'Form' part if necessary. Let me know if you have any other ideas!

    Thanks.

+ 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. [SOLVED] Taking entries from certain columns and putting them together
    By Hitch75 in forum Excel General
    Replies: 2
    Last Post: 04-06-2014, 04:45 PM
  2. Taking multiple occurances from one table and displaying them on a new table
    By Courtenay Davidson in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 11-23-2012, 05:39 AM
  3. Code to automatically enble/disable security notice for macro?
    By bigkahuna2187 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 08-20-2012, 06:23 PM
  4. Taking some data from table
    By clayman in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 01-07-2009, 10:39 AM
  5. Help with taking formula to vba code
    By Sasha in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 12-05-2005, 07:10 PM

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