VBA to Activate Worksheet

Small Paul

Board Regular
Joined
Jun 28, 2018
Messages
118
Hi

There is bound to be a simple solution to this as I am probably doing something very basic wrong!

I have a workbook with 1 worksheet from a Salesforce report download (so random name - "report***************"). I am making some changes on the sheet and then copying it to a new sheet titled "Required Data". From here I will be carrying out various manipulations.

The first part of my script is:

Code:
    Columns("A:A").Select    Selection.Insert Shift:=xlToRight
    Selection.Insert Shift:=xlToRight
    Range("A1").Select
    ActiveCell.FormulaR1C1 = "Worksheet Identifier: IFA Group, Account Name, Currency"
    Range("A2").Select
    ActiveCell.FormulaR1C1 = "1"
    Range("A3").Select
    Dim lastrow As Long
lastrow = Cells(Rows.Count, "N").End(xlUp).Row
Range("A3:A" & lastrow).FormulaR1C1 = "=IF(AND(RC[30]<>""None"",RC[30]=R[-1]C[30],RC[32]=R[-1]C[32]),R[-1]C,IF(AND(RC[30]=""None"",RC[31]=R[-1]C[31],RC[32]=R[-1]C[32]),R[-1]C,R[-1]C+1))"


    Range("B2").Select
    ActiveCell.FormulaR1C1 = "1"
    Range("B3").Select
    Range("B3:B" & lastrow).FormulaR1C1 = "=IF(C[-1]<>R[-1]C[-1],1,IF(C[10]=R[-1]C[10],R[-1],R[-1]+1))"
    


    Range("B:B").Copy
    Range("B:B").PasteSpecial Paste:=xlPasteValues
    
    
    Range("B1").Select
    ActiveCell.FormulaR1C1 = "Worksheet Identifier: Investment Product"


    Range("A4").Select
    Range("A3:A500").Copy
    Range("A3:A500").PasteSpecial xlPasteValues
    Columns("A:A").Select
    Selection.TextToColumns
    CopySheetAndRename = "Required Data"
    
    
    'THIS IS WHERE IT GOES WRONG!!!!
    Worksheets(2).Activate
    
    
    Columns("J:J").Select
    Selection.Insert Shift:=xlToRight
    Range("J:J").Value = Range("M:M").Value
    
    Columns(13).EntireColumn.delete

The PROBLEM is the final part - the script falls over at the "Worksheets(2).Activate" stage. I have tried doing it by naming the sheet in this statement, using "select" rather than "activate", using "with" etc.

As I say, it is probably something basic so if anybody can help it would be appreciated.

Many thanks
Small Paul.
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
It simply stops the script and asks me to debug. The entire script runs fine without this small section
 
Upvote 0
When you get that debug pop up it will also have a message eg Runtime error 9 Subscript out of Range.
 
Upvote 0

Forum statistics

Threads
1,223,164
Messages
6,170,444
Members
452,326
Latest member
johnshaji

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