Excel VBA automated Sheet creater - vba

Im wondering how i could create an Excel VBA that makes new Sheets and takes the names from a column with the range A2:A100 and only creates sheets if these cells arent empty.
My Sheet with the names looks something like this :
Variant | Title | Number
V1 Test1 1.1
V2 Test2 2.1
This means, that new Sheets with the names V1 & V2 should be created.
Furthermore i would want them, if possible, to have the exact same Content
A1 : productnumber A2 : amount
EDIT
Used this code now in Order to create the Sheets :
Sub CreateSheetsFromAList()
Dim MyCell As Range, MyRange As Range
Set MyRange = Sheets("Summary").Range("A10")
Set MyRange = Range(MyRange, MyRange.End(xlDown))
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
Next MyCell
End Sub
Changed "Summary" to my Sheets Name and adjusted the Range. As mentioned, i want the file to create Sheets based on the cell contents of A2:A100 only if the cell contains anything at all. Furthermore i am still not able to set the Contents of the new Sheets that are created

You have the basic method nailed, just added a check that the cell contains something and used a worksheet variable for the new sheet which is easier to use if you need to do things to it.
I don't know what you mean when you say you are "not able to set the Contents of the new Sheets"?
Sub CreateSheetsFromAList()
Dim MyCell As Range, MyRange As Range, ws As Worksheet
Set MyRange = Sheets("Summary").Range("A2:A100")
For Each MyCell In MyRange
If Len(MyCell) > 0 Then
Set ws = Worksheets.Add(After:=Worksheets(Worksheets.Count)) 'creates a new worksheet
ws.Name = MyCell.Value ' renames the new worksheet
ws.Range("A1:D1").Value = Array("Artikelnummer", "Menge pro Schrank", "Anzahl Schränke", "Menge Gesamt")
End If
Next MyCell
End Sub

Related

Create a sheets using list of values and update values from the table?

I am facing trouble while developing code for creating multiple sheets and the sheet names are taken from the list in other sheet and then in the created sheets, there are some fields needs to be updated from the table. I mean first created sheet, value to copied from main sheet cell A1 (for example), and next created sheet, value to copied from main sheet cell A2 and goes till end.
I developed code until its successfully copies it but I have no idea how to replace cell values from the table.
I dont knw how much challenging it is but any help would be greatly appreciated.
Thanks
Sarath
My current code
Sub create_sheets()
Dim MyCell As Range, MyRange As Range
Set MyRange = Sheets("input data").Range("C6")
Set MyRange = Range(MyRange, MyRange.End(xlToRight))
For Each MyCell In MyRange
Sheets("200L_50°C").Copy After:=Sheets(Sheets.Count) 'creates a new worksheet
Sheets(Sheets.Count).Name = MyCell.Value ' renames the new worksheet
Next MyCell
End Sub
First, this
Set MyRange = Sheets("input data").Range("C6")
Set MyRange = Range(MyRange, MyRange.End(xlToRight))
can be done in one line:
Set MyRange = Range(Cells(6, 3), Cells(6, 3).End(xlToRight))
Also consider more robust version:
Set MyRange = Range(Cells(6, 3), Cells(6, Columns.Count).End(xlToLeft))
Having settled this, to create sheets I would rather use .Add method:
Dim i As Long: i = 1
For Each MyCell in MyRange
Sheets.Add After:=Sheets(Sheets.Count)
Sheets(Sheets.Count).Name = MyCell.Value
'copy the value from main Worksheet - from A1, A2, etc.
'to A1 in new worksheet - it remains first sheet I assume
Sheets(Sheets.Count).Cells(1, 1).Value = Sheets(1).Cells(i, 1).Value
i = i + 1
Next MyCell
Also, using Sheet(1) is unnecessary, since we don't activate or select any other sheet.

How do I paste data into new worksheets, and delete certain rows based on cell from another table?

