+ Reply to Thread
Results 1 to 3 of 3

Macro To Loop From Last Row To Row 4

Hybrid View

  1. #1
    Registered User
    Join Date
    10-29-2012
    Location
    Leeds
    MS-Off Ver
    Excel 2010
    Posts
    49

    Macro To Loop From Last Row To Row 4

    I have two sheets, "Database" and "TeamSheet". I need to get certain data from a team in "Database" and paste it into "TeamSheet". The criteria for this data is obtained from "TeamSheet", it looks up the last active row from that sheet and finds the required data from "Database". My problem is that I don't just want the data for the last row, I want it to loop from the last row right up until row 4. Here is my code so far (which only applies to the last row):

    Sub futboldata()
    
    Dim rngC As Range
    Dim rngJ As Range
    Dim rngL As Range
    Dim rngP As Range
    Dim rngQ As Range
    Dim rngT As Range
    Dim rngU As Range
    Dim rngW As Range
    Dim rngX As Range
    Dim lngCounter As Long
    
    Workbooks("TeamSheet").Activate
    Sheets("Team1").Activate
    lngCounter = Cells.Find("*", [A1], , , xlByRows, xlPrevious).row
    
    Set rngC = Range("C" & rows.Count).End(xlUp)
    
    Set rngT = Range("T" & lngCounter)
    
    Set rngU = Range("U" & lngCounter)
    
    Set rngW = Range("W" & lngCounter)
    
    Set rngX = Range("X" & lngCounter)
    
    
    Workbooks("Database").Activate
    Sheets("Team1").Activate
    
    
    Set rngJ = Range("J" & rows.Count).End(xlUp)
    
    Set rngL = Range("L" & rows.Count).End(xlUp)
    
    Set rngP = Range("P" & rows.Count).End(xlUp)
    
    Set rngQ = Range("Q" & rows.Count).End(xlUp)
    
    'Filter by opposition team, and whether target team played at home (H) or away (A)
    With Workbooks("Database").Sheets("Team1")
    If .FilterMode Then .ShowAllData
    With .Range("A7", .Range("A" & .rows.Count).End(xlUp)).Resize(, 12)
                .AutoFilter Field:=2, Criteria1:=rngC               'C
                .AutoFilter Field:=3, Criteria1:=rngC.Offset(, 2)   'E
    End With
    End With
          
    rngJ.Copy
    Workbooks("TeamSheet").Sheets("Team1").Activate
    rngT.Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
            :=False, Transpose:=False
    
    rngL.Copy
    rngU.Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
            :=False, Transpose:=False
    
    rngP.Copy
    rngW.Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
            :=False, Transpose:=False
            
    rngQ.Copy
    rngX.Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
            :=False, Transpose:=False
    
    End Sub
    Can anyone help?

  2. #2
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,167

    Re: Macro To Loop From Last Row To Row 4

    It will be good if you explain your requirement a little more and maybe even attach a sample file so we can test on it.

    To Attach a File:

    1. Click on Go Advanced
    2. In the frame Attach Files you will see the button Manage Attachments
    3. Click the button.
    4. A new window will open titled Manage Attachments - Excel Forum.
    5. Click the Browse... button to locate your file for uploading.
    6. This will open a new window File Upload.
    7. Once you have located the file to upload click the Open button. This window will close.
    8. You are now back in the Manage Attachments - Excel Forum window.
    9. Click the Upload button and wait until the file has uploaded.
    10. Close the window and then click Submit.
    If I have helped, Don't forget to add to my reputation (click on the star below the post)
    Don't forget to mark threads as "Solved" (Thread Tools->Mark thread as Solved)
    Use code tags when posting your VBA code: [code] Your code here [/code]

  3. #3
    Registered User
    Join Date
    10-29-2012
    Location
    Leeds
    MS-Off Ver
    Excel 2010
    Posts
    49

    Re: Macro To Loop From Last Row To Row 4

    Attachment 250334Attachment 250335Sure, I've attached the two workbooks. I need to get data from "Database Test" into "Blank Team Sheet". The team I'm trying to copy the data for at the moment is Fulham- there are only three games but my code is only able to look up the last game (v Arsenal) and get info for that from "Database Test". The data I am looking for specifically are Total Shots, Shots on Target, Shots OFf Target, Total Home Corners and Total Away Corners. These will be filled into "Blank Team Sheet" based on the data from "Database Test".

    Eventually the goal will be to loop through all the teams and fill the blanks for all the matches...
    Last edited by clattenburg cake; 07-15-2013 at 12:00 PM.

+ 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] Copy dynamically changing column and Paste using VBA Loop (Loop within Loop)
    By nixon72 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 02-12-2013, 12:46 PM
  2. Macro to delete rows with a Zero value. Macro seems to be in continual loop
    By jnorri in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 08-07-2012, 03:20 PM
  3. Loop in macro
    By pdais97 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 09-16-2011, 07:12 AM
  4. Macro Loop Broken. Detects Max but doesn't continue loop
    By herchenbach in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-21-2011, 12:17 PM
  5. Loop Macro!!
    By Big Chris in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 10-20-2009, 09:38 AM

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