# Help with a EXCEL macro, Loops

**URL:** <https://boards.straightdope.com/t/help-with-a-excel-macro-loops/783257>\
**Category:** Factual Questions\
**Created:** [March 29, 2017, 12:53am UTC](https://boards.straightdope.com/t/help-with-a-excel-macro-loops/783257 "2017-03-29T00:53:54Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![notfrommensa](https://avatars.discourse-cdn.com/v4/letter/n/f14d63/32.png) [@notfrommensa](https://boards.straightdope.com/u/notfrommensa)\
**Post date:** [March 29, 2017, 12:53am UTC](https://boards.straightdope.com/t/help-with-a-excel-macro-loops/783257/1 "2017-03-29T00:53:54Z")

</div>

I need to data mine golf tournament data from a website.

I recorded a macro to do the download, but I need it to loop multiple times

```auto

Sub DownloadTournamentData()
'
' DownloadTournamentData Macro
'
'
    ActiveWorkbook.Worksheets.Add
    With ActiveSheet.QueryTables.Add(Connection:= _
        "URL;http://www.owgr.com/en/Events/EventResult.aspx?eventid= **4504**", Destination _
        :=Range("$A$1"))
        .Name = "EventResult.aspx?eventid= **4504**"

        ...
        *extraneous code*
        ...

    End With
End Sub

```

I need a loop so I can download eventids from 4504 to 4604 (I want 100 tournaments so the files don’t get really big) but I can’t figure how to insert the loop number into the bolded code.

Thanks for the help.

---

<div class="post-metadata">

**Author:** ![BubbaDog](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/bubbadog/32/333_2.png) [@BubbaDog](https://boards.straightdope.com/u/BubbaDog)\
**Post date:** [March 29, 2017, 1:21pm UTC](https://boards.straightdope.com/t/help-with-a-excel-macro-loops/783257/2 "2017-03-29T13:21:39Z")

</div>

You might try creating the connection and .Name parts as a variable and then looping the whole thing.

I don’t promise that this code will work as is but hopefully you can adapt it

```auto

For Countnum = 4504 To 4604
    UrlName = "URL;http://www.owgr.com/en/Events/EventResult.aspx?eventid=" & Countnum
    Eventname = "EventResult.aspx?eventid=" & Countnum
    
      ActiveWorkbook.Worksheets.Add
        With ActiveSheet.QueryTables.Add(Connection:=UrlName, Destination:=Range("$A$1"))
            .Name = Eventname
        End With

        ...
        extraneous code
        ...   
Next Countnum

```

This might work although I haven’t tested the code.

---

<div class="post-metadata">

**Author:** ![notfrommensa](https://avatars.discourse-cdn.com/v4/letter/n/f14d63/32.png) [@notfrommensa](https://boards.straightdope.com/u/notfrommensa)\
**Post date:** [March 29, 2017, 4:29pm UTC](https://boards.straightdope.com/t/help-with-a-excel-macro-loops/783257/3 "2017-03-29T16:29:50Z")

</div>

Thanks, I haven’t been able to try it. I will look at it this evening.

---

<div class="post-metadata">

**Author:** ![notfrommensa](https://avatars.discourse-cdn.com/v4/letter/n/f14d63/32.png) [@notfrommensa](https://boards.straightdope.com/u/notfrommensa)\
**Post date:** [March 29, 2017, 11:25pm UTC](https://boards.straightdope.com/t/help-with-a-excel-macro-loops/783257/4 "2017-03-29T23:25:26Z")

</div>

Thanks, I got it to work

---

<div class="post-metadata">

**Author:** ![BubbaDog](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/bubbadog/32/333_2.png) [@BubbaDog](https://boards.straightdope.com/u/BubbaDog)\
**Post date:** [March 30, 2017, 1:54pm UTC](https://boards.straightdope.com/t/help-with-a-excel-macro-loops/783257/5 "2017-03-30T13:54:56Z")

</div>

> [@notfrommensa](#):
>
> Thanks, I got it to work

Great!
