Excel VBA – Iterating through range, creating new sheet, error when creating new sheet

noob here.

Sub CreateNewWorksheet()
‘Go to the open column, reformat the information in column C
‘Copy and paste that information into new column
‘Make new sheets based off a list made of unique entries
Dim ws As Worksheet
Dim lastRow As Long
Dim rng As Range
‘ Set the worksheet
Set ws = Worksheets(“CopiedSheet”)
‘ Determine the last row in column A
lastRow = ws.Cells(ws.Rows.Count, “A”).End(xlUp).Row
‘ Set the range from A1 to the last row in column A
Set rng = ws.Range(“D1:D” & lastRow)
‘ Enter the formula into the range; this will display each date by month
rng.Formula = “=TEXT(RC[-1], “”mmmm””)” ‘ Pastes formula into cells

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>Dim Branchfield As Range 'Creates a variable named Branchfield and sets it to Range
Dim BranchName As Range 'BranchName is the placeholder variable for the value inside the cell
Dim WSheetFound As Boolean 'True/False Variable based on whether or not a sheet is found
Dim NewWSheet As Worksheet
Dim WSheet As Worksheet
Dim DataWSheet As Worksheet 'The Worksheet that has our original data
'Dim String
Set DataWSheet = Worksheets("CopiedSheet") 'Sets all the information in CopiedSheet to the variable DataWSheet
Set Branchfield = DataWSheet.Range("D2", DataWSheet.Range("D2").End(xlDown)) 'Holds the months
For Each BranchName In Branchfield
'Check whether the current branch name corresponds with an existing sheet
For Each WSheet In ActiveWorkbook.Worksheets
'MsgBox WSheet.Name
If WSheet.Name = BranchName Then
WSheetFound = True
BranchName.Offset(0, -3).Resize(1, 13).Copy Destination:=WSheet.Range("A1").End(xlDown).Offset(1, 0)
Exit For ' If it does assign True to the WSheet Found variable
Else
WSheetFound = False 'if it doesn't assign False to the WSheet
End If
Next WSheet
If WSheetFound = True Then 'if WSheetFound = True
Else 'if WSheetFound = False
Set NewWSheet = Sheets.Add(After:=ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count)) 'insert new worksheet
NewWSheet.Name = BranchName 'named after that branch
DataWSheet.Range("A1", DataWSheet.Range("A1").End(xlToRight)).Copy Destination:=NewWSheet.Range("A1") 'and copy the headings to it
BranchName.Offset(0, -3).Resize(1, 13).Copy Destination:=NewWSheet.Range("A2") ' then copy and paste the record to i
End If
' Next WSheet
Next BranchName
</code>
<code>Dim Branchfield As Range 'Creates a variable named Branchfield and sets it to Range Dim BranchName As Range 'BranchName is the placeholder variable for the value inside the cell Dim WSheetFound As Boolean 'True/False Variable based on whether or not a sheet is found Dim NewWSheet As Worksheet Dim WSheet As Worksheet Dim DataWSheet As Worksheet 'The Worksheet that has our original data 'Dim String Set DataWSheet = Worksheets("CopiedSheet") 'Sets all the information in CopiedSheet to the variable DataWSheet Set Branchfield = DataWSheet.Range("D2", DataWSheet.Range("D2").End(xlDown)) 'Holds the months For Each BranchName In Branchfield 'Check whether the current branch name corresponds with an existing sheet For Each WSheet In ActiveWorkbook.Worksheets 'MsgBox WSheet.Name If WSheet.Name = BranchName Then WSheetFound = True BranchName.Offset(0, -3).Resize(1, 13).Copy Destination:=WSheet.Range("A1").End(xlDown).Offset(1, 0) Exit For ' If it does assign True to the WSheet Found variable Else WSheetFound = False 'if it doesn't assign False to the WSheet End If Next WSheet If WSheetFound = True Then 'if WSheetFound = True Else 'if WSheetFound = False Set NewWSheet = Sheets.Add(After:=ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count)) 'insert new worksheet NewWSheet.Name = BranchName 'named after that branch DataWSheet.Range("A1", DataWSheet.Range("A1").End(xlToRight)).Copy Destination:=NewWSheet.Range("A1") 'and copy the headings to it BranchName.Offset(0, -3).Resize(1, 13).Copy Destination:=NewWSheet.Range("A2") ' then copy and paste the record to i End If ' Next WSheet Next BranchName </code>
Dim Branchfield As Range 'Creates a variable named Branchfield and sets it to Range
Dim BranchName As Range 'BranchName is the placeholder variable for the value inside the cell
Dim WSheetFound As Boolean 'True/False Variable based on whether or not a sheet is found

Dim NewWSheet As Worksheet
Dim WSheet As Worksheet
Dim DataWSheet As Worksheet 'The Worksheet that has our original data
'Dim String

Set DataWSheet = Worksheets("CopiedSheet") 'Sets all the information in CopiedSheet to the variable DataWSheet
Set Branchfield = DataWSheet.Range("D2", DataWSheet.Range("D2").End(xlDown)) 'Holds the months

For Each BranchName In Branchfield
'Check whether the current branch name corresponds with an existing sheet
    For Each WSheet In ActiveWorkbook.Worksheets
        'MsgBox WSheet.Name
        If WSheet.Name = BranchName Then
            WSheetFound = True
            BranchName.Offset(0, -3).Resize(1, 13).Copy Destination:=WSheet.Range("A1").End(xlDown).Offset(1, 0)
            Exit For ' If it does assign True to the WSheet Found variable
        Else
            WSheetFound = False 'if it doesn't assign False to the WSheet
        End If
    Next WSheet

    If WSheetFound = True Then 'if WSheetFound = True
        
    Else 'if WSheetFound = False
        Set NewWSheet = Sheets.Add(After:=ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count)) 'insert new worksheet
        NewWSheet.Name = BranchName 'named after that branch
        DataWSheet.Range("A1", DataWSheet.Range("A1").End(xlToRight)).Copy Destination:=NewWSheet.Range("A1") 'and copy the headings to it
        BranchName.Offset(0, -3).Resize(1, 13).Copy Destination:=NewWSheet.Range("A2") ' then copy and paste the record to i
    
    End If
 '   Next WSheet
Next BranchName
    

End Sub

My code iterates through a range, but when it gets to the 20th (or so) value, it tries to create a new sheet. Excel realizes that there is already a sheet with that name, and the code breaks. I’m totally lost and don’t know why this is, please help!

Output

New contributor

marco moriarty is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật