Answered step by step
Verified Expert Solution
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 WorksheetsCopiedSheet
Determine the last row in column A
lastRow wsCellswsRows.Count, AEndxlUpRow
Set the range from A to the last row in column A
Set rng wsRangeD:D & lastRow
Enter the formula into the range; this will display each date by month
rngFormula TEXTRCmmmm 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 'TrueFalse 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 WorksheetsCopiedSheet 'Sets all the information in CopiedSheet to the variable DataWSheet
Set Branchfield DataWSheet.RangeD DataWSheet.RangeDEndxlDown '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.OffsetResizeCopy Destination:WSheet.RangeAEndxlDownOffset
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.AddAfter:ThisWorkbook.SheetsThisWorkbookSheets.Count 'insert new worksheet
NewWSheet.Name BranchName 'named after that branch
DataWSheet.RangeA DataWSheet.RangeAEndxlToRightCopy Destination:NewWSheet.RangeA 'and copy the headings to it
BranchName.OffsetResizeCopy Destination:NewWSheet.RangeA 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
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started