+ Reply to Thread
Results 1 to 7 of 7

[Solved]On Click command button function

Hybrid View

  1. #1
    Registered User
    Join Date
    12-02-2010
    Location
    UK
    MS-Off Ver
    Excel 2007
    Posts
    5

    Question [Solved]On Click command button function

    I'm trying to create a macro for a command button that when clicked, will get the job number from that row and look for a file for that job. If it does not exist I want it to copy from a template and save with a new name, otherwise just open the file.

    However, I cannot seem to work out how to get hold of the information for the command button that calls the macro. This is what I have so far:

    ShapeExists, checks if a button on that row exists, if not it creates one if a cell in that row has changed.

    JobButton is what I am having problems with.

        Public Function ShapeExists(OnSheet As Object, Name As String) As Boolean
                
            On Error GoTo ErrShapeExists
            If Not OnSheet.Shapes(Name) Is Nothing Then
                ShapeExists = True
            End If
        ErrShapeExists:
            Exit Function
            
        End Function
        
        Private Sub Worksheet_Change(ByVal Target As Excel.Range)
        Dim buttonName As String
        buttonName = (Target.Row - 1)
            If Not ShapeExists(ActiveSheet, buttonName) Then
              If Range("O" & Target.Row).Value = "" And Target.Column <= 14 And Target.Row > 1 Then
                    ActiveSheet.Buttons.Add(910.5, Range("O" & Target.Row).Top, 80, 20).Select
                    Selection.Name = buttonName
                    Selection.OnAction = "Sheet1.JobButton"
                    ActiveSheet.Shapes(buttonName).Select
                    Selection.Characters.Text = "Open Job"
              End If
            End If
        End Sub
        
        Private Sub JobButton()
        Dim newText As String
        ActiveSheet.Shapes(Application.Caller).Select
        
        If Range("N" & Selection.TopLeftCell.Row).Value <> "" Then
            newText = "Job " & Range("N" & Selection.TopLeftCell.Row).Value
            Dim checkFilename As String
            Dim check As String
            check = "N" & Selection.TopLeftCell.Row
            checkFilename = newText & ".xlsm"
            If Dir(checkFilename) <> "" Then
            Workbooks.Open (newText)
            Else
            Dim SrcBook As Workbook
            Set SrcBook = ThisWorkbook
            Dim NewBook As Workbook
            NewBook = Workbooks.Open("Job Template.xlsm")
            SrcBook.Worksheets(1).Range("D" & Selection.TopLeftCell.Row).Copy
            NewBook.Worksheets(2).Range("B15").PasteSpecial
                With NewBook
                    .Title = newText
                    .Subject = newText
                    .SaveAs Filename:=newText
                End With
            End If
        Else
        ErrMsg:
        MsgBox ("Job Should always have a number."), , "NO JOB NUMBER"
        
        End If
        End Sub
    As you can see I am currently trying ActiveSheet.Shapes(Application.Caller).Select, this is causing a "Run-time error '13': Type mismatch".

    Any help would be much appreciated, thank you!
    Last edited by Aitchy; 12-02-2010 at 03:13 PM.

  2. #2
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,481

    Re: On Click command button function

    In a test file your code works, until it tries to open template.

    Where in your code does the error occur?
    can you post example file to demonstrate the problem
    Cheers
    Andy
    www.andypope.info

  3. #3
    Registered User
    Join Date
    12-02-2010
    Location
    UK
    MS-Off Ver
    Excel 2007
    Posts
    5

    Re: On Click command button function

    Job Tracking is the main file, and I'm trying to test the command buttons in column O.

    When I click the "Open Job" button in the Job Tracking file attached above, I get an error 400 message.

    So stepping through the JobButton method I get a Run-time error'13': Type mismatch on :

    ActiveSheet.Shapes(Application.Caller).Select

  4. #4
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,481

    Re: On Click command button function

    The first issue was that the code you posted was not the code used in your example files.
    The use of the ME object within a sheet object will refer to the sheet not the button that was just pressed.

    This assigns the shape to a object variable for use within the code.

    Private Sub JobButton()
    Dim newText As String
    Dim shpCaller As Shape
    
    Set shpCaller = ActiveSheet.Shapes(Application.Caller)
    
    If Range("N" & shpCaller.TopLeftCell.Row).Value <> "" Then
        newText = "Job " & Range("N" & shpCaller.TopLeftCell.Row).Value
        Dim checkFilename As String
        Dim check As String
        check = "N" & shpCaller.TopLeftCell.Row
        checkFilename = newText & ".xlsm"
        If Dir(checkFilename) <> "" Then
        Workbooks.Open (newText)
        Else
        Dim SrcBook As Workbook
        Set SrcBook = ThisWorkbook
        Dim NewBook As Workbook
        Set NewBook = Workbooks.Open("Job Template.xlsm")
        SrcBook.Worksheets(1).Range("D" & shpCaller.TopLeftCell.Row).Copy
        NewBook.Worksheets(2).Range("B15").PasteSpecial
            With NewBook
                .Title = newText
                .Subject = newText
                .SaveAs Filename:=newText
            End With
        End If
    Else
    ErrMsg:
    MsgBox ("Job Should always have a number."), , "NO JOB NUMBER"
    
    End If
    End Sub

  5. #5
    Registered User
    Join Date
    12-02-2010
    Location
    UK
    MS-Off Ver
    Excel 2007
    Posts
    5

    Re: On Click command button function

    Ah yes sorry, I have changed my attachment.

    Thanks for the code above, but I still get the same error 400 when I click the button and run-time error 13 when stepping through the method.

  6. #6
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,481

    Re: On Click command button function

    I do not get a 400 error. I do get a 1004 error if the current directory is not the one with the template file in it.

    Adding the exact folder would help.
        Set NewBook = Workbooks.Open(thisworkbook.path & "\Job Template.xlsm")
    You can no simply step into and through the macro as the Application.Caller value will cause an error.
    You can trap for that

    If IsError(Application.Caller) Then
        MsgBox "Not called via OnAction"
        Exit Sub
    End If

  7. #7
    Registered User
    Join Date
    12-02-2010
    Location
    UK
    MS-Off Ver
    Excel 2007
    Posts
    5

    Re: On Click command button function

    That was the problem, thank you so much for the help!

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1