Auto Run a macro in Excel

Copper Contributor

Hi 

 Can anybody help me, I have a macro that I run from my personal macro workbook that opens up about 10 different spreadsheets copies data and pastes it into my macro enabled workbook, works fine but I want to automate it.

 

I have created a new macro enabled worksheet setup a autorun macro and copied the code from my old macro below it, the issue I seem to be having is it does not seem to select my main worksheet (once its has opened it) as the active one so errors when the code tries paste copied data from another workbook into it.

 

I have inserted a snipped of the code below and made the text bold where it is bombing out, 

 

Private Sub Workbook_Open()
 
' Macro1 Macro
'
   Application.DisplayAlerts = False
    Workbooks.Open Filename:="U:\MonthEnd\Exeter\customer_balances_credit.xlsx"
    Range("A1").Select
    Range(Selection, Selection.End(xlToRight)).Select
    Range(Selection, Selection.End(xlDown)).Select
    Selection.Copy
        ChDir "C:\MonthEnd\Exeter"
    Workbooks.Open Filename:="C:\MonthEnd\Exeter\Day_End1.xlsx"
   
    ActiveWindow.ScrollWorkbookTabs Sheets:=1
    ActiveWindow.ScrollWorkbookTabs Sheets:=1
    ActiveWindow.ScrollWorkbookTabs Sheets:=1
    ActiveWindow.ScrollWorkbookTabs Sheets:=1
    Sheets("Customer Credit").Select
    Range("A2").Select
    ActiveSheet.Paste
    Windows("customer_balances_credit.xlsx").Activate
    ActiveWindow.Close
    Workbooks.Open Filename:="U:\MonthEnd\Exeter\customer_balances_debit.xlsx"
    Range("A1").Select
    Range(Selection, Selection.End(xlToRight)).Select
    Range(Selection, Selection.End(xlDown)).Select
1 Reply

@eposlink There is no need to select things in VBA. This bit:

Sheets("Customer Credit").Select
    Range("A2").Select
    ActiveSheet.Paste

can be replaced with:

ThisWorkbook.Sheets("Customer Credit").Range("A2").Paste

ThisWorkbook is a predefined variable which means "The workbook which contains the code that currently runs"