Excel/VBA - Loop through list of URLs (29 .xls files) to open and paste data into 1 single sheet












1















I have a sheet with 3 columns: URL - Target Sheet - Row



The URLs direct you to a .XLS file download automatically (example below):



http://www.eurexrepo.com/blob/157370/ccb5c831da64925cfc15aff4c5e4be85/data/euro_gjpbasket.xls



What I would like to do is to have a VBA code that loops through the rows (B2:B30) that contain the 29 links (please see image) and copy/pastes the data into a unique target sheet (specified in the second column C2:C30) in a predefined row number of the target sheet (specified in 3rd column: D2:D30).



List of URLs in cells



This is the code that I am trying to use:



Sub Import_Baskets()

Dim Rows As Long, links As Variant, link As Variant
Rows = Sheets("Admin").Cells(Sheets("Admin").Rows.Count, "B").End(xlUp).Row
links = Sheets("Admin").Range("B1:B" & Rows)

For Each link In links

Dim wkbMyWorkbook As Workbook
Dim wkbWebWorkbook As Workbook
Dim wksWebWorkSheet As Worksheet

Set wkbMyWorkbook = ActiveWorkbook

' *************************************************
' Open The Web Workbook
' *************************************************

' *************************************************
' Set the Web Workbook and Worksheet Variables
' *************************************************
Set wkbWebWorkbook = ActiveWorkbook
Set wksWebWorkSheet = ActiveSheet

' *************************************************
' Copy The Web Worksheet To My Workbook and Rename
' *************************************************
wksWebWorkSheet.Copy After:=wkbMyWorkbook.Sheets(Sheets.Count)
wkbMyWorkbook.Sheets(ActiveSheet.Name).Name = "GC"

'.Range("$A$" + row_number)
' *************************************************
' Close the Web Workbook
' *************************************************
wkbMyWorkbook.Activate
wkbWebWorkbook.Close


Next link

'Next


End Sub


Any help will be much appreciated! :)










share|improve this question

























  • what is the problem with your code?

    – Kubie
    Nov 21 '18 at 15:01











  • It doesn't recognize the links in the rows (that's where I get the problem when debugging)

    – Jason
    Nov 21 '18 at 15:09
















1















I have a sheet with 3 columns: URL - Target Sheet - Row



The URLs direct you to a .XLS file download automatically (example below):



http://www.eurexrepo.com/blob/157370/ccb5c831da64925cfc15aff4c5e4be85/data/euro_gjpbasket.xls



What I would like to do is to have a VBA code that loops through the rows (B2:B30) that contain the 29 links (please see image) and copy/pastes the data into a unique target sheet (specified in the second column C2:C30) in a predefined row number of the target sheet (specified in 3rd column: D2:D30).



List of URLs in cells



This is the code that I am trying to use:



Sub Import_Baskets()

Dim Rows As Long, links As Variant, link As Variant
Rows = Sheets("Admin").Cells(Sheets("Admin").Rows.Count, "B").End(xlUp).Row
links = Sheets("Admin").Range("B1:B" & Rows)

For Each link In links

Dim wkbMyWorkbook As Workbook
Dim wkbWebWorkbook As Workbook
Dim wksWebWorkSheet As Worksheet

Set wkbMyWorkbook = ActiveWorkbook

' *************************************************
' Open The Web Workbook
' *************************************************

' *************************************************
' Set the Web Workbook and Worksheet Variables
' *************************************************
Set wkbWebWorkbook = ActiveWorkbook
Set wksWebWorkSheet = ActiveSheet

' *************************************************
' Copy The Web Worksheet To My Workbook and Rename
' *************************************************
wksWebWorkSheet.Copy After:=wkbMyWorkbook.Sheets(Sheets.Count)
wkbMyWorkbook.Sheets(ActiveSheet.Name).Name = "GC"

'.Range("$A$" + row_number)
' *************************************************
' Close the Web Workbook
' *************************************************
wkbMyWorkbook.Activate
wkbWebWorkbook.Close


Next link

'Next


End Sub


Any help will be much appreciated! :)










share|improve this question

























  • what is the problem with your code?

    – Kubie
    Nov 21 '18 at 15:01











  • It doesn't recognize the links in the rows (that's where I get the problem when debugging)

    – Jason
    Nov 21 '18 at 15:09














