Excel VBA: How to create multiple Excel files using a single Excel template - vba

My requirement is simple:
I have a list of filename in column A:A200 of Sheet1 of Book1 and an Excel Book2 which is a readymade template with 6 sheets.
I need to create or rather replicate this excel Book2, by changing the file name of the template excel to the names I have in column A:A200 of sheet1 of Book1.
Also a small functionality would be that the same name (name of the Excel or the name present in the column A) should be added to Sheet1, cell B5 of the new sheet.
So basically I have to make copies of an excel template and rename them according to my list and also change a value in the new sheet as the name of the Excel.
Am sure many users would have wanted this simple functionality, so any small VBA code, or reference to any code already present would be helpful.

Check out the example here
You can Down load the zipped Folder there as well to test and work with the code.

Related

XML sheet names not seeming to agree with vba worksheet names

I have a large bloated workbook.
I've made the file a zip and opened the zip to inspect the xml files. Then I've looked at the worksheets folder in the zip to see this:
If I then look in the vba editor I can see this:
But "Sheet25" in the vba is a tab for notes with only 5 lines of text on it so these names are not the same.
Is there an easy way for me to map between the two different identifiers? e.g. what Sheet is sheet25.xml?
The simplest check is to open sheet25.xml. You will see the matching codeName in this line at the top:
<sheetPr codeName="Sheet22"/>
EDIT:
If you don't want to open the xml file (because it's too big), there is another way you can trace the mapping of the xml sheet filenames to the codeNames of the sheets in the workbook.
Open up the xl/_rels/workbook.xml.rels document. You should see lines like this:
<Relationship Target="worksheets/sheet3.xml" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet" Id="rId3"/>
<Relationship Target="worksheets/sheet2.xml" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet" Id="rId2"/>
<Relationship Target="worksheets/sheet1.xml" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet" Id="rId1"/>
Link the xml sheet name to its resource ID on the far right, e.g.,
worksheets/sheet2.xml => Id="rId2"
Then open up the xl/workbook.xml document, where you get:
<sheet r:id="rId1" sheetId="1" name="one"/>
<sheet r:id="rId2" sheetId="3" name="Sheet3"/>
<sheet r:id="rId3" sheetId="4" name="Sheet1"/>
Link the resource ID to the sheet name, e.g.,
r:id="rId2" => name="Sheet3"
And then from the VBE editor:
Sheet3 (Sheet3)
Match the name to the tab name inside the parentheses to get the codeName:
Sheet3 <= (Sheet3)
Done.
I think that the sheet number of the xml file is just the order of the sheet in the workbook counting from the left at the point at which you saved the file. To find it you can open your spreadsheet and unhide all sheets. Alternatively you can use the formula =SHEET() in a worksheet to identify its number.
Note that this is different to the sheetcode in VBA which is a permanent sheet number that you see if you open the project explorer in VBA. The sheetcode will not change if you move it around.

Importing data into a master worksheet

I have a doubt about my program to the VBA/EXCEL
I made a file called "MASTER WORKSHEET" with various data from a client, my goal is: To create a VBA to open any file in Excel and that I import chosen cells to fill out all data of the "MASTER WORKSHEET", for example, all excel file I open with the button "IMPORT DATA", it will automatically capture only those cells that I choose, but my program it captures but is with configuration problems on VALUE or NUMBER.
If your question is simply "how do I copy a cell from one place to another", an example would be:
Workbooks("Input Workbook").Worksheets("Input Worksheet").Range("J1").Copy(Workbooks("Master Workbook").Worksheets("Master Worksheet").Range("A4"))
If you just want to copy the value, without the formula and formatting, you could use
Workbooks("Master Workbook").Worksheets("Master Worksheet").Range("A4").Value = Workbooks("Input Workbook").Worksheets("Input Worksheet").Range("J1").Value
Obviously, the workbook, sheet, and range names need to be changed to your particular situation.

I need to generate new workbooks using existing sheets that I will autopop with data

I have two sheets on my template. Sheet one contains a SKU in column A and a descr. in column B
On sheet 2, I have a pre-written template that has generic SKU and Description. That means under the item sku, it says D999 and under the description it says xxx.
I want to replace "999" with the value in A2 on my first sheet, but the 999 is in multiple columns through the sheet. Next, I want to replace the "xxx" with the data in B2 from sheet 1. I can't seem to find a VBA code that will do this specifically.
Lastly, once the find and replaces are done, I need to have it save me a new workbook using sheet2, in the same format, and name it as the value in A4 on sheet 2. I think you should be able to find the workbook here My Workbook
Basically, I want to be able to enter a bulk amount of new SKUs and Description, and be able to generate a new workbook for each SKU I enter. Right now I have 78 waiting to have sheets made.
In the cell to the right of the cells where you want to type in the SKU, add the following formula: =Match(A2, LookupsheetName!B:B,0). This will return the row the SKU was found on. In the next cell to the right use the Index function: =Index(LookupSheetName!A1:B1000, B2, 0). This will bring the label in column B to the current page. The Match function locates the row the SKU was found on.
Try these for looking up the information, because they are more efficient than VLookups.
The code for copying a worksheet to another workbook can be created using the Macro recorder on the Developer tab. Turn the recorder on then do the steps to manually move/copy to another workbook. When done, open the VBA editor and figure out how to save the workbook to a new name each time.
Good luck.

Hyperlink after Vlookup

This simple thing is eating my brain out. Here is the challenge:
I have 2 sheets in the same workbook. Say Sheet 1 and Sheet 2. I want to Vlookup for the value present in Sheet 1 Column A and Sheet 2 column B. If the value is found i want to put a hyperlink in Sheet 1 Column A to point it to Sheet 2 Column D. In simple words, there are program names present in both the sheets and associated risk in sheet 2. If the program is present in both the sheets then i want to navigate directly to the risk present in the Sheet 2 upon clicking the program name in Sheet1.
I am attaching the sample file with this thread. Please somebody help me. I have tried with vba and normal formulas. Nothing is being worked out.
SAmple file can be downloaded from here
Here's how you can do that with a formula. You'll need to change the workbook name in the HYPERLINK formula to match yours.
=IF(ISERROR(VLOOKUP($A1,Sheet2!$B:$B,1,0)),"",HYPERLINK("[StackTest.xlsx]'Sheet2'!D"&TEXT(MATCH($A1,Sheet2!$B:$B,0),"#"),"Click Here"))

Excel VBA - combining worksheets from multiple workbooks and rename new worksheet tab

I am trying to create some VBA code that will do the following:
Take worksheets 2 and 3 from a number of different workbooks (all in the same folder) and put them into a new workbook.
Worksheet 2 has the same name in each of the different workbooks so I want the VBA to also change the name of the new worksheet to a cell reference from the original worksheet (in this case cell A6)
Worksheet 3 has the same name in each of the different workbooks so I want the VBA to also change the name of this new worksheet to the same cell reference (A6), combined with the text "ph"
Any help would be gratefully received
Rob
You'll be able to figure out all of the code you need with the help of the Macro Recorder. Turn that on and manually do your listed steps (make sure you stop recording when that's done), then look for pieces of code that move the worksheets and rename them. Once you see that code, you can figure out how to customize it to your needs.