I have data in an Excel spreadsheet called "Master" of about 1,000 rows I am trying to split into 42 different spreadsheets based on an identifier called the Region ID. I have created a table with the 42 unique different regions in another spreadsheet in the same workbook, along with what I want to name the new worksheets. I have the following code so far, which creates the 42 new spreadsheets and names them accordingly.
Sub Create_Tabs()
Dim MyCell As Range, MyRange As Range
Set MyRange = Sheets("TabList").Range("D2")
Set MyRange = Range(MyRange, MyRange.End(xlDown))
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
'I would like to create code here to paste the "Master" spreadsheet contents in the newly created worksheet and keep only rows corresponding Region IDs (delete the rest).
Next MyCell
End Sub
How do I paste the "Master" spreadsheet contents in the newly created worksheet and keep only rows corresponding Region IDs (delete the rest)?
Filter your master data on tab names and copy them over to the new sheet.
Something like this will work. You might need to change your filter field based on your data.
Sub Create_Tabs_And_Copy_Data()
Dim MyCell As Range, MyRange As Range
Set MyRange = Sheets("TabList").Range("D2")
Set MyRange = Range(MyRange, MyRange.End(xlDown))
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
'I would like to create code here to paste the "Master" spreadsheet contents in the newly created worksheet and keep only rows corresponding Region IDs (delete the rest).
With Sheets("Master").UsedRange
.AutoFilter
.AutoFilter Field:=1, Criteria1:=MyCell.Value
.SpecialCells(xlCellTypeVisible).Copy Sheets(MyCell.Value).Cells(1, 1)
Application.CutCopyMode = False
End With
Next MyCell
End Sub

VBA - Copy template sheets to multiple sheets of another workbook if criteria met

I've been trying to get the code to work for the past week with no luck. I tried various modifications, which ends up giving different error codes.
The first Error I was getting was with Set rng = Intersect(.UsedRange, .Columns(2))
Object doesn’t support this property or method
So then I changed this to just going through the entire column just to see if it would work : Set rng = Range("B:B"), when I do that then it reads through and I get an error for Set HyperlinkedBook = Workbooks.Open(Filename:=cell.Offset(0, -1).Value) with the error code:
run time error 1004 Sorry we couldn’t find 24 James.xlsx
Is it possible it was moved, renamed or deleted?
I believe that this line of the code is assuming that the hyperlink should open a different workbook with that name, however this is not the case. The hyperlink on the summary sheet links through to other sheets on the same master workbook, only the templates are on a separate book.
So to overcome this I tried changing this line as well and ended up with the code below, which manages to open the template workbook, and copy just the tab name onto the first sheet and then gives an error for the following line TemplateBook.Sheets("Red").Copy ActiveSheet.Paste, saying
subscript out of range
Sub Summary()
Dim MasterBook As Workbook
Set MasterBook = ActiveWorkbook
With MasterBook
Dim rng As Range
Set rng = Range("B:B")
End With
Dim TemplateBook As Workbook
Set TemplateBook = Workbooks.Open(Filename:=" C:\Users\Desktop\Example template.xlsx")
Dim cell As Range
For Each cell In rng
If cell.Value = "Red" Then
cell.Offset(0, -1).Hyperlinks(1).Follow NewWindow:=False, AddHistory:=True
TemplateBook.Sheets("Red").Copy ActiveSheet.paste
ElseIf cell.Value = "Blue" Then
cell.Offset(0, -1).Hyperlinks(1).Follow NewWindow:=False, AddHistory:=True
TemplateBook.Sheets("Blue").Copy ActiveSheet.paste
End If
Next cell
End Sub
I tried several more variations but I just can’t get it to copy the correct template, switch back to the master workbook sheet, follow through the link to correct sheet in the same master workbook, and paste the template.
A few comments about the modifications I made to your code:
Instead of using the entire Column B, try to use only cells in Column B that have values inside them.
Try to avoid using ActiveWorkbook, if the code lies in the same workbook then use ThisWorkbook instead.
When you set a Range, fully qualify it by stating the Workbook and Worksheet, as in : Set Rng = Sht.Range("B1:B" & Sht.Cells(Sht.Rows.Count, "B").End(xlUp).Row).
I replaced your 2 Ifs with Select Case, as they the result in both is the same, and it will also allow you more flexibility in the future to add more cases.
When you copy an entire sheet with TemplateBook.Sheets("Red") and paste it to another Workbook, the syntax is TemplateBook.Sheets("Red").Copy after:=Sht.
Code
Option Explicit
Sub Summary()
Dim MasterBook As Workbook
Dim Sht As Worksheet
Dim Rng As Range
Set MasterBook = ThisWorkbook '<-- use ThisWorkbook not ActiveWorkbook
Set Sht = MasterBook.Worksheets("Sheet3") '<-- define the sheet you want to loop thorugh (modify to your sheet's name)
Set Rng = Sht.Range("B1:B" & Sht.Cells(Sht.Rows.Count, "B").End(xlUp).Row) '<-- set range to all cells in column B with values
Dim TemplateBook As Workbook
Set TemplateBook = Workbooks.Open(Filename:="C:\Users\Desktop\Example template.xlsx")
Dim cell As Range
For Each cell In Rng
Select Case cell.Value
Case "Red", "Blue"
cell.Offset(0, -1).Hyperlinks(1).Follow NewWindow:=False, AddHistory:=True '<-- not so sure what values you have here
TemplateBook.Sheets(cell.Value).Copy after:=Sht '<-- paste after the sheet defined
Case Else
' do something if you have other cases , not sure it's needed
End Select
Next cell
End Sub
Edit 1: to copy>>paste contents of the sheet, use the loop below:
For Each cell In Rng
Select Case cell.Value
Case "Red", "Blue"
cell.Offset(0, -1).Hyperlinks(1).Follow NewWindow:=False, AddHistory:=True '<-- not so sure what values you have here
Application.CutCopyMode = False
TemplateBook.Sheets(cell.Value).UsedRange.Copy
Sht.Range("A1").PasteSpecial '<-- paste into the sheet at Range("A1")
Case Else
' do something if you have other cases , not sure it's needed
End Select
Next cell
Edit 2: Create a new worksheet, and then rename it with the cell.Offset(0, -1).Value
TemplateBook.Sheets(cell.Value).Copy after:=Sht
Dim CopiedSheet As Worksheet
Set CopiedSheet = ActiveSheet
CopiedSheet.Name = cell.Offset(0, -1)