1












1








1








I have a sheet with 3 columns: URL - Target Sheet - Row



The URLs direct you to a .XLS file download automatically (example below):



http://www.eurexrepo.com/blob/157370/ccb5c831da64925cfc15aff4c5e4be85/data/euro_gjpbasket.xls



What I would like to do is to have a VBA code that loops through the rows (B2:B30) that contain the 29 links (please see image) and copy/pastes the data into a unique target sheet (specified in the second column C2:C30) in a predefined row number of the target sheet (specified in 3rd column: D2:D30).



List of URLs in cells



This is the code that I am trying to use:



Sub Import_Baskets()

Dim Rows As Long, links As Variant, link As Variant
Rows = Sheets("Admin").Cells(Sheets("Admin").Rows.Count, "B").End(xlUp).Row
links = Sheets("Admin").Range("B1:B" & Rows)

For Each link In links

Dim wkbMyWorkbook As Workbook
Dim wkbWebWorkbook As Workbook
Dim wksWebWorkSheet As Worksheet

Set wkbMyWorkbook = ActiveWorkbook

' *************************************************
' Open The Web Workbook
' *************************************************

' *************************************************
' Set the Web Workbook and Worksheet Variables
' *************************************************
Set wkbWebWorkbook = ActiveWorkbook
Set wksWebWorkSheet = ActiveSheet

' *************************************************
' Copy The Web Worksheet To My Workbook and Rename
' *************************************************
wksWebWorkSheet.Copy After:=wkbMyWorkbook.Sheets(Sheets.Count)
wkbMyWorkbook.Sheets(ActiveSheet.Name).Name = "GC"

'.Range("$A$" + row_number)
' *************************************************
' Close the Web Workbook
' *************************************************
wkbMyWorkbook.Activate
wkbWebWorkbook.Close


Next link

'Next


End Sub


Any help will be much appreciated! :)










share|improve this question
















I have a sheet with 3 columns: URL - Target Sheet - Row



The URLs direct you to a .XLS file download automatically (example below):



http://www.eurexrepo.com/blob/157370/ccb5c831da64925cfc15aff4c5e4be85/data/euro_gjpbasket.xls



What I would like to do is to have a VBA code that loops through the rows (B2:B30) that contain the 29 links (please see image) and copy/pastes the data into a unique target sheet (specified in the second column C2:C30) in a predefined row number of the target sheet (specified in 3rd column: D2:D30).



List of URLs in cells



This is the code that I am trying to use:



Sub Import_Baskets()

Dim Rows As Long, links As Variant, link As Variant
Rows = Sheets("Admin").Cells(Sheets("Admin").Rows.Count, "B").End(xlUp).Row
links = Sheets("Admin").Range("B1:B" & Rows)

For Each link In links

Dim wkbMyWorkbook As Workbook
Dim wkbWebWorkbook As Workbook
Dim wksWebWorkSheet As Worksheet

Set wkbMyWorkbook = ActiveWorkbook

' *************************************************
' Open The Web Workbook
' *************************************************

' *************************************************
' Set the Web Workbook and Worksheet Variables
' *************************************************
Set wkbWebWorkbook = ActiveWorkbook
Set wksWebWorkSheet = ActiveSheet

' *************************************************
' Copy The Web Worksheet To My Workbook and Rename
' *************************************************
wksWebWorkSheet.Copy After:=wkbMyWorkbook.Sheets(Sheets.Count)
wkbMyWorkbook.Sheets(ActiveSheet.Name).Name = "GC"

'.Range("$A$" + row_number)
' *************************************************
' Close the Web Workbook
' *************************************************
wkbMyWorkbook.Activate
wkbWebWorkbook.Close


Next link

'Next


End Sub


Any help will be much appreciated! :)







excel vba excel-vba web url






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 21 '18 at 16:53









Kubie

1,3671619




1,3671619










asked Nov 21 '18 at 14:52









JasonJason

102




