Option Explicit
Function getLastUsedRow(ws As Worksheet) As Long
Dim lastUsedRow As Long: lastUsedRow = 1
On Error Resume Next
lastUsedRow = ws.Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
On Error GoTo 0
getLastUsedRow = lastUsedRow
End Function
Function checkIfWorksheetExists(wb As Workbook, wsName As String) As Boolean
Dim i As Long
Dim found As Boolean
found = False
For i = 1 To wb.Worksheets.Count
If Trim(wb.Worksheets(i).Name) = Trim(wsName) Then
found = True
Exit For
End If
Next i
checkIfWorksheetExists = found
End Function
Sub GENERATE_SHEETS_BASED_ON_TEMPLATE()
' -----------------------------------------------------------------------------------------------
' First we check if RUN worksheet exist, if it doesn't we exit the code !
' -----------------------------------------------------------------------------------------------
If Not checkIfWorksheetExists(ThisWorkbook, "RUN") Then
MsgBox "Please create 'RUN' sheet, and run this macro again!", vbExclamation
Exit Sub
End If
' -----------------------------------------------------------------------------------------------
' Next we check if Summary worksheet exist, if it doesn't we exit the code !
' -----------------------------------------------------------------------------------------------
If Not checkIfWorksheetExists(ThisWorkbook, "Summary") Then
MsgBox "Please create 'Summary' sheet, and run this macro again!", vbExclamation
Exit Sub
End If
' -----------------------------------------------------------------------------------------------
' Next we check if Template worksheet exist, if it doesn't we exit the code !
' -----------------------------------------------------------------------------------------------
If Not checkIfWorksheetExists(ThisWorkbook, "Template") Then
MsgBox "Please create 'Template' sheet, and run this macro again!", vbExclamation
Exit Sub
End If
Dim i As Long
Dim j As Long
Dim k As Long
Application.ScreenUpdating = False
' --------------------------------------------------------------------------------------------
' --------------------------- VARIABLES FOR SUMMARY WORKSHEET ! ------------------------------
' --------------------------------------------------------------------------------------------
Dim wsSummary As Worksheet: Set wsSummary = ThisWorkbook.Worksheets("Summary")
Dim wsSummaryStartingRow As Long: wsSummaryStartingRow = 2 ' Starting row, first is header row !
Dim wsSummaryEndingRow As Long: wsSummaryEndingRow = getLastUsedRow(wsSummary) + 10 ' Ending row !
Dim wsSummaryCurrentSheetName As String
' --------------------------------------------
' Loop through all rows in Summary sheet !
' --------------------------------------------
For i = wsSummaryStartingRow To wsSummaryEndingRow
' ----------------------
' Reset variables !
' ----------------------
wsSummaryCurrentSheetName = ""
' ----------------------
' Extract variables !
' ----------------------
wsSummaryCurrentSheetName = Trim(wsSummary.Range("A" & CStr(i)).Value2)
' -----------------------
' Skip empty values !
' -----------------------
If wsSummaryCurrentSheetName <> "" Then
' -----------------------------------------------------------------------------
' Next if the sheet doesn't exists already, generate it based on template !
' -----------------------------------------------------------------------------
If Not checkIfWorksheetExists(ThisWorkbook, wsSummaryCurrentSheetName) Then
' -----------------------------------
' Create a copy of the template !
' -----------------------------------
ThisWorkbook.Worksheets("Template").Copy After:=ThisWorkbook.Worksheets(ThisWorkbook.Worksheets.Count)
ActiveSheet.Name = wsSummaryCurrentSheetName
' ---------------------------------------------------------------------------
' Fill the copy of the Template sheet, with values from the current row !
' ---------------------------------------------------------------------------
ThisWorkbook.Worksheets(wsSummaryCurrentSheetName).Range("A7").Value2 = wsSummary.Range("A" & CStr(i)).Value2
ThisWorkbook.Worksheets(wsSummaryCurrentSheetName).Range("G8").Value2 = wsSummary.Range("B" & CStr(i)).Value2
ThisWorkbook.Worksheets(wsSummaryCurrentSheetName).Range("B8").Value2 = wsSummary.Range("C" & CStr(i)).Value2
ThisWorkbook.Worksheets(wsSummaryCurrentSheetName).Range("B7").Value2 = wsSummary.Range("D" & CStr(i)).Value2
ThisWorkbook.Worksheets(wsSummaryCurrentSheetName).Range("G7").Value2 = wsSummary.Range("E" & CStr(i)).Value2
ThisWorkbook.Worksheets(wsSummaryCurrentSheetName).Range("H7").Value2 = wsSummary.Range("F" & CStr(i)).Value2
ThisWorkbook.Worksheets(wsSummaryCurrentSheetName).Range("I7").Value2 = wsSummary.Range("G" & CStr(i)).Value2
ThisWorkbook.Worksheets(wsSummaryCurrentSheetName).Range("J7").Value2 = wsSummary.Range("H" & CStr(i)).Value2
ThisWorkbook.Worksheets(wsSummaryCurrentSheetName).Range("L7").Value2 = wsSummary.Range("I" & CStr(i)).Value2
ThisWorkbook.Worksheets(wsSummaryCurrentSheetName).Range("K7").Value2 = wsSummary.Range("J" & CStr(i)).Value2
ThisWorkbook.Worksheets(wsSummaryCurrentSheetName).Range("M7").Value2 = wsSummary.Range("K" & CStr(i)).Value2
ThisWorkbook.Worksheets(wsSummaryCurrentSheetName).Range("N7").Value2 = wsSummary.Range("L" & CStr(i)).Value2
ThisWorkbook.Worksheets(wsSummaryCurrentSheetName).Range("O7").Value2 = wsSummary.Range("M" & CStr(i)).Value2
ThisWorkbook.Worksheets(wsSummaryCurrentSheetName).Range("R7").Value2 = wsSummary.Range("N" & CStr(i)).Value2
ThisWorkbook.Worksheets(wsSummaryCurrentSheetName).Range("T7").Value2 = wsSummary.Range("O" & CStr(i)).Value2
ThisWorkbook.Worksheets(wsSummaryCurrentSheetName).Range("H82").Value2 = wsSummary.Range("P" & CStr(i)).Value2
ThisWorkbook.Worksheets(wsSummaryCurrentSheetName).Range("I82").Value2 = wsSummary.Range("Q" & CStr(i)).Value2
ThisWorkbook.Worksheets(wsSummaryCurrentSheetName).Range("J82").Value2 = wsSummary.Range("R" & CStr(i)).Value2
ThisWorkbook.Worksheets(wsSummaryCurrentSheetName).Range("Q7").Value2 = wsSummary.Range("S" & CStr(i)).Value2
End If
End If
Next i
' ----------------------------
' Activate the RUN sheet !
' ----------------------------
ThisWorkbook.Worksheets("RUN").Activate
' -----------------------------
' Remove unused variables !
' -----------------------------
Set wsSummary = Nothing
Application.ScreenUpdating = True
' ------------------------------------------------
' Inform the user that process was completed !
' ------------------------------------------------
MsgBox "Done!", vbInformation
End Sub