VBA & Macro Problems

Create RFI Worksheets from an Excel Template Using VBA

Steve KSteve K Nov 25, 2026 870 views

Question details

Create an RFI worksheet from a template and populate it with the next schedule row when a button is clicked.

Create RFI Worksheets from an Excel Template Using VBA
Product
Microsoft Excel
Device & OS
not provided
Scenario
An RFI schedule supplies data for copies of a template, with each new worksheet receiving a sequential name such as RFI001.
Observed behavior
Each button click should create a numbered RFI sheet and fill its named cells from the corresponding schedule row.
Before you start

Use desktop Excel for Windows with macros enabled, and save the workbook as an Excel Macro-Enabled Workbook (.xlsm). Prepare visible sheets named Schedule and Template, with schedule headings in row 1 and data beginning in row 2.

Solution 1Recommended

Copy and populate the RFI template with an ActiveX button

Use worksheet-scoped names and a VBA macro to create one numbered RFI sheet per schedule row.

This example maps RFI001 to Schedule row 2, RFI002 to row 3, and so on. It finds the highest existing three-digit RFI sheet number, so use this mapping consistently and do not delete numbered sheets if you need to preserve the sequence.

Create single-cell names scoped to the Template sheet. Each name must match a schedule heading and follow Excel's naming rules; headings without a matching named cell are skipped.

1
Name the template cells

On Template, select a cell, open Formulas > Name Manager > New, enter a name matching a Schedule heading, and set Scope to Template. Repeat for each field to populate.

2
Add the VBA code

Press Alt+F11, select Insert > Module, and paste this code: Option Explicit Public Sub CreateRFI() Dim schedule As Worksheet, template As Worksheet, result As Worksheet Dim ws As Worksheet, target As Range Dim maxNo As Long, n As Long, dataRow As Long, col As Long, lastCol As Long Dim heading As String On Error GoTo Failed Set schedule = ThisWorkbook.Worksheets("Schedule") Set template = ThisWorkbook.Worksheets("Template") For Each ws In ThisWorkbook.Worksheets If UCase$(ws.Name) Like "RFI###" Then n = CLng(Right$(ws.Name, 3)) If n > maxNo Then maxNo = n End If Next ws If maxNo >= 999 Then MsgBox "This example supports RFI001 through RFI999." Exit Sub End If n = maxNo + 1 dataRow = n + 1 If Application.WorksheetFunction.CountA(schedule.Rows(dataRow)) = 0 Then MsgBox "Schedule row " & dataRow & " is empty." Exit Sub End If template.Copy After:=ThisWorkbook.Worksheets(ThisWorkbook.Worksheets.Count) Set result = ActiveSheet result.Name = "RFI" & Format$(n, "000") lastCol = schedule.Cells(1, schedule.Columns.Count).End(xlToLeft).Column For col = 1 To lastCol heading = Trim$(CStr(schedule.Cells(1, col).Value)) If Len(heading) > 0 Then Set target = LocalTemplateCell(result, heading) If Not target Is Nothing Then target.Value = schedule.Cells(dataRow, col).Value End If Next col MsgBox result.Name & " created from Schedule row " & dataRow & "." Exit Sub Failed: MsgBox "Could not create the RFI: " & Err.Description & _ ". If a partial sheet was created, review or remove it before retrying." End Sub Private Function LocalTemplateCell(ByVal ws As Worksheet, ByVal heading As String) As Range Dim nm As Name, r As Range For Each nm In ws.Names If StrComp(Mid$(nm.Name, InStrRev(nm.Name, "!") + 1), heading, vbTextCompare) = 0 Then Set r = Nothing On Error Resume Next Set r = nm.RefersToRange On Error GoTo 0 If Not r Is Nothing Then If r.Parent Is ws Then If r.Cells.CountLarge = 1 Then Set LocalTemplateCell = r End If End If Exit Function End If Next nm End Function

3
Connect an ActiveX button

On Schedule, choose Developer > Insert > Command Button (ActiveX Control). With Design Mode enabled, double-click the button and add CreateRFI between the CommandButton1_Click event lines.

4
Test the sequence

Return to Excel, turn off Developer > Design Mode, and click the button. Check that RFI001 contains the values from Schedule row 2, then click again to create RFI002 from row 3.

Copy and populate the RFI template with an ActiveX button
Check missing fields: Unmatched headings are intentionally skipped. If a value is missing, check the heading spelling, the name's Template scope, and whether the name refers to a single cell on the template.
Free Microsoft Office alternative

Manage RFI schedules and templates with WPS Spreadsheets

WPS Office is a free, lightweight alternative with a familiar spreadsheet interface and Microsoft Office format compatibility for moving existing schedules and templates. Excel-specific ActiveX and VBA behavior may differ, so test any automation before relying on it.

Edit RFI schedules and worksheet templates in a familiar interfaceOpen and work with Microsoft Excel formats such as XLSXUse a free, lightweight office suiteMove existing spreadsheet files into WPS Office
microsoft office alternative - wps office

Frequently Asked Questions

Why does the macro skip a schedule column?

The macro skips headings without a matching single-cell name scoped to the copied worksheet. In Formulas > Name Manager, check that the original name is scoped to Template and matches the row 1 heading.

How does the macro choose the next schedule row?

It finds the highest existing sheet name from RFI001 through RFI999, adds one, and uses that number plus one as the Schedule row. For example, RFI002 uses row 3.

Can I run the ActiveX button in Excel for the web?

No. Open the workbook in desktop Excel for Windows to use the ActiveX button and VBA macro.

What happens if I delete an RFI worksheet?

The macro uses the highest remaining RFI number, not a separate completion log. Deleting the highest-numbered sheet can cause its schedule row to be used again on the next click.