102













  • what is the problem with your code?

    – Kubie
    Nov 21 '18 at 15:01











  • It doesn't recognize the links in the rows (that's where I get the problem when debugging)

    – Jason
    Nov 21 '18 at 15:09



















  • what is the problem with your code?

    – Kubie
    Nov 21 '18 at 15:01











  • It doesn't recognize the links in the rows (that's where I get the problem when debugging)

    – Jason
    Nov 21 '18 at 15:09

















what is the problem with your code?

– Kubie
Nov 21 '18 at 15:01





what is the problem with your code?

– Kubie
Nov 21 '18 at 15:01













It doesn't recognize the links in the rows (that's where I get the problem when debugging)

– Jason
Nov 21 '18 at 15:09





It doesn't recognize the links in the rows (that's where I get the problem when debugging)

– Jason
Nov 21 '18 at 15:09












1 Answer
1






active

oldest

votes


















0














This could work for your situation. Haven't tested it though so please save your workbook before trying.



Sub Import_Baskets()

Dim lastRow As Long, link As String
Dim targetSheet As Worksheet
Dim webBook As Workbook
Dim webSheet As Worksheet
Dim shName As String
Dim stRow As Long

With ThisWorkbook.Worksheets("Admin")

lastRow = .Cells(.Rows.Count, "B").End(xlUp).Row

' set your start and last row
For row = 2 To lastRow

link = Trim(CStr(.Range("B" & row).Value))
shName = Trim(CStr(.Range("C" & row).Value))
stRow = .Range("D" & row).Value

' open web wb
Set webBook = Workbooks.Open(link)

' set web ws
Set webSheet = webBook.Worksheets("enter desired sheet name")

' copy & paste
On Error GoTo CreateSheet
Set targetSheet = ThisWorkbook.Worksheets(shName)
On Error GoTo 0
webSheet.UsedRange.Copy destination:=targetSheet.Range("A" & stRow)

' close web wb
webBook.Close
.Activate
Next
End With
Exit Sub

' if sheet doesn't exist, insert it
CreateSheet:
With ThisWorkbook
.Sheets.Add(After:=.Sheets(.Sheets.Count)).Name = shName
End With
Resume Next

End Sub





share|improve this answer





















  • 1





    It works perfectly. Thank you very much!!

    – Jason
    Nov 21 '18 at 18:13











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53414690%2fexcel-vba-loop-through-list-of-urls-29-xls-files-to-open-and-paste-data-int%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














This could work for your situation. Haven't tested it though so please save your workbook before trying.



Sub Import_Baskets()

Dim lastRow As Long, link As String
Dim targetSheet As Worksheet
Dim webBook As Workbook
Dim webSheet As Worksheet
Dim shName As String
Dim stRow As Long

With ThisWorkbook.Worksheets("Admin")

lastRow = .Cells(.Rows.Count, "B").End(xlUp).Row

' set your start and last row
For row = 2 To lastRow

link = Trim(CStr(.Range("B" & row).Value))
shName = Trim(CStr(.Range("C" & row).Value))
stRow = .Range("D" & row).Value

' open web wb
Set webBook = Workbooks.Open(link)

' set web ws
Set webSheet = webBook.Worksheets("enter desired sheet name")

' copy & paste
On Error GoTo CreateSheet
Set targetSheet = ThisWorkbook.Worksheets(shName)
On Error GoTo 0
webSheet.UsedRange.Copy destination:=targetSheet.Range("A" & stRow)

' close web wb
webBook.Close
.Activate
Next
End With
Exit Sub

' if sheet doesn't exist, insert it
CreateSheet:
With ThisWorkbook
.Sheets.Add(After:=.Sheets(.Sheets.Count)).Name = shName
End With
Resume Next

End Sub





share|improve this answer





















  • 1





    It works perfectly. Thank you very much!!

    – Jason
    Nov 21 '18 at 18:13
















0














This could work for your situation. Haven't tested it though so please save your workbook before trying.



Sub Import_Baskets()

Dim lastRow As Long, link As String
Dim targetSheet As Worksheet
Dim webBook As Workbook
Dim webSheet As Worksheet
Dim shName As String
Dim stRow As Long

With ThisWorkbook.Worksheets("Admin")

lastRow = .Cells(.Rows.Count, "B").End(xlUp).Row

' set your start and last row
For row = 2 To lastRow