Generate new sheets from a range and allow for use of "IfElse" from an adjecent range to force what the new sheet should contain

I found and altered simple code to create new sheets from a master and name the sheets from the range. The code is below and works without error.
I would like to include an adjacent range (A11) in the code that will determine that the new/copied sheet will be created using MasterReport or MasterReport2 as the copy from. If range A11 has a "P" I would like the new sheet to be copied from MasterReport and if range A11 contains an "S" the new sheet to be copied will be from MasterReport2. MasterReport and MasterReport2 are hidden sheets.
Most likely need to identify a second range and use IfElse but where within the current code should this go. Would like to maintain that the new sheet names will reflect range C11 values.
Dim MyCell As Range, MyRange As Range
Set MyRange = Sheets("ProjectDefinition").Range("c11")
Set MyRange = Range(MyRange, MyRange.End(xlDown))
For Each MyCell In MyRange
Sheets("MasterReport").Select
ActiveSheet.Copy After:=Sheets(Sheets.Count)
Sheets(Sheets.Count).Name = MyCell.Value
Next MyCell
There are a number of methods to get your optional termplate worksheet but I believe a Select Case statement will offer the best expandability.
Dim myCell As Range, strWSname As String
With Sheets("ProjectDefinition")
For Each myCell In .Range(.Range("C11"), .Range("C11").End(xlDown))
Select Case UCase(myCell.Offset(0, -2))
Case "S"
strWSname = "MasterReport2"
Case "P"
strWSname = "MasterReport"
Case Else
strWSname = "MasterReport" 'default
End Select
Sheets(strWSname).Copy After:=Sheets(Sheets.Count)
Sheets(Sheets.Count).Name = myCell.Value
Next myCell
End With

Need a VBA code. rename sheets from list

Need a VBA code. rename sheets from list
I have a list of names in sheet 2 cell w3 thru w22. I need to name sheets/tabs 3 thur 22 from the the list of names. The names change.
and if no name in w3 thru w22. I want the tabs to be numbered 1 thru 20
Any Ideas?
Dim MyCell As Range, MyRange As Range, Dim i As Integer
Set MyRange = Sheets("Summary").Range("w3")
Set MyRange = Range(MyRange, MyRange.End(xlDown))
For Each MyCell In MyRange
If MyCell.Value IS NULL
Sheets(Sheets.Count + 3).Name = i
Sheets(Sheets.Count + 3).Name = MyCell.Value ' renames the new worksheet
Next MyCell
Or something like this maybe, I didn't test it since you didn't give us anything to go off of or make any effort to solve it yourself.
http://en.kioskea.net/faq/27361-excel-a-macro-to-create-and-name-worksheets-based-on-a-list Kudos to this website for giving me a basis to go off of.