VBA crash Excel

Peltz

Board Regular
Joined
Aug 30, 2011
Messages
87
Hi there. The code I've compild her crashes Excel, and I can't figure out whats wrong with it. I can open excel excel an other codes just fine. Isuspect it has something to do with sending multipe mails. Ive rand the code through the step through method, and it seems to work. Any help troubleshoting it would be greatly appreciated. I would happily send the full document if needed. Thanks!

Code:
Sub fristbrudd_spesifisert()
Dim ansatt As String
Dim sisterad As Integer
Dim sisterad1 As Integer
Dim rng As Range
Dim OutApp As Object
Dim OutMail As Object
Dim Siste As Long
Dim adresse As String
Dim i As Integer
Dim k As Integer
MsgBox "Kan ta noe tid å generere rapporter, finn noe kaffe", , "Oppfordring"
Sheets("Forefallende").Range("P2:U300").ClearContents
sisterad = Sheets("Pasientforløp").Range("A3000").End(xlUp).Row
sisterad1 = Sheets("Forefallende").Range("H1000").End(xlUp).Row
For k = 3 To sisterad1
ansatt = Sheets("Forefallende").Cells(k, 8) 'hvor navn for den som søkes hentes
adresse = Sheets("Forefallende").Cells(k, 9)

For i = 9 To sisterad
'fiks kopi
If Sheets("Pasientforløp").Cells(i, 5) = ansatt And Sheets("Pasientforløp").Cells(i, 5) <> "" And Sheets("Pasientforløp").Cells(i, 9) <> True And Now > Sheets("Pasientforløp").Cells(i, 7).Value Then
    Sheets("Forefallende").Range("P100").End(xlUp).Offset(1, 0) = Sheets("Pasientforløp").Cells(i, 10)
    Sheets("Forefallende").Range("Q100").End(xlUp).Offset(1, 0) = Sheets("Pasientforløp").Cells(i, 2)
    Sheets("Forefallende").Range("R100").End(xlUp).Offset(1, 0) = Sheets("Pasientforløp").Cells(i, 7)
    Sheets("Forefallende").Range("S100").End(xlUp).Offset(1, 0) = Sheets("Pasientforløp").Cells(i, 5)


End If
Next i
If Sheets("Forefallende").Range("P2") = "" Then

GoTo 10
End If

Siste = Sheets("Forefallende").Range("P1").CurrentRegion.Rows.Count
Set rng = Sheets("Forefallende").Range("P2: S" & Siste)

    On Error GoTo 0
    If rng Is Nothing Then
        MsgBox "The selection is not a range or the sheet is protected" & _
               vbNewLine & "please correct and try again.", vbOKOnly
        Exit Sub
    End If
    With Application
        .EnableEvents = False
        .ScreenUpdating = False
    End With
    Set OutApp = CreateObject("Outlook.Application")
    Set OutMail = OutApp.CreateItem(0)
    On Error Resume Next
    With OutMail
    .Display
    End With
    signature = OutMail.HTMLbody
    With OutMail
        .To = adresse
        .CC = ""
        .BCC = ""
        .Subject = "Oppfølging pakkeforløp"
        .HTMLbody = "<font size=""2"" face=""Calibri"" color=""black"">" & "Hei " & ansatt & "<br><br><br>" & _
        "Disse oppgavene er ikke fullførte eller avhuket i forløpsskjemaet" & vbNewLine & RangetoHTML(rng) & signature
        '.Send
        .Display
    End With
    On Error GoTo 0
    With Application
        .EnableEvents = True
        .ScreenUpdating = True
    End With
    Set OutMail = Nothing
    Set OutApp = Nothing
10
If Sheets("Forefallende").Cells(2, 16) <> "" Then
Sheets("Forefallende").Range("O2") = 1
Sheets("Forefallende").Range("P2:U300").ClearContents
End If
Next k
If Sheets("Forefallende").Range("O2") = "" Then
MsgBox "Ingen frister utløpt", , "Søk ferdig"
Else: Sheets("Forefallende").Range("O2").ClearContents
End If


End Sub
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
I am having a quick look. One thing to note is the following
Code:
[COLOR=#333333]Siste = Sheets("Forefallende").Range("P1").CurrentRegion.Rows.Count
[/COLOR][COLOR=#333333]Set rng = Sheets("Forefallende").Range("P2: S" & Siste)
[/COLOR]

If Siste is 0 then the next line will error as you cannot have row 0
 
Upvote 0
Thanks. I've got a header (not table) in that row, so I dont think that is the problem. The problem seems to occure when trying to generate the third mail.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,216,119
Messages
6,128,946
Members
449,480
Latest member
yesitisasport

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