Making this code more efficient

NewbieMan

New Member
Joined
Nov 25, 2017
Messages
33
Wonder if someone could help me with the code below. First, it does work however so you can try it. Its not very efficient in the following manner. I am using it to generate information for all NCAA coaches. I have completed the first 2 however you can imagine doing this for all of them will be very uneconomical. I would like to ask for a couple of things..

1) for the wins, losses and experience paramters (the games played will likely come out) I am having problems with creating a search function that will sum the info from row 3 down until the End but then come up 2 rows..sort of like End(xlDown).offset(-2,0).
2) Because I am still new, I can look at the code and conceptualize that there must be a way to loop this rather than rewriting essentially the same code out dozens of times. The main difference will come in 2 places really. The first will be the html page as each coach has his own...so I imagine writing a string variable for the coach name (as the rest of the html is the same for all) and also in the wins, losses and experience, each coach due to his experience will have more or less rows (hence my hope to have VBA go to the end of the data and then backup 2 rows since not everyone will have just 4 years of experience..Please note that I do not want to include 2017-18 since the season is not over.

Right now I am doing the sub then calling the next one for the next coach. Thats how I would write this, but I hope that someone can make the adjustment keeping in mind that the code also creates a sheet in the workbook named after the coach.

How could this be written to loop the first sub and grab the data for other coaches? Would I just change the name of the coach in the html to a string variable and then list all of the coach names? Im getting better with straight codes but looping is still a challenge given my background has nothing to do with tech.

Thanks for any and all help in advance.



Option Explicit
Public qt As QueryTable
Public ws As Worksheet
Public URL As String




Sub CoachAbileneChristian()
'School: Abilene Christian
'Coach: Joe Golding


URL = "https://www.sports-reference.com/cbb/coaches/joe-golding-1.html"


Set ws = ActiveSheet
'set up a table import (the URL; tells Excel that this query comes from a website)
Set qt = ws.QueryTables.Add(Connection:="URL;" & URL, Destination:=Range("A1"))
'tell Excel to refresh the query whenever you open the file
qt.RefreshOnFileOpen = True
'giving the query a name can help you refer to it later
qt.Name = "JoeGoldingCareer"
'you want to import column headers
qt.FieldNames = True
'need to know name or number of table to bring in
'(we'll bring in the first table)
qt.WebFormatting = xlWebFormattingNone
'qt.WebSelectionType = xlAllTables
'qt.WebTables =1
'import the data
qt.Refresh BackgroundQuery:=False


ActiveSheet.Name = "JoeGolding"


'Dim TotalGames As Range
'Set TotalGames = Range("D3:D6")
'Results = WorksheetFunction.Sum(TotalGames)
'Range("P3") = Results


Dim Results As Long
Dim TotalExperience As Range
Dim Career As Integer


Set TotalExperience = Range("B3:B6")
Results = TotalExperience.EntireRow.Count
Range("P3") = Results




Dim TotalWins As Range


Set TotalWins = Range("E3:E6")
Results = WorksheetFunction.Sum(TotalWins)
Range("Q3") = Results


Dim TotalLosses As Range


Set TotalLosses = Range("F3:F6")
Results = WorksheetFunction.Sum(TotalLosses)
Range("R3") = Results


Set ws = Sheets.Add
Sheets(1).Activate
Call CoachAirForce


End Sub


Sub CoachAirForce()
'School: Air Force
'Coach: Dave Pilipovich


URL = "https://www.sports-reference.com/cbb/coaches/dave-pilopovich-1.html"


Set ws = ActiveSheet
'set up a table import (the URL; tells Excel that this query comes from a website)
Set qt = ws.QueryTables.Add(Connection:="URL;" & URL, Destination:=Range("A1"))
'tell Excel to refresh the query whenever you open the file
qt.RefreshOnFileOpen = True
'giving the query a name can help you refer to it later
qt.Name = "DavePilipovichCareer"
'you want to import column headers
qt.FieldNames = True
'need to know name or number of table to bring in
'(we'll bring in the first table)
qt.WebFormatting = xlWebFormattingNone
'qt.WebSelectionType = xlAllTables
'qt.WebTables =1
'import the data
qt.Refresh BackgroundQuery:=False


