Macro to create excel sheets dynamically based on values of one sheet

Sub Macro2()
Dim wsNew As Worksheet
Dim wsMaster As Worksheet
Dim x As Long

Dim cell As Range
'Sheet2 will be copied as new sheet and renamed with data in Sheet1 columns A and B
Set wsMaster = ActiveWorkbook.Sheets("Sheet2")
x = 1

Do While Not IsEmpty(Sheet1.Cells(x, 1).Value)
wsMaster.Copy Before:=wsMaster

Sheet_name_to_create = Sheet1.Cells(x, 1).Value & "-" & Sheet1.Cells(x, 2).Value

Set wsNew = ActiveWorkbook.ActiveSheet
wsNew.Name = Sheet_name_to_create

x = x + 1
Loop

End Sub

This creates sheets with name of col A and B in sheet 1 and copy the sheet2

Advertisements

Parse xml using C# and print path to each element

Code:

using System;
using System.Collections;
using System.Linq;
using System.Text;
using System.Xml;
using System.IO;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Stack elements = new Stack();
StringBuilder output = new StringBuilder();
//Example string
String xmlString =
@”<bookstore>
<book genre=’autobiography’ publicationdate=’1981-03-22′ ISBN=’1-861003-11-0′>
<title>The Autobiography of Benjamin Franklin</title>
<author>
<first-name>Benjamin</first-name>
<last-name>Franklin</last-name>
</author>
<price>8.99</price>
</book>
</bookstore>”;

// Replace the path if you have an xml file
// XmlTextReader reader = new XmlTextReader(“D:\\Example.xml”);
XmlReader reader = XmlReader.Create(new StringReader(xmlString));
while(reader.Read())
{
if (reader.NodeType == XmlNodeType.Element)
{
elements.Push(reader.Name);
}
if (reader.NodeType == XmlNodeType.EndElement)
{
//Console.WriteLine(currentElement);
if (reader.Name == elements.Peek())
{
Console.WriteLine(elements.Peek() + “:” + PrintValues(elements));
elements.Pop();
}
}

}
Console.ReadLine();
}
public static String PrintValues(IEnumerable myCollection)
{
Stack reverse = new Stack();
String value = “”;
foreach (Object obj in myCollection)
reverse.Push(obj.ToString());
foreach (String values in reverse)
value += values+”/”;
return value;
}
}
}

Output:

title:bookstore/book/title/
first-name:bookstore/book/author/first-name/
last-name:bookstore/book/author/last-name/
author:bookstore/book/author/
price:bookstore/book/price/
book:bookstore/book/
bookstore:bookstore/