• DİKKAT

    DOSYA İndirmek/Yüklemek için ÜCRETLİ ALTIN ÜYELİK Gereklidir!
    Altın Üyelik Hakkında Bilgi

Şarta uyan sayfaları tek kitapta mail göndermek

Katılım
21 Nisan 2008
Mesajlar
77
Excel Vers. ve Dili
2010
Arkadaşlar Merhaba,
Ekteki dosyamda EUR, USD ve GBP diye sayfalarım bulunmaktadır. Bu sayfalardan herhangi birinin yada birilerinin A5 hücresinde veri bulunuyorsa mail göndersin. Veri olmayan sayfaları maile eklemesin istiyorum. Ekteki dosyamda macroyu çalıştırdığımda bahsettiğim üç sayfayıda eklemektedir fakat istediğim sadece A5 hücresinde veri olan sayfalar maile eklensin. Kodları ve dosyayı sizinle paylaşıyorum. Yardımlarınızı rica ederim. İyi çalışmalar dilerim.

Kod:
Option Explicit
Sub Mail_Sheets_Array()
'Working in 2000-2010
    Dim FileExtStr As String
    Dim FileFormatNum As Long
    Dim Sourcewb As Workbook
    Dim Destwb As Workbook
    Dim TempFilePath As String
    Dim TempFileName As String
    Dim OutApp As Object
    Dim OutMail As Object
    Dim sh As Worksheet
    Dim TheActiveWindow As Window
    Dim TempWindow As Window

    With Application
        .ScreenUpdating = False
        .EnableEvents = False
    End With

    Set Sourcewb = ActiveWorkbook

    'Copy the sheets to a new workbook
    'We add a temporary Window to avoid the Copy problem
    'if there is a List or Table in one of the sheets and
    'if the sheets are grouped
    With Sourcewb
        Set TheActiveWindow = ActiveWindow
        Set TempWindow = .NewWindow
        .Sheets(Array("EUR", "USD", "GBP")).Copy
    End With

    'Close temporary Window
    TempWindow.Close

    Set Destwb = ActiveWorkbook

    'Determine the Excel version and file extension/format
    With Destwb
        If Val(Application.Version) < 12 Then
            'You use Excel 97-2003
            FileExtStr = ".xls": FileFormatNum = -4143
        Else
            'You use Excel 2007-2010
            'We exit the sub when your answer is NO in the security dialog that you only
            'see  when you copy a sheet from a xlsm file with macro's disabled.
            If Sourcewb.Name = .Name Then
                With Application
                    .ScreenUpdating = True
                    .EnableEvents = True
                End With
                MsgBox "Your answer is NO in the security dialog"
                Exit Sub
            Else
                Select Case Sourcewb.FileFormat
                Case 51: FileExtStr = ".xlsx": FileFormatNum = 51
                Case 52:
                    If .HasVBProject Then
                        FileExtStr = ".xlsm": FileFormatNum = 52
                    Else
                        FileExtStr = ".xlsx": FileFormatNum = 51
                    End If
                Case 56: FileExtStr = ".xls": FileFormatNum = 56
                Case Else: FileExtStr = ".xlsb": FileFormatNum = 50
                End Select
            End If
        End If
    End With

    '    'Change all cells in the worksheets to values if you want
    '    For Each sh In Destwb.Worksheets
    '        sh.Select
    '        With sh.UsedRange
    '            .Cells.Copy
    '            .Cells.PasteSpecial xlPasteValues
    '            .Cells(1).Select
    '        End With
    '        Application.CutCopyMode = False
    '        Destwb.Worksheets(1).Select
    '    Next sh

    'Save the new workbook/Mail it/Delete it
    TempFilePath = Environ$("temp") & "\"
    TempFileName = "Part of " & Sourcewb.Name & " " & Format(Now, "dd-mmm-yy h-mm-ss")

    Set OutApp = CreateObject("Outlook.Application")
    Set OutMail = OutApp.CreateItem(0)

    With Destwb
        .SaveAs TempFilePath & TempFileName & FileExtStr, FileFormat:=FileFormatNum
        On Error Resume Next
        With OutMail
            .to = "UUUU@UUUU"
            .CC = ""
            .BCC = ""
            .Subject = "Subject line"
            .Body = "Hi there"
            .Attachments.Add Destwb.FullName
            'You can add other files also like this
            '.Attachments.Add ("C:\test.txt")
            .Display   'or use .Send
        End With
        On Error GoTo 0
        .Close savechanges:=False
    End With

    'Delete the file you have send
    Kill TempFilePath & TempFileName & FileExtStr

    Set OutMail = Nothing
    Set OutApp = Nothing

    With Application
        .ScreenUpdating = True
        .EnableEvents = True
    End With
End Sub
 

Ekli dosyalar

Kodu eklenti yaptım, böyle bir deneyin.
Kod:
Option Explicit
Sub Mail_Sheets_Array()
'Working in 2000-2010
    Dim FileExtStr As String
    Dim FileFormatNum As Long
    Dim Sourcewb As Workbook
    Dim Destwb As Workbook
    Dim TempFilePath As String
    Dim TempFileName As String
    Dim OutApp As Object
    Dim OutMail As Object
    Dim sh As Worksheet
    Dim TheActiveWindow As Window
    Dim TempWindow As Window
    Dim arr()
    Dim sht As Worksheet
    Dim c As Integer
    Dim i As Integer
    Dim ii As Integer
    Dim s As String
    With Application
        .ScreenUpdating = False
        .EnableEvents = False
    End With

    Set Sourcewb = ActiveWorkbook

    'Copy the sheets to a new workbook
    'We add a temporary Window to avoid the Copy problem
    'if there is a List or Table in one of the sheets and
    'if the sheets are grouped
    With Sourcewb
        Set TheActiveWindow = ActiveWindow
        Set TempWindow = .NewWindow
    If Sheets("EUR").[a5] <> "" And Sheets("GBP").[a5] <> "" And Sheets("USD").[a5] <> "" Then
    .Sheets(Array("EUR", "GBP", "USD")).Copy: GoTo 10
    ElseIf Sheets("EUR").[a5] <> "" And Sheets("USD").[a5] <> "" Then
    .Sheets(Array("EUR", "USD")).Copy: GoTo 10
    ElseIf Sheets("EUR").[a5] <> "" And Sheets("GBP").[a5] <> "" Then
    .Sheets(Array("EUR", "GBP")).Copy: GoTo 10
    ElseIf Sheets("USD").[a5] <> "" And Sheets("GBP").[a5] <> "" Then
    .Sheets(Array("USD", "GBP")).Copy: GoTo 10
    
    ElseIf Sheets("EUR").[a5] <> "" Then
    .Sheets("EUR").Copy: GoTo 10
    ElseIf Sheets("USD").[a5] <> "" Then
    .Sheets("USD").Copy: GoTo 10
    ElseIf Sheets("GBP").[a5] <> "" Then
    .Sheets("GBP").Copy: GoTo 10
    End If
    '.Sheets(Array("EUR", "USD", "GBP")).Copy
        
    End With
10
    'Close temporary Window
    TempWindow.Close

    Set Destwb = ActiveWorkbook

    'Determine the Excel version and file extension/format
    With Destwb
        If Val(Application.Version) < 12 Then
            'You use Excel 97-2003
            FileExtStr = ".xls": FileFormatNum = -4143
        Else
            'You use Excel 2007-2010
            'We exit the sub when your answer is NO in the security dialog that you only
            'see  when you copy a sheet from a xlsm file with macro's disabled.
            If Sourcewb.Name = .Name Then
                With Application
                    .ScreenUpdating = True
                    .EnableEvents = True
                End With
                MsgBox "Your answer is NO in the security dialog"
                Exit Sub
            Else
                Select Case Sourcewb.FileFormat
                Case 51: FileExtStr = ".xlsx": FileFormatNum = 51
                Case 52:
                    If .HasVBProject Then
                        FileExtStr = ".xlsm": FileFormatNum = 52
                    Else
                        FileExtStr = ".xlsx": FileFormatNum = 51
                    End If
                Case 56: FileExtStr = ".xls": FileFormatNum = 56
                Case Else: FileExtStr = ".xlsb": FileFormatNum = 50
                End Select
            End If
        End If
    End With

    '    'Change all cells in the worksheets to values if you want
    '    For Each sh In Destwb.Worksheets
    '        sh.Select
    '        With sh.UsedRange
    '            .Cells.Copy
    '            .Cells.PasteSpecial xlPasteValues
    '            .Cells(1).Select
    '        End With
    '        Application.CutCopyMode = False
    '        Destwb.Worksheets(1).Select
    '    Next sh

    'Save the new workbook/Mail it/Delete it
    TempFilePath = Environ$("temp") & "\"
    TempFileName = "Part of " & Sourcewb.Name & " " & Format(Now, "dd-mmm-yy h-mm-ss")

    Set OutApp = CreateObject("Outlook.Application")
    Set OutMail = OutApp.CreateItem(0)

    With Destwb
        .SaveAs TempFilePath & TempFileName & FileExtStr, FileFormat:=FileFormatNum
        On Error Resume Next
        With OutMail
            .To = "UUUU@UUUU"
            .CC = ""
            .BCC = ""
            .Subject = "Subject line"
            .Body = "Hi there"
            .Attachments.Add Destwb.FullName
            'You can add other files also like this
            '.Attachments.Add ("C:\test.txt")
            .Display   'or use .Send
        End With
        On Error GoTo 0
        .Close savechanges:=False
    End With

    'Delete the file you have send
    Kill TempFilePath & TempFileName & FileExtStr

    Set OutMail = Nothing
    Set OutApp = Nothing

    With Application
        .ScreenUpdating = True
        .EnableEvents = True
    End With
End Sub
 
Hamitcan Bey merhaba,
Cevabınız için çok teşekkür ederim. Kodu deniyorum fakat hata var sanırım bir yerde kodu çalıştırdığımda aşağıdaki koda geliyor yani ekrana mesajbox çıkıyor. Sorun ne olabilir acaba?

Kod:
 MsgBox "Your answer is NO in the security dialog"
 
Hamitcan Bey merhaba,
Sorun çözüldü. Kodları aşağıda paylaşıyorum. Yardımlarınız için çok teşekkürler.
Arkadaşlar, Bu macro istenilen sayfaların maile eklenmesi yardımcı olmaktadır. Örneğin TRY, EUR , USD vs. sekmeleriniz varsa bunların içinde belirli bir hücrede veri varsa sadece o sayfaları maile eklemektedir.(Sekmelerdeki B5 hücresi dolu ise) Umarım bu çalışma başkalarınada yararlı olur. Herkese iyi çalışmalar.


Kod:
Option Explicit

Sub Mail_Sheets_Array()
'Working in 2000-2010
    Dim FileExtStr As String
    Dim FileFormatNum As Long
    Dim Sourcewb As Workbook
    Dim Destwb As Workbook
    Dim TempFilePath As String
    Dim TempFileName As String
    Dim OutApp As Object
    Dim OutMail As Object
    Dim sh As Worksheet
    Dim TheActiveWindow As Window
    Dim TempWindow As Window

'Yeni degerler eklendi
 Dim MySheets() As String
    Dim Wks As Worksheet
    Dim WksCnt As Long



    With Application
        .ScreenUpdating = False
        .EnableEvents = False
    End With


    'Burası degistirildi

    'Set Sourcewb = ActiveWorkbook
     '   With Sourcewb
      '  Set TheActiveWindow = ActiveWindow
       ' Set TempWindow = .NewWindow
        '.Sheets(Array("EUR", "USD", "GBP")).Copy
   ' End With
   ' TempWindow.Close
   ' Set Destwb = ActiveWorkbook

   'Yeni kodlar
