Solved

Excel Master Time sheet gives runtime error '5'

Posted on 2008-06-25
9
666 Views
Last Modified: 2012-05-05
We have an Excel Master Time Sheet that we have custom macros that we created and they work fine but if anyone opens up any other Excel file, both Excel files will give a message saying "Run-time error '5' and Invalid procedure call or argument."  It as if the macros see another Excel file open and try to run something.  We did not create the macros and this is a little new for us so anyone that can assist us and helping to allow us to open multiple Excel time sheets at once without the error is greatly appreciated.  
Sub Clear_Cells()
 

    Dim Count As Integer

    Dim LstLine As Boolean

    Dim LstCell As Range

    Dim Flag, FstAdd, LstAdd, Rnge As String

    

    Count = 4

    LstLine = False

    Flag = "last line"

    FstAdd = "D4"

    

    ActiveSheet.Unprotect

    

    Do Until LstLine = True

        Set LstCell = Cells(Count, 2).Find(Flag)

        If Not LstCell Is Nothing Then

            LstAdd = LstCell.Address

            LstLine = True

        Else

            Count = Count + 1

        End If

    Loop

    

    If Len(LstAdd) = 4 Then

        LstAdd = Right(LstAdd, 1)

    ElseIf Len(LstAdd) = 5 Then

        LstAdd = Right(LstAdd, 2)

    ElseIf Len(LstAdd) = 6 Then

        LstAdd = Right(LstAdd, 3)

    Else

        LstAdd = Right(LstAdd, 4)

    End If

    

    LstAdd = "J" & LstAdd

    Rnge = FstAdd & ":" & LstAdd

    Range(Rnge).Select

    Selection.ClearContents

    Range("A3").Activate
 

    ActiveSheet.Protect
 

End Sub
 

Sub TimeSheet_Print()

    

    ActiveSheet.Unprotect

    

    Range("K4").Select

    Selection.AutoFilter

    Selection.AutoFilter Field:=11, Criteria1:="<>"

    Columns("A:L").Select

    ActiveWindow.Selection.PrintOut Copies:=1

    Selection.AutoFilter

    ActiveWindow.ScrollColumn = 1

    Range("B2").Select

    CommandBars("TEST").Controls(1).State = msoButtonUp
 

    ActiveSheet.Protect
 

End Sub
 

Private Sub Workbook_BeforeClose(Cancel As Boolean)

    

    Application.CommandBars("TEST").Delete
 

End Sub
 

Private Sub Workbook_Open()

    

    Set mybar = Application.CommandBars _

        .Add(Name:="TEST", Position:=msoBarTop)

        mybar.Visible = True

        mybar.Enabled = True

    Set myCntrl = mybar.Controls.Add(Type:=msoControlButton, ID:=1)

    With myCntrl

        .Style = msoButtonIconAndCaption

        .OnAction = "Clear_Cells"

        .Caption = "Clear Cells"

        .Visible = True

    End With

    Set myCntrl = mybar.Controls.Add(Type:=msoControlButton, ID:=1)

    With myCntrl

        .Style = msoButtonIconAndCaption

        .OnAction = "TimeSheet_Print"

        .Caption = "Print Timesheet"

        .Visible = True

    End With

    Set myCntrl = mybar.Controls.Add(Type:=msoControlButton, ID:=1)

    With myCntrl

        .Style = msoButtonIconAndCaption

        .OnAction = "JobFinder"

        .Caption = "Job Finder"

        .Visible = True

    End With

    Set myCntrl = mybar.Controls.Add(Type:=msoControlButton, ID:=1)

    With myCntrl

        .Style = msoButtonIconAndCaption

        .OnAction = "ClearJobFinder"

        .Caption = "Clear Job Finder Page"

        .Visible = True

    End With
 

    Sheets("TimeSheet").Select

    Range("B1").Select

    

    

    ActiveSheet.Protect
 
 

End Sub

Open in new window

0
Comment
Question by:regsamp
  • 5
  • 4
9 Comments
 
LVL 6

