Search code examples
excelvba

VBA Excel problem with copying images across all worksheets generated by the function


I have a function, which populates the worksheets for me. One of the elements of this worksheet is copying the image located on the "Frontsheet" worksheet.

My code looks like this:

 Function CreateStandardSheet(wkb As Workbook, SheetName As String) As Worksheet
 Set CreateStandardSheet = wkb.Sheets.Add(After:=wkb.Sheets(wkb.Sheets.Count))
 With CreateStandardSheet
    .Name = SheetName
    .Tab.Color = RGB(255, 0, 0)
    Union(.Columns("A"), .Columns("S")).ColumnWidth = 0.88
    .Columns("M:P").ColumnWidth = 13
    Rows(1).RowHeight = 3
    Rows(49).RowHeight = 3
    
    '--------------------------------2.1 Major working area ------------------------------------------
    With Range("B2:L48")
    .Merge
    .BorderAround ColorIndex:=1, Weight:=xlMedium
    End With
    '---------------------------------2.2 Logo section -------------------------------------------------
    With Range("M41:P46") '....2.2.1 CLIENT LOGO....
    .Merge
    .BorderAround ColorIndex:=1, Weight:=xlMedium
    On Error Resume Next
    Worksheets("Frontsheet").Shapes("ClientLogo").Copy
    CreateStandardSheet.Paste
    
    With CreateStandardSheet.Shapes("ClientLogo")
    .Top = 570
    .Left = 600
    .Width = 170
    End With

At the following line:

 Worksheets("Frontsheet").Shapes("ClientLogo").Copy

I have an error:

Compile error: Expected variable or procedure, not module.

I am wondering where the problem might be and why can't I populate the images across all the worksheets created by this function.


Solution

  • Qualify Your Objects (Ranges and Worksheets)

    Function CreateStandardSheet(ByVal wkb As Workbook, ByVal SheetName As String) As Worksheet
        
        Dim sh As Object
        On Error Resume Next
            Set sh = wkb.Sheets(SheetName)
        On Error GoTo 0
        
        If Not sh Is Nothing Then
            MsgBox "A sheet named """ & sh.Name & """ already exists " _
                & "in workbook """ & wkb.Name & """!", vbCritical
            Exit Function
        End If
        
        Set CreateStandardSheet = wkb.Sheets.Add(After:=wkb.Sheets(wkb.Sheets.Count))
        
        With CreateStandardSheet
    
            .Name = SheetName
            .Tab.Color = RGB(255, 0, 0)
            Union(.Columns("A"), .Columns("S")).ColumnWidth = 0.88
            .Columns("M:P").ColumnWidth = 13
            .Rows(1).RowHeight = 3
            .Rows(49).RowHeight = 3
            '--------------------------------2.1 Major working area ------------------------------------------
            With .Range("B2:L48")
                .Merge
                .BorderAround ColorIndex:=1, Weight:=xlMedium
            End With
            '---------------------------------2.2 Logo section -------------------------------------------------
            With .Range("M41:P46") '....2.2.1 CLIENT LOGO....
                .Merge
                .BorderAround ColorIndex:=1, Weight:=xlMedium
            End With
            
            wkb.Worksheets("Frontsheet").Shapes("ClientLogo").Copy
            .Paste
            
            With .Shapes("ClientLogo")
                .Top = 570
                .Left = 600
                .Width = 170
            End With
            ' Missing code?
        End With
        ' Missing code?
    
    End Function