link = Trim(CStr(.Range("B" & row).Value))
shName = Trim(CStr(.Range("C" & row).Value))
stRow = .Range("D" & row).Value

' open web wb
Set webBook = Workbooks.Open(link)

' set web ws
Set webSheet = webBook.Worksheets("enter desired sheet name")

' copy & paste
On Error GoTo CreateSheet
Set targetSheet = ThisWorkbook.Worksheets(shName)
On Error GoTo 0
webSheet.UsedRange.Copy destination:=targetSheet.Range("A" & stRow)

' close web wb
webBook.Close
.Activate
Next
End With
Exit Sub

' if sheet doesn't exist, insert it
CreateSheet:
With ThisWorkbook
.Sheets.Add(After:=.Sheets(.Sheets.Count)).Name = shName
End With
Resume Next

End Sub





share|improve this answer





















  • 1





    It works perfectly. Thank you very much!!

    – Jason
    Nov 21 '18 at 18:13














0












0








0







This could work for your situation. Haven't tested it though so please save your workbook before trying.



Sub Import_Baskets()

Dim lastRow As Long, link As String
Dim targetSheet As Worksheet
Dim webBook As Workbook
Dim webSheet As Worksheet
Dim shName As String
Dim stRow As Long

With ThisWorkbook.Worksheets("Admin")

lastRow = .Cells(.Rows.Count, "B").End(xlUp).Row

' set your start and last row
For row = 2 To lastRow

link = Trim(CStr(.Range("B" & row).Value))
shName = Trim(CStr(.Range("C" & row).Value))
stRow = .Range("D" & row).Value

' open web wb
Set webBook = Workbooks.Open(link)

' set web ws
Set webSheet = webBook.Worksheets("enter desired sheet name")

' copy & paste
On Error GoTo CreateSheet
Set targetSheet = ThisWorkbook.Worksheets(shName)
On Error GoTo 0
webSheet.UsedRange.Copy destination:=targetSheet.Range("A" & stRow)

' close web wb
webBook.Close
.Activate
Next
End With
Exit Sub

' if sheet doesn't exist, insert it
CreateSheet:
With ThisWorkbook
.Sheets.Add(After:=.Sheets(.Sheets.Count)).Name = shName
End With
Resume Next

End Sub





share|improve this answer















This could work for your situation. Haven't tested it though so please save your workbook before trying.



Sub Import_Baskets()

Dim lastRow As Long, link As String
Dim targetSheet As Worksheet
Dim webBook As Workbook
Dim webSheet As Worksheet
Dim shName As String
Dim stRow As Long

With ThisWorkbook.Worksheets("Admin")

lastRow = .Cells(.Rows.Count, "B").End(xlUp).Row

' set your start and last row
For row = 2 To lastRow

link = Trim(CStr(.Range("B" & row).Value))
shName = Trim(CStr(.Range("C" & row).Value))
stRow = .Range("D" & row).Value

' open web wb
Set webBook = Workbooks.Open(link)

' set web ws
Set webSheet = webBook.Worksheets("enter desired sheet name")

' copy & paste
On Error GoTo CreateSheet
Set targetSheet = ThisWorkbook.Worksheets(shName)
On Error GoTo 0
webSheet.UsedRange.Copy destination:=targetSheet.Range("A" & stRow)

' close web wb
webBook.Close
.Activate
Next
End With
Exit Sub

' if sheet doesn't exist, insert it
CreateSheet:
With ThisWorkbook
.Sheets.Add(After:=.Sheets(.Sheets.Count)).Name = shName
End With
Resume Next

End Sub






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 21 '18 at 16:46

























answered Nov 21 '18 at 15:18









KubieKubie

1,3671619




1,3671619








  • 1





    It works perfectly. Thank you very much!!

    – Jason
    Nov 21 '18 at 18:13














  • 1





    It works perfectly. Thank you very much!!

    – Jason
    Nov 21 '18 at 18:13








1




1





It works perfectly. Thank you very much!!

– Jason
Nov 21 '18 at 18:13





It works perfectly. Thank you very much!!

– Jason
Nov 21 '18 at 18:13




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53414690%2fexcel-vba-loop-through-list-of-urls-29-xls-files-to-open-and-paste-data-int%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

