VBA goes past range requested


New Member
Jan 15, 2016
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?

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.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

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

Next MyCell

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


Well-known Member
Jun 17, 2005
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).

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
    With Sheets(MyCell.Value)
        .PasteSpecial Paste:=xlPasteColumnWidths, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
        .Range("A2") = MyCell.Value
    End With
Next MyCell

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

End Sub
Last edited:

Forum statistics

Latest member

Some videos you may like

This Week's Hot Topics

  • VBA (Userform)
    Hi All, I just would like to know why my code isn't working. Here is my VBA code: [CODE=vba]Private Sub OKButton_Click() Dim i As Integer...
  • List box that changes fill color
    Hello, I have gone through so many pages trying to figure this out. I have a 2020 calendar that depending on the day needs to have a certain...
  • Remove duplicates and retain one. Cross-linked cases
    Hi all I ran out of google keywords to use and still couldn't find a reference how to achieve the results of a single count. It would be great if...
  • VBA Copy and Paste With Duplicates
    Hello All, I'm in need of some input. My VBA skills are sub-par at best. I've assembled this code from basic research and it works but is...
  • Macro
    is it possible for a macro to run if the active cell value is different to the value above it
  • IF DATE and TIME
    I currently use this to check if date has passed but i also need to set a time on it too. Is it possible? [CODE=vba]=IF(B:B>TODAY(),"Not...