VBA - Copying and Pasting new workbook to existing sheet

jschell10

New Member
Joined
Apr 19, 2018
Messages
1
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.

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:

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.

Forum statistics

Threads
1,223,888
Messages
6,175,207
Members
452,618
Latest member
Tam84

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