Expert Comment

by:Jaffa0
ID: 21867183
So basically, when you have this open, it is capturing the opening of another workbook?
Is that the only code? I'm thinking that there may be a class module capturing events - it will have something like

Private WithEvents App As Application

Private Sub Class_Initialize()
    Set App = Application
End Sub

in it.
0
 

Author Comment

by:regsamp
ID: 21867495
Yes, when we have it open, it is capturing the opening of another workbook.  The rest of the code that I found in other sections is listed below.  


Dim TextSearch As Boolean

Private Sub cmdSearch_Click()

    SearchJobs
   
End Sub
Sub SearchJobs()
   
    Dim strSQL As String, sConn As String, oCn As ADODB.Connection, rsRecs As ADODB.Recordset
    Dim CurRow As Integer, WkSht As Excel.Worksheet
   
    On Error GoTo Error_Handler
   
    sConn = "Provider=SQLOLEDB;Data Source=test;User ID=sa;Password=;Initial Catalog=dwma"
   
    Set WkSht = Application.Sheets("JobFinder")
    WkSht.Select
    Columns("A:C").Select
    Selection.Clear

    Selection.Borders(xlDiagonalDown).LineStyle = xlNone
    Selection.Borders(xlDiagonalUp).LineStyle = xlNone
    With Selection.Borders(xlEdgeLeft)
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = xlAutomatic
    End With
    With Selection.Borders(xlEdgeTop)
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = xlAutomatic
    End With
    With Selection.Borders(xlEdgeBottom)
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = xlAutomatic
    End With
    With Selection.Borders(xlEdgeRight)
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = xlAutomatic
    End With
    With Selection.Borders(xlInsideVertical)
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = xlAutomatic
    End With
    With Selection.Borders(xlInsideHorizontal)
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = xlAutomatic
    End With
   
    strSQL = "SELECT p.ProjectsID As ProjectsID, p.ProjNum As ProjNum, t.ProjNum As TaskNum, p.ProjName As ProjName, t.Description As Description," & _
    " p.Status As Status, t.Status As tStatus, p.Exp, p.Bill_Level As Bill_Level, t.Bill_Level As tBill_Level," & _
    " p.Prin As Prin, p.PM As PM, p.PSM As PSM, t.Prin As tPrin, t.PM As tPM, t.PSM As tPSM, c.Client_Name As Client_Name, p.Exp As Exp" & _
    " FROM ((Projects p LEFT JOIN Clients c ON p.ClientID = c.ClientID)" & _
    " LEFT JOIN Tasks t ON p.ProjectsID = t.ProjectID) WHERE p.Status = 'A' AND t.Status = 'A'"
    If TextSearch Then
            strSQL = strSQL & " AND p.ProjNum LIKE '" & txtSearch & "%' OR t.ProjNum LIKE '" & txtSearch & "%' OR p.ProjName LIKE '" & txtSearch & "%' OR t.Description LIKE '%" & txtSearch & "%'"
    Else
        If optDateSearch.Value = True And optAfter.Value = True Then
            strSQL = strSQL & " AND (p.date_assigned >= '" & txtDate & "')"
        ElseIf optDateSearch.Value = True And optBefore.Value = True Then
            strSQL = strSQL & " AND (p.date_assigned <= '" & txtDate & "')"
        End If
    End If
    strSQL = strSQL & " ORDER BY p.ProjNum, t.ProjNum"
   
    Set oCn = New ADODB.Connection
   
    oCn.Open sConn
    Set rsRecs = New ADODB.Recordset
    rsRecs.CursorLocation = adUseClient
    rsRecs.Open strSQL, oCn, 1, 2, 1
   
    CurRow = 1
    Do Until rsRecs.EOF
        If rsRecs("TaskNum") = "" Then
            WkSht.Cells(CurRow, 1) = rsRecs("ProjNum")
        Else
            WkSht.Cells(CurRow, 1) = rsRecs("TaskNum")
        End If
        WkSht.Cells(CurRow, 2) = rsRecs("ProjName") & " " & rsRecs("Description")
        CurRow = CurRow + 1
        rsRecs.MoveNext
    Loop
    Sheets("JobFinder").Select
    Unload Me
    Exit Sub
   
