Answered step by step
Verified Expert Solution
Link Copied!
Question
1 Approved Answer

Sub CreateNewWorksheet ( ) ' Go to the open column, reformat the information in column C 'Copy and paste that information into new column 'Make

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
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
''''
This code runs perfectly, but then it tries to create a new sheet that already exists, and fails
image text in transcribed

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image
Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

PC Magazine Guide To Client Server Databases

Authors: Joe Salemi

1st Edition

156276070X, 978-1562760700

More Books

Students explore these related Databases questions