VBA Code to Create A New Sheet from a Template, input some values from a Master sheet, and return results from Template back into Master

Hachimiya

New Member
Joined
Mar 1, 2019
Messages
1
Hi, I'm new to VBA and need to use it for something similar to what was posted in this thread ( https://www.mrexcel.com/forum/excel...de-create-new-sheet-template-rename-list.html ), but with a few differences.

I have a workbook that tracks the names of various assets & certain numerical metrics in the same row (eg. Price, Opex, etc.) in a sheet called "Master". The names start in column B, row 3, and the various metrics run all the way to Column L.

I also have a sheet named 'Template' that has various formulas, and takes the numerical metrics from columns C-L as inputs to spit out financial indicators (IRR, NPV, etc.) on these assets. The section to input these metrics on the Template sheet is a row (E3:O3) with the same format/ordering of metrics as the Master sheet.

I want to write a macro that copies the 'Template' sheet, renames it to the asset name, and populates the inputs section with the appropriate metrics. After that, I want to pull the financial ratios that are calculated (these will appear in cells F7:I7), and paste them into the Master document on the corresponding row, starting from column M.

This is what I want the master sheet to look like

------------inputs for template---------- | ----outputs from template--------

B | C | D |.... | L | M | N | ...
Name | Price | Age | ....| Interest rate | NPV | IRR | ...


Could you help me out please? Thanks so much
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
Try this

Code:
Sub CreateSheetFromTemplate()
    Dim sh1 As Worksheet, sh2 As Worksheet, sh3 As Worksheet
    Dim u1 As Double
    Application.ScreenUpdating = False
    Application.DisplayAlerts = False
    
    Set sh1 = Sheets("Master")
    Set sh2 = Sheets("Template")
    u1 = sh1.Range("B" & Rows.Count).End(xlUp).Row
    
    For i = 3 To u1
        asset = sh1.Cells(i, "B").Value
        On Error Resume Next
        Sheets(asset).Delete
        On Error GoTo 0
        sh2.Copy after:=Sheets(Sheets.Count)
        Set sh3 = ActiveSheet
        sh3.Name = asset
        sh3.Range("E3:N3").Value = sh1.Range("C" & i & ":L" & i).Value
        sh1.Range("M" & i & ":P" & i).Value = sh3.Range("F7:I7").Value
    Next
    
    sh1.Select
    Application.ScreenUpdating = True
    MsgBox "Finish"
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,888
Messages
6,175,213
Members
452,618
Latest member
Tam84

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top