Excel 2003: Add worksheet with variable as worksheet name?


New Member
Sep 7, 2011
OK, folks... some more VBA help needed please!!!

I need to create a macro which will prompt the user to select a PDF file on their HDD, then have the PDF filename used as the worksheet name.

The PDFs have the same naming convention: "MachineReport 1234ABC 31102011" where MachineReport is constant, and 1234ABC and 31102011 will vary - they are the machine serial number and date of report creation respectively.

Ideally I'd like the worksheet tab to be named with just 'MachineReport 1234ABC'.

And I'd like this to loop and allow the user to add multiple worksheets, one for each MachineReport PDF file selected by the user, named appropriately.

And for extra ease of use... the macro should, at the start, check for any worksheets already called 'MachineReport xxxxxxx' and delete them.

Sounds simple in theory but can I work it out? No. Hence why I'm asking the collective wisdom on here!!!

Thanks in advance!


Well-known Member
Apr 27, 2011
try this, run "Control" macro to start the process:

Sub Control()
Dim strFile As String

Call DeleteSheets

'get file name
strFile = Application.GetOpenFilename("PDF Files (*.pdf),*.pdf")

'if user cancels file selection then exit loop
If strFile = "False" Then
Exit Do
Call AddSheet(strFile)
End If
End Sub

Sub AddSheet(strFile As String)
Dim ws As Worksheet
Dim part1 As String
Dim part2 As String

strFile = Left(strFile, Len(strFile) - 4) 'get rid of PDF extension

'get only the file name
If InStr(1, strFile, "\", vbTextCompare) Then
strFile = Right(strFile, Len(strFile) - InStr(1, strFile, "\", vbTextCompare))
Exit Do
End If

part1 = Left(strFile, InStr(1, strFile, " ", vbTextCompare))
strFile = Right(strFile, Len(strFile) - Len(part1))
part2 = Left(strFile, Len(strFile) - InStr(1, strFile, " ", vbTextCompare))

'add and rename worksheet
Set ws = Worksheets.Add
On Error Resume Next
ws.Name = part1 & " " & part2
If Err.Number <> 0 Then
Application.DisplayAlerts = False
Application.DisplayAlerts = True
MsgBox "Worksheet already exists", vbCritical
End If

Set ws = Nothing
End Sub

Sub DeleteSheets()
Dim ws As Worksheet

'delete worksheets with "MachineReport" in the name
Application.DisplayAlerts = False
For Each ws In Worksheets
If InStr(1, ws.Name, "MachineReport", vbTextCompare) Then
End If
Application.DisplayAlerts = True
End Sub

Forum statistics

Latest member

Some videos you may like

This Week's Hot Topics

  • populate from drop list with multiple tables
    Hi All, i have a drop list that displays data, what i want is when i select one of those from the list to populate text from different tables on...
  • Find list of words from sheet2 in sheet1 before a comma and extract text vba
    Hi Friends, Trying to find the solution on my task. But did not find suitable one to the need. Here is my query and sample file with details...
  • Dynamic Formula entry - VBA code sought
    Hello, really hope one of you experts can help with this - i've spent hours on this and getting no-where. .I have a set of data (more rows than...
  • Listbox Header
    Have a named range called "AccidentsHeader" Within my code I have: [CODE]Private Sub CommandButton1_Click() ListBox1.RowSource =...
  • Complex Heat Map using conditional formatting
    Good day excel world. I have a concern. Below link have a list of countries that carries each country unique data. [URL...
  • Conditional formatting
    Hi good morning, hope you can help me please, I have cells P4:P54 and if this cell is equal to 1 then i want row O to say "Fully Utilised" and to...