VBA goes past range requested

michaelehoudek

New Member
Joined
Jan 15, 2016
Messages
21
I have the following code. It works fine and does what I want, but then is continuing past the list of names. After the last named sheet is created, it throws an error after it creates the next new worksheet, but can't name it as it has created and named the last worksheet I wanted it to. The debug take me to the line bolded below. I'm thinking the error is caused by the Set MyRange line, but don't know what to do. Any ideas?

(code)
Dim MyCell As Range, MyRange As Range


Set MyRange = Selection
Set MyRange = Range(MyRange, MyRange.End(xlDown))
Application.DisplayAlerts = False


For Each MyCell In MyRange

Sheets("BASE").Select
Columns("A:BT").Select
Selection.Copy

Sheets.Add After:=Sheets(Sheets.Count) 'creates a new worksheet
Sheets(Sheets.Count).Name = MyCell.Value ' renames the new worksheet

Selection.PasteSpecial Paste:=xlPasteColumnWidths, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
ActiveSheet.Paste

Range("a2").Value = MyCell.Value
Range("a2").Select

Next MyCell


Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True
Application.DisplayAlerts = True
End Sub
(End code)
 
Last edited:

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
I suspect it's because you set 'MyRange' to Range(MyRange, MyRange.End(xlDown)). Depending on which cell(s) you select before running the code, this is potentially referring to more than 1 million rows. See if the below code helps (I've removed the unecessary selecting of sheets and cells).


Code:
Sub x()
Dim MyCell As Range
Dim MyRange As Range
With Application
    .Calculation = xlCalculationManual
    .ScreenUpdating = False
    .DisplayAlerts = False
End With

Set MyRange = Selection.activeregion
For Each MyCell In MyRange
    Sheets.Add after:=Sheets(Sheets.Count)
    Sheets(Sheets.Count).Name = MyCell.Value
    
    Sheets("Base").Columns("A:BT").Copy
    
    With Sheets(MyCell.Value)
        .PasteSpecial Paste:=xlPasteColumnWidths, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
        .Paste
        .Range("A2") = MyCell.Value
    End With
Next MyCell

With Application
    .Calculation = xlCalculationAutomatic
    .ScreenUpdating = True
    .DisplayAlerts = True
End With

End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,213,490
Messages
6,113,956
Members
448,535
Latest member
alrossman

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