Please Help...Edit Macro to Run From Master Workbook

Fieldsdj

Board Regular
Joined
Feb 27, 2012
Messages
65
Hello....I'm a noob when it comes to this. I've come along way but I've been trying for about 2 hours now to get this to work. I'm desperate....Please help!

I have code that works great. When a button is clicked it copies a master template sheet to the end of the tabs and names it according to a cell. I'm trying to figure out how to have all my macros in a "master" workbook and have a macro in the "user" workbook that references the macro in the "master".

Here is the working code that I'm trying to do this with:
Code:
Sub BuildSheet1_B29()

    Dim numrows As Long, i As Long
    Dim sname As String
    Dim objMySheet As Object
    Dim varInvalidChars As Variant
    Dim lngCellPos As Long
    
    varInvalidChars = Array(":", "\", "/", "?", "*", "[", "]")
    Application.ScreenUpdating = False
    With Sheet10 'Sheet name for the main sheet where the button will be.
        numrows = WorksheetFunction.CountA(.Range("B29"))
        If numrows = 0 Then Exit Sub
            For i = 1 To numrows
                'Check for invalid characters within the proposed name
                For lngCellPos = 1 To Len(.Cells(i + 28, "B").Value)
                    If IsNumeric(Application.Match(Mid(.Cells(i + 28, "B").Value, lngCellPos, 1), varInvalidChars, 0)) = True Then
                    MsgBox "The name you're trying to enter in cell B" & i + 28 & " contains one or more invalid characters." & vbNewLine & "Remove any invalid characters: : \ / ? * [ or ] and try again.", vbCritical
                    Exit Sub
                    End If
                Next lngCellPos
            'Proposed sheet name validation
            If Len(.Cells(i + 28, "B").Value) = 0 Then
                MsgBox "You have not entered a name in cell B" & i + 28 & "." & vbNewLine & "Ensure there is a name in the cell and try again.", vbCritical
                Exit Sub
            ElseIf Len(.Cells(i + 28, "B").Value) > 31 Then
                MsgBox "The name you're trying to enter in cell B" & i + 28 & " has more then 31 characters." & vbNewLine & "Ensure the name has a maximum of 31 characters and try again.", vbCritical
                Exit Sub
            ElseIf StrConv(.Cells(i + 28, "B").Value, vbProperCase) = "History" Then
                MsgBox "A tab cannot be named ""History"" as it is a reserved name." & vbNewLine & "Change the name in cell B" & i + 28 & " and try again.", vbCritical
                Exit Sub
            Else
                'If the proposed name in cell B[i+28] already exists in the workbook, then...
                On Error Resume Next 'Stop run-time error 9 message if the tab doesn't exist
                    Set objMySheet = Sheets(.Cells(i + 28, "B").Value)
                    If Err.Number = 0 Then
                        '...put that name to the 'sname' variable and alert the user that it already exists and quit the routine.
                        sname = .Cells(i + 28, "B").Value
                        MsgBox "There is already a Training Worksheet called """ & sname & """ in the workbook." & vbNewLine & "Remove or change the name in cell B" & i + 28 & " and try again.", vbCritical
                        Exit Sub
                    End If
                On Error GoTo 0
            End If
            'If the code gets here copy the template sheet (set here as 'Sheet2') to the end of this workbook and name it via the text in cell B[i+28]
            
            Sheet2.Copy After:=Sheets(Sheets.Count)
            ThisWorkbook.Sheets(Sheets.Count).Name = .Cells(i + 28, "B").Value
            Sheet6.Activate
            ActiveSheet.Unprotect
            Range("A8").Select 'Selects Cell A8 on Sheet 6 (Status sheet). This cell holds the value "CORE" to copy into the Range of cells below.
                    Selection.Copy
                    Range("C12,E12,G12,I12,K12,M12,Q12,S12").Select
                    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
                     :=False, Transpose:=False
                     
            Range("I71").Select 'Selects Cell I71 on Sheet 6 (Status sheet). This cell holds the value "NO CORE" to copy into the Range of cells below.
                    Selection.Copy
                    Range("O12:P12").Select
                    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
                     :=False, Transpose:=False
                     Range("U11").Select
             
             ActiveSheet.Protect
            Sheet10.Activate
            Sheet10.Protect
            
            Next i
    End With
    
    Set objMySheet = Nothing
    Application.ScreenUpdating = True
End Sub

I took the code above out of the user workbook and put it into the master workbook. I put this code in the user workbook to refer to and run it.
Code:
Sub Build_B29()
Application.ScreenUpdating = False
Application.Run ("'S:\OSL\AFE Training\CodeMasterWorkbook.xlsm'!BuildSheet1_B29")
Application.ScreenUpdating = True
    
End Sub

I've tested this with a small procedure and it worked so I know it's possible. I've tried everything to get my code to work and I'm about at my wits end. I've gotten as far as getting the template sheet to copy and paste into the user workbook but it won't change the name. Here is my edited code so far:
Code:
Sub BuildSheet1_B29()

    Dim numrows As Long, i As Long
    Dim sname As String
    Dim objMySheet As Object
    Dim varInvalidChars As Variant
    Dim lngCellPos As Long
    Dim wkbMain As Object, wkbCode As Object
    Dim ws1 As Worksheet
    Dim wsname As Object
    Set wkbMain = Workbooks("AFE C-130 Training Plan Test Copy.xlsm")
    Set wkbCode = Workbooks("CodeMasterWorkbook.xlsm")
    Set ws1 = Sheets("C-130 MTP Template")
    Set wsname = wkbMain.Sheet("Setup").Cells(i + 28, "B").Value
    
    
    varInvalidChars = Array(":", "\", "/", "?", "*", "[", "]")
    Application.ScreenUpdating = False
    With ActiveSheet 'Sheet name for the main sheet where the button will be.
        numrows = WorksheetFunction.CountA(.Range("B29"))
        If numrows = 0 Then Exit Sub
            For i = 1 To numrows
                'Check for invalid characters within the proposed name
                For lngCellPos = 1 To Len(.Cells(i + 28, "B").Value)
                    If IsNumeric(Application.Match(Mid(.Cells(i + 28, "B").Value, lngCellPos, 1), varInvalidChars, 0)) = True Then
                    MsgBox "The name you're trying to enter in cell B" & i + 28 & " contains one or more invalid characters." & vbNewLine & "Remove any invalid characters: : \ / ? * [ or ] and try again.", vbCritical
                    Exit Sub
                    End If
                Next lngCellPos
            'Proposed sheet name validation
            If Len(.Cells(i + 28, "B").Value) = 0 Then
                MsgBox "You have not entered a name in cell B" & i + 28 & "." & vbNewLine & "Ensure there is a name in the cell and try again.", vbCritical
                Exit Sub
            ElseIf Len(.Cells(i + 28, "B").Value) > 31 Then
                MsgBox "The name you're trying to enter in cell B" & i + 28 & " has more then 31 characters." & vbNewLine & "Ensure the name has a maximum of 31 characters and try again.", vbCritical
                Exit Sub
            ElseIf StrConv(.Cells(i + 28, "B").Value, vbProperCase) = "History" Then
                MsgBox "A tab cannot be named ""History"" as it is a reserved name." & vbNewLine & "Change the name in cell B" & i + 28 & " and try again.", vbCritical
                Exit Sub
            Else
                'If the proposed name in cell B[i+28] already exists in the workbook, then...
                On Error Resume Next 'Stop run-time error 9 message if the tab doesn't exist
                    Set objMySheet = Sheets(.Cells(i + 28, "B").Value)
                    If Err.Number = 0 Then
                        '...put that name to the 'sname' variable and alert the user that it already exists and quit the routine.
                        sname = .Cells(i + 28, "B").Value
                        MsgBox "There is already a Training Worksheet called """ & sname & """ in the workbook." & vbNewLine & "Remove or change the name in cell B" & i + 28 & " and try again.", vbCritical
                        Exit Sub
                    End If
                On Error GoTo 0
            End If
            'If the code gets here copy the template sheet (set here as 'Sheet2') to the end of this workbook and name it via the text in cell B[i+28]
            With wkbMain
                .Sheets("C-130 MTP Template").Copy After:=Sheets(Sheets.Count)
                ThisWorkbook.Sheets(Sheets.Count).Name = wsname
            wkbMain.Sheet6.Activate
            ActiveSheet.Unprotect
            Range("A8").Select 'Selects Cell A8 on Sheet 6 (Status sheet). This cell holds the value "CORE" to copy into the Range of cells below.
                    Selection.Copy
                    Range("C12,E12,G12,I12,K12,M12,Q12,S12").Select
                    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
                     :=False, Transpose:=False
                     
            Range("I71").Select 'Selects Cell I71 on Sheet 6 (Status sheet). This cell holds the value "NO CORE" to copy into the Range of cells below.
                    Selection.Copy
                    Range("O12:P12").Select
                    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
                     :=False, Transpose:=False
                     Range("U11").Select
             
             ActiveSheet.Protect
            wkbMain.Sheet10.Activate
            wkbMain.Sheet10.Protect
            End With
            Next i
    End With
    
    Set objMySheet = Nothing
    Application.ScreenUpdating = True
End Sub

I can upload and share my workbooks to Send Anywhere if needed. Thank you so much to anyone that will take the time to help me out.

Oh, in case it's relevant, I'm getting an error on this line that says Object doesn't support this property or method.
Code:
Set wsname = wkbMain.Sheet("Setup").Cells(i + 28, "B").Value
 
Last edited:

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
Code:
Set wsname = wkbMain.Sheet("Setup").Cells(i + 28, "B").Value

Delete the .Value from the end. Or delete the Set from the front. If you use Set, you have to be initializing an Object Variable and the .Value indicates a data type, so it will throw the error. But be careful, whichever correction you make that the variable is used correctly in the rest of the code or it will cause errors there also. I see that further down in the code you have 'sname' variable assigned to the same range value. Maybe you don't even need the line that is causing the error.
 
Last edited:
Upvote 0
Code:
Set wsname = wkbMain.Sheet("Setup").Cells(i + 28, "B").Value

Delete the .Value from the end. Or delete the Set from the front. If you use Set, you have to be initializing an Object Variable and the .Value indicates a data type, so it will throw the error. But be careful, whichever correction you make that the variable is used correctly in the rest of the code or it will cause errors there also. I see that further down in the code you have 'sname' variable assigned to the same range value. Maybe you don't even need the line that is causing the error.

Thank you for replying so quickly. I tried both ways that you suggested and both give me the same error as before.
 
Upvote 0
I removed that line and edited the code to what it was before adding it and now I get an error at this line:
Code:
ThisWorkbook.Sheets(Sheets.Count).Name = .Cells(i + 28, "B").Value

This is the line that should set what to name the copied sheet. I've tried removing ThisWorkBook completely and replacing it with wkbMain and neither work.
 
Upvote 0
I removed that line and edited the code to what it was before adding it and now I get an error at this line:
Code:
ThisWorkbook.Sheets(Sheets.Count).Name = .Cells(i + 28, "B").Value

This is the line that should set what to name the copied sheet. I've tried removing ThisWorkBook completely and replacing it with wkbMain and neither work.

It is hard to tell. Is the statement inside a 'With' statement? The period in front of Cells indicates that it is. If it is not, you need a parent object for .Cells( ). Remember, Cells is a property and not an object. It only takes on an object type action when qualified by a sheet object.
 
Last edited:
Upvote 0
It is hard to tell. Is the statement inside a 'With' statement? The period in front of Cells indicates that it is. If it is not, you need a parent object for .Cells( ). Remember, Cells is a property and not an object. It only takes on an object type action when qualified by a sheet object.

It is in a With statement:
Code:
 With wkbMain
                .Sheets("C-130 MTP Template").Copy After:=Sheets(Sheets.Count)
                ThisWorkbook.Sheets(Sheets.Count).Name = .Cells(i + 28, "B").Value

wkbMain is the user workbook, not the workbook where the code is. I'm guessing that is my problem somehow. I don't know how to reference the user workbook.
 
Upvote 0
It is in a With statement:
Rich (BB code):
 With wkbMain
                .Sheets("C-130 MTP Template").Copy After:=Sheets(Sheets.Count)
                ThisWorkbook.Sheets(Sheets.Count).Name = .Cells(i + 28, "B").Value
wkbMain is the user workbook, not the workbook where the code is. I'm guessing that is my problem somehow. I don't know how to reference the user workbook.

You need to qualify your Sheets with the parent workbook. I Assume it is ThisWorkbook, since the next line uses that.
'ThisWorbook.Sheets(ThisWorkbook.Sheets.Count)'.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,215,562
Messages
6,125,546
Members
449,237
Latest member
Chase S

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top