Hello,
I am creating some script in workbook "A" that allows me to open workbook "B" from the internet and paste the contents of workbook "B" into workbook "A". The problem I am running into is workbook "B" is staying open, so when the script reruns it pastes from workbook "C" into workbook "B" instead of workbook "A". Hopefully I have not made this too confusing.
Here is the script in its entirety. Any help is greatly appreciated.
I am creating some script in workbook "A" that allows me to open workbook "B" from the internet and paste the contents of workbook "B" into workbook "A". The problem I am running into is workbook "B" is staying open, so when the script reruns it pastes from workbook "C" into workbook "B" instead of workbook "A". Hopefully I have not made this too confusing.
Here is the script in its entirety. Any help is greatly appreciated.
Code:
Sub IE()
Dim IE As Object
Dim targetURL As String
Dim wb1 As Workbook
Set wb1 = ThisWorkbook
ThisWorkbook.Worksheets(1).Columns("a:as").Clear
targetURL = "www.yyy.com"
Set IE = New InternetExplorerMedium
IE.Visible = True ' Set to true to watch what's happening
IE.Navigate targetURL
' Wait IE
Do While IE.ReadyState < 4 Or IE.Busy: DoEvents: Loop
' Wait document
Do While IE.Document.ReadyState <> "complete": DoEvents: Loop
'Change dropdown to Detail and update
IE.Document.forms("aspnetForm").elements("ctl00_ContentPlaceHolder1_pnlReportForm_ctl05_cmbReportToRun_I").Value = "Detail"
IE.Document.forms("aspnetForm").elements("ctl00_ContentPlaceHolder1_pnlReportForm_ctl05_cmbReportToRun_I").FireEvent ("onchange")
' Wait IE
Do While IE.ReadyState < 4 Or IE.Busy: DoEvents: Loop
' Wait document
Do While IE.Document.ReadyState <> "complete": DoEvents: Loop
'Change Location to Billings and Update
IE.Document.forms("aspnetForm").elements("ctl00_ContentPlaceHolder1_pnlReportForm_ctl05_cmbLocation_I").Value = "CC-Harlingen Call Center"
IE.Document.forms("aspnetForm").elements("ctl00_ContentPlaceHolder1_pnlReportForm_ctl05_cmbLocation_I").FireEvent ("onchange")
'Wait 4 seconds
Application.Wait Now + #12:00:04 AM#
'Change Manager to "All" and update
IE.Document.forms("aspnetForm").elements("ctl00_ContentPlaceHolder1_pnlReportForm_ctl05_cmbManager_I").Value = "ALL"
IE.Document.forms("aspnetForm").elements("ctl00_ContentPlaceHolder1_pnlReportForm_ctl05_cmbManager_I").FireEvent ("onchange")
'Wait 4 seconds
Application.Wait Now + #12:00:04 AM#
'Click Run Report
IE.Document.forms("aspnetForm").elements("ctl00$ContentPlaceHolder1$pnlReportForm$ctl05$btnRunReport").Click
'Wait 15 Seconds
Application.Wait Now + #12:00:15 AM#
'export as CSV
For Each link In IE.Document.getElementsByTagName("a")
If link.Title = "CSV (comma delimited)" Then: link.Click: Exit For
Next
'Wait 5 seconds
Application.Wait Now + #12:00:05 AM#
'Send Alt + O to the active window
AppActivate "Reports: - Internet Explorer"
Application.SendKeys "%{O}", True
'allow workbook to open then fire paste() subroutine
Application.OnTime Now + TimeValue("00:00:05"), "paste"
'Wait 5 seconds
Application.Wait Now + #12:00:05 AM#
IE.Quit
End Sub
Code:
'subroutine to allow workbook to open, then paste data from CSV file into this workbook
Sub paste()
Dim sourceColumn As Range, targetColumn As Range
Dim wb2 As Workbook
For Each wb2 In Workbooks
If wb2.Name Like "*Retention Tracker Dashboard*" Then
Set sourceCols = wb2.Worksheets(1).Columns("a:as")
Set targetCols = ThisWorkbook.Worksheets(1).Columns("a:as")
sourceCols.copy Destination:=targetCols
wb2.Close
End If
Next wb2
End Sub
Last edited by a moderator: