Stop second part of VBA code

KlausW

Active Member
Joined
Sep 9, 2020
Messages
378
Office Version
  1. 2016
Platform
  1. Windows
Hi, I have a little challenge. I use this VBA code to save the 3sheets and mail them. It works really well. Now I want to add a little change but cannot figure out how to put it in.

The change shout be, that if I make a X in cell K1 in the sheet “Fast besætning” the VBA code shall only save the fil and nor send it by mail.

Some that can help get it running.
All help will be appreciated.
Best Regards
Klaus W
VBA Code:
Sub Rektangelafrundedehjørner5_Klik()

 Dim Fname  As String, ws As Worksheet, wbk As Workbook
 
 Set wbk = ThisWorkbook
 
    Fname = Sheets("Fast besætning").Range("g1").Value
    Sheets(Array("1.deling", "2.deling", "3.deling")).Copy
    
    For Each ws In ActiveWorkbook.Worksheets
        With ws.UsedRange
            .Value = .Value
        End With
    Next ws
        
    With ActiveWorkbook
        Application.Dialogs(xlDialogSaveAs).Show Fname, 51
    End With
    
    With wbk
        .Activate
        .Sheets("Fast besætning").Select
    End With
    
    Mail_workbook_Outlook
End Sub

Sub Mail_workbook_Outlook()
'her er koden til at sende mail
    Dim Edress As String, Subj As String
    Dim OutlookOBJ As Object, mItem As Object
    '---------------------------------------------'
    Set OutlookOBJ = CreateObject("Outlook.Application")
    Set mItem = OutlookOBJ.CreateItem(0)
    
        On Error Resume Next

    With mItem
    
        .To = Sheets("Fast besætning").Range("g2").Value & "; " & Range("g3").Value & "; " & Range("g4").Value & "; " & Range("g5").Value & "; " & Range("g6").Value & "; " & Range("g7").Value & "; " & Range("g8").Value & "; " & Range("g9").Value & "; " & Range("g10").Value & "; " & Range("g11").Value & "; " & Range("g12").Value & "; " & Range("g13").Value & "; " & Range("g14").Value & "; " & Range("g15").Value & "; " & Range("g16").Value & "; " & Range("g17").Value & "; " & Range("g18").Value & "; " & Range("g19").Value & "; " & Range("g20").Value & "; " & Range("g21").Value & "; " & Range("g22").Value
        
        .CC = ""
        .BCC = ""
        .Subject = Sheets("Fast besætning").Range("f1").Value
        
        .Body = Range("l1").Value & vbNewLine & vbNewLine & Range("l2").Value & vbNewLine & _
        Range("l3").Value & vbNewLine & Range("l4").Value & vbNewLine & Range("l5").Value
        '& vbNewLine & _ Range("n8").Value
        
        
                                
 ThisWorkbook.Save
        .Attachments.Add ThisWorkbook.Path & "\" & ThisWorkbook.Name
        .Display
        '.Send                               '<-- .Send will auto send email without review
    End With

End Sub
 

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
Add these few lines of code at the end of your macro:
VBA Code:
    '...
    With wbk
        .Activate
        .Sheets("Fast besætning").Select
    End With
    '------ added ------------
    If UCase(Range("K1")) <> "X" Then
        Mail_workbook_Outlook
    Else
        ThisWorkbook.Save
    End If
    '-------------------------
End Sub
 
Last edited:
Upvote 0
Solution
Add these few lines of code at the end of your macro:
VBA Code:
    '...
    With wbk
        .Activate
        .Sheets("Fast besætning").Select
    End With
    '------ added ------------
    If UCase(Range("K1")) <> "X" Then
        Mail_workbook_Outlook
    Else
        ThisWorkbook.Save
    End If
    '-------------------------
End Sub
Hi rollis13, it just works thank you very much. Greetings from Denmark Klaus W
 
Upvote 0

Forum statistics

Threads
1,213,504
Messages
6,114,016
Members
448,543
Latest member
MartinLarkin

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