YannD

vba open xls from web and import sheets

Jan 5th, 2021 (edited)
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Sub xlsurl()
  2.     ' current workbook
  3.    Dim wb As Workbook
  4.     ' new worksheet to insert data
  5.    Dim ws As Worksheet
  6.     ' number of sheets in current workbook
  7.    Dim i As Integer
  8.     ' remote workbook
  9.    Dim wurl As Workbook
  10.    
  11.     ' let's set some "constants"
  12.    ' xls to download
  13.    Const surl As String = _
  14.         "https://www.ishares.com/uk/professional/en/products/251853/ishares-msci-brazil-ucits-etf-inc-fund/1535604580409.ajax?fileType=xls&fileName=iShares-MSCI-Brazil-UCITS-ETF-USD-Dist_fund&dataType=fund"
  15.     ' list of sheets to import as a string with comas (no spaces!)
  16.    Dim sheetsImport() As String
  17.     sheetsImport = Split("Overview,Historical,Holdings", ",")
  18.    
  19.  
  20.     ' obvious, i hope...
  21.    Set wb = ThisWorkbook
  22.     ' so let's open the remote xls
  23.    Set wurl = Workbooks.Open(surl)
  24.    
  25.     ' ok, now we will loop through all names in sheetImport; it could be only 1 of course
  26.     ' attention: no error handling! so if no sheet exists, etc - the code will crash
  27.     ' ---start of sheets loop---
  28.    Dim tmpsheet As Variant
  29.     For Each tmpsheet In sheetsImport
  30.    
  31.     ' counting sheets in current workbook; this is a safe solution - you can always change it to replace data in the existing sheet
  32.    i = wb.Sheets.Count
  33.     ' add new sheet at the end of existing ones
  34.    Set ws = wb.Sheets.Add(After:=wb.Sheets(i))
  35.    
  36.     ' new sheet name is set to the name of the imported sheet + underscore + current date
  37.    ' safety feature for sheet name:
  38.    ' if sheet with that name already exists - add _I to the date until free name is found
  39.    ' for example:
  40.    ' original_sheetname_2021-01-06 exists
  41.    ' original_sheetname_2021-01-06_1 is created; if exists
  42.    ' original_sheetname_2021-01-06_2 is created; and so on
  43.    Dim nameDate As String
  44.     Dim nameI As Integer
  45.     nameDate = tmpsheet & "_" & Date
  46.     nameI = 0
  47.     Dim f As Boolean
  48.     f = WsExists(nameDate)
  49.     Do While f
  50.         nameI = nameI + 1
  51.         nameDate = tmpsheet & "_" & Date & "_" & nameI
  52.         f = WsExists(nameDate)
  53.     Loop
  54.     ' end of safe name loop
  55.    
  56.     ' set new sheet safe name
  57.    ws.Name = nameDate
  58.  
  59.     ' just for easy reference
  60.    Dim wsheet As Worksheet
  61.     ' set it to the selected sheet from the downloaded XLS
  62.    Set wsheet = wurl.Worksheets(tmpsheet)
  63.     ' and copy its data to the new sheet in the current workbook
  64.    wsheet.Cells.Copy Destination:=ws.Range("A1")
  65.    
  66.     Next tmpsheet
  67.     ' ---end of sheets loop---
  68.  
  69.     ' close downloaded file
  70.    wurl.Close
  71.  
  72. End Sub
  73.  
  74. Function WsExists(sh As String, Optional wb As Workbook) As Boolean
  75.     Dim shtout As Worksheet
  76.  
  77.     If wb Is Nothing Then Set wb = ThisWorkbook
  78.     On Error Resume Next
  79.     Set shtout = wb.Sheets(sh)
  80.     On Error GoTo 0
  81.     WsExists = Not shtout Is Nothing
  82. End Function
Advertisement
Add Comment
Please, Sign In to add comment