+ Reply to Thread
Results 1 to 3 of 3

Excel Crashes when I Run the function

  1. #1
    Registered User
    Join Date
    01-18-2019
    Location
    Houston, Texas
    MS-Off Ver
    1812
    Posts
    16

    Excel Crashes when I Run the function

    Please help every time i run this function Excel 2016 crashes and excel restarts no errors given before the crash.






    'Function the make a new incident report
    Public Sub incidentDoc(lastSheet As String)

    Call ApplicationActions(True) 'Disable all Excel Automatic Actions

    Dim fileDir As String

    fileDir = FileSelection.BrowseFolders 'Save file Directory

    If (fileDir = vbNullString) Then 'If user cancled the file dialog

    Exit Sub 'Exit funtion

    End If

    Dim jobNumber As String
    Dim RigName As String
    Dim clientName As String
    Dim eventDate As String

    Dim msWord As Word.Application
    Dim msWordDoc As Word.Document
    Dim newLine As String

    newLine = Chr(11)

    Set msWord = CreateObject("Word.Application")
    msWord.Visible = False

    Set msWordDoc = msWord.Documents.Add

    'msWordDoc.Name = lastSheet

    Dim incidentType As String

    Dim rowIdx As Integer

    For rowIdx = 50 To 61

    If (CBool(Sheets(lastSheet).Range("D" & rowIdx).Value)) Then

    If (incidentType <> vbNullString) Then incidentType = incidentType & " / "

    incidentType = incidentType & UCase(Sheets(lastSheet).Range("A" & rowIdx).Value)

    End If

    Next rowIdx

    If (incidentType = vbNullString) Then incidentType = "TBD"

    Dim serialList As String

    For rowIdx = 10 To 11

    If (Sheets(lastSheet).Range("B" & rowIdx).Value <> vbNullString) Then

    If (serialList <> vbNullString) Then serialList = serialList & " / "

    serialList = serialList & UCase(Sheets(lastSheet).Range("B" & rowIdx).Value)

    'If (Sheets(lastSheet).Range("D" & rowIdx).Value <> vbNullString) Then

    'serialList = serialList & " (" & Sheets(lastSheet).Range("D" & rowIdx).Value & ")"

    'Else

    'serialList = serialList & " (N/A)"

    'End If

    End If

    Next rowIdx

    For rowIdx = 9 To 11

    If (Sheets(lastSheet).Range("F" & rowIdx).Value <> vbNullString) Then

    If (serialList <> vbNullString) Then serialList = serialList & " / "

    serialList = serialList & UCase(Sheets(lastSheet).Range("F" & rowIdx).Value)

    'If (Sheets(lastSheet).Range("H" & rowIdx).Value <> vbNullString) Then

    'serialList = serialList & " (" & Sheets(lastSheet).Range("H" & rowIdx).Value & ")"

    'Else

    'serialList = serialList & " (N/A)"

    'End If

    End If

    Next rowIdx

    If (serialList = vbNullString) Then serialList = "N/A"

    Dim fieldPersonnel As String

    For rowIdx = 5 To 6

    If (Sheets(lastSheet).Range("F" & rowIdx).Value <> vbNullString) Then

    If (fieldPersonnel <> vbNullString) Then fieldPersonnel = fieldPersonnel & " / "

    fieldPersonnel = fieldPersonnel & Sheets(lastSheet).Range("F" & rowIdx).Value

    End If

    Next rowIdx

    If (fieldPersonnel = vbNullString) Then fieldPersonnel = "N/A"

    With msWordDoc
    .Content.InsertAfter "Client: " & Sheets(lastSheet).Range("B5").Value & newLine
    If (Sheets(lastSheet).Range("B5").Value <> vbNullString) Then clientName = Replace(CleanFileName(Sheets(lastSheet).Range("B5").Value), " ", "_")

    .Content.InsertAfter "Well Name: " & Sheets(lastSheet).Range("B6").Value & newLine
    .Content.InsertAfter "Job #: " & Sheets(lastSheet).Range("F4").Value & newLine
    If (Sheets(lastSheet).Range("F4").Value <> vbNullString) Then jobNumber = Replace(CleanFileName(Sheets(lastSheet).Range("F4").Value), " ", "_")

    .Content.InsertAfter "Rig Name & Number: " & Sheets(lastSheet).Range("B7").Value & newLine
    If (Sheets(lastSheet).Range("B7").Value <> vbNullString) Then RigName = Replace(CleanFileName(Sheets(lastSheet).Range("B7").Value), " ", "_")

    .Content.InsertAfter "County, State: " & Sheets(lastSheet).Range("B8").Value
    .Content.InsertParagraphAfter
    .Content.InsertAfter "Incident Date: " & Format(Sheets(lastSheet).Range("B4").Value, "Short Date") & newLine
    If (Sheets(lastSheet).Range("B4").Value <> vbNullString) Then eventDate = CStr(Format(Sheets(lastSheet).Range("B4").Value, "MM_DD_YYYY"))

    .Content.InsertAfter "Incident Time: " & Format(Sheets(lastSheet).Range("D4").Value, "hh:mm") & newLine
    .Content.InsertAfter "Incident Type: " & incidentType
    .Content.InsertParagraphAfter
    .Content.InsertAfter "Tool Serial Number: " & serialList & newLine
    .Content.InsertAfter "MWD monel OD/ID: " & Sheets(lastSheet).Range("F42").Value & newLine
    .Content.InsertAfter "Fin Gauge: " & Sheets(lastSheet).Range("D48").Value & newLine
    .Content.InsertAfter "Temp.: " & Sheets(lastSheet).Range("B47").Value & newLine
    .Content.InsertAfter "Lockdown Type: " & Sheets(lastSheet).Range("B48").Value & newLine
    .Content.InsertAfter "Axial Isolator SN: " & Sheets(lastSheet).Range("B49").Value & newLine
    .Content.InsertAfter "Axial Isolator Tupe: " & Sheets(lastSheet).Range("D49").Value & newLine
    .Content.InsertAfter "Agitator Placement above MWD: " & Sheets(lastSheet).Range("F48").Value
    If (UCase(Sheets(lastSheet).Range("F48").Value) = "YES") Then
    .Content.InsertAfter newLine & "Agitator Brand: " & Sheets(lastSheet).Range("H48").Value & newLine
    .Content.InsertAfter "Agitator Distance To MWD: " & Sheets(lastSheet).Range("F49").Value
    End If
    .Content.InsertParagraphAfter
    .Content.InsertAfter "MWD Engineers: " & fieldPersonnel & newLine

    If (Sheets(lastSheet).Range("F8").Value <> vbNullString) Then
    .Content.InsertAfter "Downtime: " & Sheets(lastSheet).Range("F8").Value & newLine
    Else
    Sheets(lastSheet).Range("F8").Value = "TBD"
    End If
    .Content.InsertAfter "Reporter: " & newLine
    .Content.InsertAfter "MWD Coordinator: " & Sheets(lastSheet).Range("B9").Value

    If (Sheets(lastSheet).Range("A21").Value <> vbNullString) Then
    .Content.InsertParagraphAfter
    .Content.InsertAfter "Description of issue: " & Sheets(lastSheet).Range("A21").Value
    End If

    If (Sheets(lastSheet).Range("A27").Value <> vbNullString) Then
    .Content.InsertParagraphAfter
    .Content.InsertAfter "Corrective Action Taken: " & Sheets(lastSheet).Range("A27").Value
    End If

    If (Sheets(lastSheet).Range("B35").Value <> vbNullString) Then
    .Content.InsertParagraphAfter
    Dim currDepthStr As String

    currDepthStr = "Current Depth: " & Sheets(lastSheet).Range("B35").Value & "'"

    If (Sheets(lastSheet).Range("B41").Value <> vbNullString) Then currDepthStr = currDepthStr & ", Flow " & Sheets(lastSheet).Range("B41").Value

    If (Sheets(lastSheet).Range("B37").Value <> vbNullString) Then currDepthStr = currDepthStr & ", Mud Type " & Sheets(lastSheet).Range("B37").Value

    If (Sheets(lastSheet).Range("B38").Value <> vbNullString) Then currDepthStr = currDepthStr & ", Mud Wt. " & Sheets(lastSheet).Range("B38").Value & " GPM"

    If (Sheets(lastSheet).Range("F7").Value <> vbNullString) Then currDepthStr = currDepthStr & ", Run # " & Sheets(lastSheet).Range("F7").Value

    If (Sheets(lastSheet).Range("F7").Value <> vbNullString) Then currDepthStr = currDepthStr & ", Run # " & Sheets(lastSheet).Range("F7").Value

    If (Sheets(lastSheet).Range("F45").Value <> vbNullString) Then currDepthStr = currDepthStr & ", " & Sheets(lastSheet).Range("F45").Value & " circ hours"

    If (Sheets(lastSheet).Range("F44").Value <> vbNullString) Then currDepthStr = currDepthStr & ", " & Sheets(lastSheet).Range("F44").Value & " pwr hours"

    .Content.InsertAfter currDepthStr

    End If

    Dim saveFile As String

    If (Right(fileDir, 1) <> "\") Then fileDir = fileDir & "\"

    #If VBA7 Or VBA8 Then

    saveFile = fileDir & Replace(lastSheet, " ", "_") & "_" & jobNumber & "_" & clientName & "_" & RigName & "_" & eventDate & ".docx"

    #Else

    saveFile = fileDir & Replace(lastSheet, " ", "_") & "_" & jobNumber & "_" & clientName & "_" & RigName & "_" & eventDate & ".doc"

    #End If

    If (FileExists(saveFile)) Then

    If (MsgBox("Cannot save file it already existis in that location." & vbCrLf _
    & "Would you like to overwrite it?" & vbCrLf & vbCrLf _
    & "If you choose no the file will be opened but not saved", _
    vbYesNo, "File Existis") = vbYes) Then

    Application.DisplayAlerts = False

    .SaveAs saveFile

    Application.DisplayAlerts = True

    End If

    Else

    .SaveAs saveFile

    End If

    End With

    msWord.Visible = True

    Call ApplicationActions(False) 'Enable all Excel Automatic Actions

    End Sub

  2. #2
    Forum Expert Bob Phillips's Avatar
    Join Date
    09-03-2005
    Location
    Wessex
    MS-Off Ver
    Office 2003, 2010, 2013, 2016, 365
    Posts
    3,284

    Re: Excel Crashes when I Run the function

    Put a breakpoint in the first line, run it and step through the code to find how far it gets.

  3. #3
    Forum Guru Sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2016 | 2019
    Posts
    13,520

    Re: Excel Crashes when I Run the function

    Hi Must06

    Please take a moment to read our forum rules...here
    Your post does not comply with Rule # 2
    2. Programming code must be enclosed in code tags to improve readability. (A, Z)
    Please Login or Register  to view this content.
    So...Edit your post...Highlight the code and press the # button

    This post should receive no further attention until OP has complied...
    Good Luck...
    I don't presume to know what I am doing, however, just like you, I too started somewhere...
    One-day, One-problem at a time!!!
    If you feel I have helped, please click on the [★ Add Reputation] to left of post window...
    Also....Add a comment if you like!!!!
    And remember...Mark Thread as Solved...
    Excel Forum Rocks!!!

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Macro crashes intermittently - believe on the listrows.add function
    By erblues in forum Excel Programming / VBA / Macros
    Replies: 37
    Last Post: 09-06-2018, 03:50 AM
  2. Replies: 1
    Last Post: 06-15-2018, 02:36 PM
  3. Code works in Windows excel 2003 and 2010 but crashes in Mac excel 2011
    By kiweed in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 11-19-2012, 08:46 PM
  4. select function crashes the excel
    By assaf1978 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 05-12-2011, 06:01 AM
  5. Using Sort function in macro crashes
    By sfax39 in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 06-04-2009, 03:47 PM
  6. [SOLVED] Help with function that crashes Excel
    By Ken Loomis in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 07-13-2005, 09:05 PM
  7. Excel 2003 crashes loading excel files created Excel 2000
    By Jeff Lewin Australia in forum Excel General
    Replies: 0
    Last Post: 06-27-2005, 12:05 AM

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