Set Sourcewb = ActiveWorkbook
    
    WksCnt = 0
    With Sourcewb
        Set TheActiveWindow = ActiveWindow
        Set TempWindow = .NewWindow
        For Each Wks In .Worksheets
            If Wks.Range("B5").Value <> "" Then
                WksCnt = WksCnt + 1
                ReDim Preserve MySheets(1 To WksCnt)
                MySheets(WksCnt) = Wks.Name
            End If
        Next Wks
        .Sheets(MySheets).Copy
    End With
    
    TempWindow.Close

    Set Destwb = ActiveWorkbook



    'Determine the Excel version and file extension/format
    With Destwb
        If Val(Application.Version) < 12 Then
            'You use Excel 97-2003
            FileExtStr = ".xls": FileFormatNum = -4143
        Else
            'You use Excel 2007-2010
            'We exit the sub when your answer is NO in the security dialog that you only
            'see  when you copy a sheet from a xlsm file with macro's disabled.
            If Sourcewb.Name = .Name Then
                With Application
                    .ScreenUpdating = True
                    .EnableEvents = True
                End With
                MsgBox "Your answer is NO in the security dialog"
                Exit Sub
            Else
                Select Case Sourcewb.FileFormat
                Case 51: FileExtStr = ".xlsx": FileFormatNum = 51
                Case 52:
                    If .HasVBProject Then
                        FileExtStr = ".xlsm": FileFormatNum = 52
                    Else
                        FileExtStr = ".xlsx": FileFormatNum = 51
                    End If
                Case 56: FileExtStr = ".xls": FileFormatNum = 56
                Case Else: FileExtStr = ".xlsb": FileFormatNum = 50
                End Select
            End If
        End If
    End With

    '    'Change all cells in the worksheets to values if you want
    '    For Each sh In Destwb.Worksheets
    '        sh.Select
    '        With sh.UsedRange
    '            .Cells.Copy
    '            .Cells.PasteSpecial xlPasteValues
    '            .Cells(1).Select
    '        End With
    '        Application.CutCopyMode = False
    '        Destwb.Worksheets(1).Select
    '    Next sh

    'Save the new workbook/Mail it/Delete it
    TempFilePath = Environ$("temp") & "\"
    TempFileName = "Part of " & Sourcewb.Name & " " & Format(Now, "dd-mmm-yy h-mm-ss")

    Set OutApp = CreateObject("Outlook.Application")
    Set OutMail = OutApp.CreateItem(0)

    With Destwb
        .SaveAs TempFilePath & TempFileName & FileExtStr, FileFormat:=FileFormatNum
        On Error Resume Next
        With OutMail
            .to = "UUUU@UUUU"
            .CC = ""
            .BCC = ""
            .Subject = "Subject line"
            .Body = "Hi there"
            .Attachments.Add Destwb.FullName
            'You can add other files also like this
            '.Attachments.Add ("C:\test.txt")
            .Display   'or use .Send
        End With
        On Error GoTo 0
        .Close savechanges:=False
    End With

    'Delete the file you have send
    Kill TempFilePath & TempFileName & FileExtStr

    Set OutMail = Nothing
    Set OutApp = Nothing

    With Application
        .ScreenUpdating = True
        .EnableEvents = True
    End With
End Sub
 
Kodun içinde bu konu belirtilmiş. Anladığım kadarı ile makroları etkinleştirilmemiş bir dosya içinden bir sayfayı kopyalamak istediğinizde bu mesaj çıkıyor. Böyle olunca da koddan çıkılıyor.
Kod:
  'You use Excel 2007-2010
            'We exit the sub when your answer is NO in the security dialog that you only
            'see  when you copy a sheet from a xlsm file with macro's disabled.
 
Geri
Üst