# Print worksheet that has same name as cell value



## dgraham1962 (Dec 29, 2022)

Hi,

I would like to write VBA code that will print a specific worksheet that has the same name that will appear in a cell. the cell data will change based on other data entries.

Regards
David


----------



## swapnilk (Dec 30, 2022)

Try This:

```
Sub PrintSpecificWorksheet()
    ' Declare a variable to hold the name of the worksheet
    Dim wsName As String

    ' Set the value of wsName to the value in cell A1 of the active sheet
    wsName = ActiveSheet.Range("A1").Value

    ' Check if the worksheet with the name specified in wsName exists
    If Not WorksheetExists(wsName) Then
        ' If the worksheet does not exist, display an error message
        MsgBox "The worksheet with the name '" & wsName & "' does not exist."
    Else
        ' If the worksheet does exist, print it
        Sheets(wsName).PrintOut
    End If
End Sub

Function WorksheetExists(ByVal wsName As String) As Boolean
    ' This function checks if a worksheet with the specified name exists
    On Error Resume Next
    WorksheetExists = Not (Sheets(wsName) Is Nothing)
End Function
```


----------



## dgraham1962 (Dec 31, 2022)

swapnilk said:


> Try This:
> 
> ```
> Sub PrintSpecificWorksheet()
> ...


----------



## dgraham1962 (Dec 31, 2022)

Thanks very much swapnilk, work a charm and the comments in the code were very helpful to understand what it all meant.

Regards
David


----------

