Hi all,
I have a worksheet with cell values A4-30.
I want my VBA script to create a new worksheet for every one of these values that is titled after the value, then copies a range of A1:G60 from a worksheet called Default, and that puts the value also into cell B2 of each new worksheet. I want these new sheets to appear on the right (towards the end of the workbook).
Right now I am trying the two modules below. The worksheet titling is working fine but new sheets are being created on the left, and the B2 formula (=TRIM(MID(@CELL("filename"),FIND("]",@CELL("filename",A1))+1,255))) doesnt' appear to be updating as the cells are copied.
'Name macro
Sub CreateSheets()
'Dimension variables and declare data types
Dim rng As Range
Dim cell As Range
'Enable error handling
On Error GoTo Errorhandling
'Show inputbox to user and prompt for a cell range
Set rng = Application.InputBox(Prompt:="Select cell range:", _
Title:="Create sheets", _
Default:=Selection.Address, Type:=8)
'Iterate through cells in selected cell range
For Each cell In rng
'Check if cell is not empty
If cell <> "" Then
'Insert worksheet and name the worksheet based on cell value
Sheets.Add.Name = cell
End If
'Continue with next cell in cell range
Next cell
'Go here if an error occurs
Errorhandling:
'Stop macro
End Sub
Sub CopyData()
Worksheets("Default").Range("A1:J60").Copy
ActiveSheet.Range("A1:J60").PasteSpecial Paste:=xlPasteFormulas
ActiveSheet.Range("A1:J60").PasteSpecial xlPasteColumnWidths
ActiveSheet.Range("A1:J60").PasteSpecial Paste:=xlPasteFormats
End Sub
I have a worksheet with cell values A4-30.
I want my VBA script to create a new worksheet for every one of these values that is titled after the value, then copies a range of A1:G60 from a worksheet called Default, and that puts the value also into cell B2 of each new worksheet. I want these new sheets to appear on the right (towards the end of the workbook).
Right now I am trying the two modules below. The worksheet titling is working fine but new sheets are being created on the left, and the B2 formula (=TRIM(MID(@CELL("filename"),FIND("]",@CELL("filename",A1))+1,255))) doesnt' appear to be updating as the cells are copied.
'Name macro
Sub CreateSheets()
'Dimension variables and declare data types
Dim rng As Range
Dim cell As Range
'Enable error handling
On Error GoTo Errorhandling
'Show inputbox to user and prompt for a cell range
Set rng = Application.InputBox(Prompt:="Select cell range:", _
Title:="Create sheets", _
Default:=Selection.Address, Type:=8)
'Iterate through cells in selected cell range
For Each cell In rng
'Check if cell is not empty
If cell <> "" Then
'Insert worksheet and name the worksheet based on cell value
Sheets.Add.Name = cell
End If
'Continue with next cell in cell range
Next cell
'Go here if an error occurs
Errorhandling:
'Stop macro
End Sub
Sub CopyData()
Worksheets("Default").Range("A1:J60").Copy
ActiveSheet.Range("A1:J60").PasteSpecial Paste:=xlPasteFormulas
ActiveSheet.Range("A1:J60").PasteSpecial xlPasteColumnWidths
ActiveSheet.Range("A1:J60").PasteSpecial Paste:=xlPasteFormats
End Sub