ActiveSheet.Name = "DavePilipovich"


'Dim TotalGames As Range
'Set TotalGames = Range("D3:D6")
'Results = WorksheetFunction.Sum(TotalGames)
'Range("P3") = Results


Dim Results As Long
Dim TotalExperience As Range
Dim Career As Integer


Set TotalExperience = Range("B3:B6")
Results = TotalExperience.EntireRow.Count
Range("P3") = Results




Dim TotalWins As Range


Set TotalWins = Range("E3:E6")
Results = WorksheetFunction.Sum(TotalWins)
Range("Q3") = Results


Dim TotalLosses As Range


Set TotalLosses = Range("F3:F6")
Results = WorksheetFunction.Sum(TotalLosses)
Range("R3") = Results


Set ws = Sheets.Add
Sheets(1).Activate


End Sub
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
Re: Making this code more efficient please

If U have formatted everything the same for all coach names U just need to make some adjustments to how U call the sub and some of the code. Untested code. HTH. Dave
ps. please use code tags
Code:
 Option Explicit
 Public qt As QueryTable
 Public ws As Worksheet
 Public URL As String

Sub YourSubName(Coachname As String, TableNo As Integer)
'Call YourSubName("joe-golding", 1)
Dim NewStr As String, Cnt As Integer
'remove "-" from name
For Cnt = 1 To Len(Coachname)
If Asc(Mid(Coachname, Cnt, 1)) <> 45 Then
NewStr = NewStr & Mid(Coachname, Cnt, 1)
End If
Next Cnt
 
'**URL = "https://www.sports-reference.com/cbb/coaches/joe-golding-1.html"
URL = "https://www.sports-reference.com/cbb/coaches/" & _
                          Coachname & "-" & TableNo & ".html"
Set ws = ActiveSheet
 'set up a table import (the URL; tells Excel that this query comes from a website)
 Set qt = ws.QueryTables.Add(Connection:="URL;" & URL, Destination:=Range("A1"))
 'tell Excel to refresh the query whenever you open the file
 qt.RefreshOnFileOpen = True
 'giving the query a name can help you refer to it later
'**qt.Name = "JoeGoldingCareer"
qt.Name = NewStr & "Career"
 'you want to import column headers
 qt.FieldNames = True
 'need to know name or number of table to bring in
 '(we'll bring in the first table)
 qt.WebFormatting = xlWebFormattingNone
 'qt.WebSelectionType = xlAllTables
'**qt.WebTables =1
'qt.WebTables = TableNo
 'import the data
 qt.Refresh BackgroundQuery:=False
'**ActiveSheet.Name = "JoeGolding"
ActiveSheet.Name = NewStr
 'Dim TotalGames As Range
 'Set TotalGames = Range("D3:D6")
 'Results = WorksheetFunction.Sum(TotalGames)
 'Range("P3") = Results
 Dim Results As Long
 Dim TotalExperience As Range
 Dim Career As Integer
 Set TotalExperience = Range("B3:B6")
 Results = TotalExperience.EntireRow.Count
 Range("P3") = Results
 Dim TotalWins As Range
 Set TotalWins = Range("E3:E6")
 Results = WorksheetFunction.Sum(TotalWins)
 Range("Q3") = Results
 Dim TotalLosses As Range
 Set TotalLosses = Range("F3:F6")
 Results = WorksheetFunction.Sum(TotalLosses)
 Range("R3") = Results
 Set ws = Sheets.Add
 Sheets(1).Activate
 'Call CoachAirForce
 End Sub
Note: to use the sub maintain the format...
Code:
Call YourSubName("joe-golding", 1)
 
Upvote 0
Re: Making this code more efficient please

Thank you ND for this...really appreciate the time...and the solution

I was unfortunately unable to get this to run but I will take more time with it on the weekend. This is greatly appreciated.
 
Upvote 0
Re: Making this code more efficient please

It occurs to me that the URL line was wrong. Trial....
Code:
Url = "https://www.sports-reference.com/cbb/coaches/" & _
                          Coachname & "-" & CStr(TableNo) & ".html"
HTH. Dave
 
Upvote 0

Forum statistics

Threads
1,214,785
Messages
6,121,543
Members
449,038
Latest member
Guest1337

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top