Error_Handler:
    MsgBox "Error in entry. Please type the correct date format." & vbCrLf & "Error #" & Err.Number & vbCrLf & "Description: " & Err.Description, vbExclamation + vbOKOnly, "Error in entry"

End Sub

Private Sub optDateSearch_Click()

    txtSearch.Enabled = False
    lblSearch.Enabled = False
    optAfter.Enabled = True
    optBefore.Enabled = True
    txtDate.Enabled = True
    lblDate.Enabled = True
    TextSearch = False
   
End Sub

Private Sub optTextSearch_Click()

    txtSearch.Enabled = True
    lblSearch.Enabled = True
    optAfter.Enabled = False
    optBefore.Enabled = False
    txtDate.Enabled = False
    lblDate.Enabled = False
    TextSearch = True

End Sub

Private Sub UserForm_Activate()
   
    TextSearch = True

End Sub

Sub JobFinder()
    frmJobFinder.Show
End Sub
Sub ClearJobFinder()
   
    Sheets("JobFinder").Select
    Columns("A:B").Select
    Selection.ClearContents
    Selection.QueryTable.Delete
    Range("A1").Select
    Sheets("TimeSheet").Select
    Range("B1").Select

End Sub

0
 
LVL 6

Expert Comment

by:Jaffa0
ID: 21867517
Okay, there doesn't seem to be anything there to cause that behaviour. Can you post your workbook? If not, when you get the runtime error, press debug and let us know which line it is falling over on.
0
 

Author Comment

by:regsamp
ID: 21867563
This is the line it goes on when we press debug:
Set mybar = Application.CommandBars _
        .Add(Name:="DWMA", Position:=msoBarTop)
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 6

Accepted Solution

by:
Jaffa0 earned 500 total points
ID: 21867584
Does the second workbook you open contain the same worksheet_open code as the first? I suspect you are trying to add a command bar that already exists.
0
 

Author Comment

by:regsamp
ID: 21867678
Yes, the second workbook contains the same worksheet_open code as the first.  Is there anyway around it or if it is has the same code then it it will have this behavior?  
0
 
LVL 6

Expert Comment

by:Jaffa0
ID: 21867746
You are trying to add a toolbar that already exists. You could give the toolbar a different name - you'd have two that look the same but control the two different workbooks. You could check if the bar exists and only create it if it doesn't, and then keep a record of this so you don't destroy it if the workbook is closed - but then you wouldn't have a bar to control that second book. It really depends on the purpose of having the same bar created by two different workbooks.
0
 

Author Comment

by:regsamp
ID: 21867784
Well, the toolbar is an option that is on every time sheet so I know there must be away but I don't enough to tell Excel if another file opens with the same toobar, not to create?  I guess it would be easier that some of the Manager's had time sheets that did not have the toobar option and could use those in conjunction with the ones that did have it.  
0
 

Author Comment

by:regsamp
ID: 21868364
Well, you have helped me out Jaffa0.  Thank you for your help.  
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Excel Error per WinZip Add-In 9 31
Sending Attachment via CDO 3 22
Add macros on Open 9 32
Cells protection in Excel 7 22
Introduction This Article briefly covers methods of calculating the NPV and IRR variants in Excel as well as the limitations in calculating and interpreting IRR results. Paraphrasing Richard Shockley, author of my favourite finance reference tex…
Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
The viewer will learn how to simulate a series of coin tosses with the rand() function and learn how to make these “tosses” depend on a predetermined probability. Flipping Coins in Excel: Enter =RAND() into cell A2: Recalculate the random variable…
The view will learn how to download and install SIMTOOLS and FORMLIST into Excel, how to use SIMTOOLS to generate a Monte Carlo simulation of 30 sales calls, and how to calculate the conditional probability based on the results of the Monte Carlo …

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now