鏡平學校

ꓛꓣだゔៀៅຸ໢ທຮ໕໒ ,ໂ'໥໓າ໼ឨឲ៵៭ៈゎゔit''䖳𥁄卿' ☨₤₨こゎもょの;ꜹꟚꞖꞵꟅꞛေၦေɯ,ɨɡ𛃵𛁹ޝ޳ޠ޾,ޤޒޯ޾𫝒𫠁သ𛅤チョ'サノބޘދ𛁐ᶿᶇᶀᶋᶠ㨑㽹⻮ꧬ꧹؍۩وَؠ㇕㇃㇪ ㇦㇋㇋ṜẰᵡᴠ 軌ᵕ搜۳ٰޗޮ޷ސޯ𫖾𫅀ल, ꙭ꙰ꚅꙁꚊꞻꝔ꟠Ꝭㄤﺟޱސꧨꧼ꧴ꧯꧽ꧲ꧯ'⽹⽭⾁⿞⼳⽋២៩ញណើꩯꩤ꩸ꩮᶻᶺᶧᶂ𫳲𫪭𬸄𫵰𬖩𬫣𬊉ၲ𛅬㕦䬺𫝌𫝼,,𫟖𫞽ហៅ஫㆔ాఆఅꙒꚞꙍ,Ꙟ꙱エ ,ポテ,フࢰࢯ𫟠𫞶 𫝤𫟠ﺕﹱﻜﻣ𪵕𪭸𪻆𪾩𫔷ġ,ŧآꞪ꟥,ꞔꝻ♚☹⛵𛀌ꬷꭞȄƁƪƬșƦǙǗdžƝǯǧⱦⱰꓕꓢႋ神 ဴ၀க௭எ௫ឫោ ' េㇷㇴㇼ神ㇸㇲㇽㇴㇼㇻㇸ'ㇸㇿㇸㇹㇰㆣꓚꓤ₡₧ ㄨㄟ㄂ㄖㄎ໗ツڒذ₶।ऩछएोञयूटक़कयँृी,冬'𛅢𛅥ㇱㇵㇶ𥄥𦒽𠣧𠊓𧢖𥞘𩔋цѰㄠſtʯʭɿʆʗʍʩɷɛ,əʏダヵㄐㄘR{gỚṖḺờṠṫảḙḭᴮᵏᴘᵀᵷᵕᴜᴏᵾq﮲ﲿﴽﭙ軌ﰬﶚﶧ﫲Ҝжюїкӈㇴffצּ﬘﭅﬈軌'ffistfflſtffतभफɳɰʊɲʎ𛁱𛁖𛁮𛀉 𛂯𛀞నఋŀŲ 𫟲𫠖𫞺ຆຆ ໹້໕໗ๆทԊꧢꧠ꧰ꓱ⿝⼑ŎḬẃẖỐẅ ,ờỰỈỗﮊDžȩꭏꭎꬻ꭮ꬿꭖꭥꭅ㇭神 ⾈ꓵꓑ⺄㄄ㄪㄙㄅㄇstA۵䞽ॶ𫞑𫝄㇉㇇゜軌𩜛𩳠Jﻺ‚Üမ႕ႌႊၐၸဓၞၞၡ៸wyvtᶎᶪᶹစဎ꣡꣰꣢꣤ٗ؋لㇳㇾㇻㇱ㆐㆔,,㆟Ⱶヤマފ޼ޝަݿݞݠݷݐ',ݘ,ݪݙݵ𬝉𬜁𫝨𫞘くせぉて¼óû×ó£…𛅑הㄙくԗԀ5606神45,神796'𪤻𫞧ꓐ㄁ㄘɥɺꓵꓲ3''7034׉ⱦⱠˆ“𫝋ȍ,ꩲ軌꩷ꩶꩧꩫఞ۔فڱێظペサ神ナᴦᵑ47 9238їﻂ䐊䔉㠸﬎ffiﬣ,לּᴷᴦᵛᵽ,ᴨᵤ ᵸᵥᴗᵈꚏꚉꚟ⻆rtǟƴ𬎎

Why https connections are so slow when debugging (stepping over) in Java?