• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Macro to break data and save on file location

Hi All,

Can anyone help me with vba code to break data in attached excel into new separate excel files based on values in column B and save them separately in file location.

Also I need a macro in the above which mails each file to ABC@gmail.com.I have outlook 365 installed on mydesk.
 

Attachments

  • data break.xlsx
    508.8 KB · Views: 5
Hi @Shakti Bansal

Below you can find some code for separating the data based on the values in "B" and creating new workbooks... that said, I would advise against using it with so many unique values in "B", as it may take some time and slow your PC down significantly while doing it. In any case, it should work as intended:
Code:
Sub data_break()

    Application.ScreenUpdating = False

    Sheets.Add After:=ActiveSheet
    With Sheets(1)
        .Range("B2:B" & .Columns("B").Cells(Rows.Count).End(xlUp).Row).Copy Sheets(2).Cells(1, 1)
    End With
    Sheets(2).Columns("A").RemoveDuplicates Columns:=1, Header:=xlNo

    Dim NewWorkbook As Workbook
    Dim ThisWorkbook As Workbook
    Dim NewWorkbookName As String
    Dim relativePath As String
    Dim c As Range
    Dim lastrow As Integer

    Set ThisWorkbook = ActiveWorkbook
    lastrow = Columns("B").Cells(Rows.Count).End(xlUp).Row

    For Each c In Sheets(2).Range("A1:A" & Range("A1").End(xlDown).Row).Cells

        NewWorkbookName = c.Value & ".xlsx"

        Set NewWorkbook = Workbooks.Add
        relativePath = ThisWorkbook.Path & "\" & NewWorkbookName
        ActiveWorkbook.SaveAs Filename:=relativePath

        With ThisWorkbook.Sheets(1)
            If .FilterMode = True Then
                .ShowAllData
            End If

            .Range("A1:X" & lastrow).AutoFilter Field:=2, Criteria1:=c.Value
            .Range("A1:X" & Range("A1").End(xlDown).Row).Copy Workbooks(NewWorkbookName).Sheets(1).Cells(1, 1)
        End With
      
        Workbooks(NewWorkbookName).Close savechanges:=True

    Next c
  
    Application.DisplayAlerts = False
        ThisWorkbook.Sheets(2).Delete
    Application.DisplayAlerts = True

    ThisWorkbook.Sheets(1).Range("A1").AutoFilter
      
    Application.ScreenUpdating = True

End Sub

For the email part, since it is only one email, do you want to send all files at once or one email for each?
 
Hi @Shakti Bansal

Below you can find some code for separating the data based on the values in "B" and creating new workbooks... that said, I would advise against using it with so many unique values in "B", as it may take some time and slow your PC down significantly while doing it. In any case, it should work as intended:
Code:
Sub data_break()

    Application.ScreenUpdating = False

    Sheets.Add After:=ActiveSheet
    With Sheets(1)
        .Range("B2:B" & .Columns("B").Cells(Rows.Count).End(xlUp).Row).Copy Sheets(2).Cells(1, 1)
    End With
    Sheets(2).Columns("A").RemoveDuplicates Columns:=1, Header:=xlNo

    Dim NewWorkbook As Workbook
    Dim ThisWorkbook As Workbook
    Dim NewWorkbookName As String
    Dim relativePath As String
    Dim c As Range
    Dim lastrow As Integer

    Set ThisWorkbook = ActiveWorkbook
    lastrow = Columns("B").Cells(Rows.Count).End(xlUp).Row

    For Each c In Sheets(2).Range("A1:A" & Range("A1").End(xlDown).Row).Cells

        NewWorkbookName = c.Value & ".xlsx"

        Set NewWorkbook = Workbooks.Add
        relativePath = ThisWorkbook.Path & "\" & NewWorkbookName
        ActiveWorkbook.SaveAs Filename:=relativePath

        With ThisWorkbook.Sheets(1)
            If .FilterMode = True Then
                .ShowAllData
            End If

            .Range("A1:X" & lastrow).AutoFilter Field:=2, Criteria1:=c.Value
            .Range("A1:X" & Range("A1").End(xlDown).Row).Copy Workbooks(NewWorkbookName).Sheets(1).Cells(1, 1)
        End With
     
        Workbooks(NewWorkbookName).Close savechanges:=True

    Next c
 
    Application.DisplayAlerts = False
        ThisWorkbook.Sheets(2).Delete
    Application.DisplayAlerts = True

    ThisWorkbook.Sheets(1).Range("A1").AutoFilter
     
    Application.ScreenUpdating = True

End Sub

For the email part, since it is only one email, do you want to send all files at once or one email for each?
Hi I want to send each file separately one by one
 
Hi I am getting error while initiating the macro,Please refer to below screenshot.

View attachment 36299
Hi,

Is it possible you are running the code in an unsaved workbook or perhaps it was opened from an email?... since it gives each new file the path of the original, you need to make sure there is a path to use, which means the file must be saved beforehand.
Please try saving the original file (the one with all the data) to a folder and then running the code.

Let me know if that solves the problem
 
@Shakti Bansal

Replace previous with the following... added code to send each file via email. You can change the .To, .Subject and .Body as you see fit:
Code:
Sub data_break()

    Application.ScreenUpdating = False

    Sheets.Add After:=ActiveSheet
    With Sheets(1)
        .Range("B2:B" & .Columns("B").Cells(Rows.Count).End(xlUp).Row).Copy Sheets(2).Cells(1, 1)
    End With
    Sheets(2).Columns("A").RemoveDuplicates Columns:=1, Header:=xlNo

    Dim NewWorkbook As Workbook
    Dim ThisWorkbook As Workbook
    Dim NewWorkbookName As String
    Dim relativePath As String
    Dim c As Range
    Dim lastrow As Integer
    Dim OutLookApp As Object
    Dim OutLookMailItem As Object

    Set ThisWorkbook = ActiveWorkbook
    lastrow = Columns("B").Cells(Rows.Count).End(xlUp).Row

    For Each c In Sheets(2).Range("A1:A" & Range("A1").End(xlDown).Row).Cells

        NewWorkbookName = c.Value & ".xlsx"

        Set NewWorkbook = Workbooks.Add
        relativePath = ThisWorkbook.Path & "\" & NewWorkbookName
        ActiveWorkbook.SaveAs Filename:=relativePath

        With ThisWorkbook.Sheets(1)
            If .FilterMode = True Then
                .ShowAllData
            End If

            .Range("A1:X" & lastrow).AutoFilter Field:=2, Criteria1:=c.Value
            .Range("A1:X" & Range("A1").End(xlDown).Row).Copy Workbooks(NewWorkbookName).Sheets(1).Cells(1, 1)
        End With
    
        Workbooks(NewWorkbookName).Close savechanges:=True
      
        Set OutLookApp = CreateObject("Outlook.application")
        Set OutLookMailItem = OutLookApp.CreateItem(0)
        Set Attach = OutLookMailItem.Attachments
      
        With OutLookMailItem
            .To = "abc@gmail.com"
            .Subject = "Put your subject here"
            .Body = "Put your body here"
            Attach.Add relativePath
            .Send
        End With

    Next c

    Application.DisplayAlerts = False
        ThisWorkbook.Sheets(2).Delete
    Application.DisplayAlerts = True

    ThisWorkbook.Sheets(1).Range("A1").AutoFilter
    
    Application.ScreenUpdating = True
  
End Sub
 
Last edited:
@Shakti Bansal

Replace previous with the following... added code to send each file via email. You can change the .To, .Subject and .Body as you see fit:
Code:
Sub data_break()

    Application.ScreenUpdating = False

    Sheets.Add After:=ActiveSheet
    With Sheets(1)
        .Range("B2:B" & .Columns("B").Cells(Rows.Count).End(xlUp).Row).Copy Sheets(2).Cells(1, 1)
    End With
    Sheets(2).Columns("A").RemoveDuplicates Columns:=1, Header:=xlNo

    Dim NewWorkbook As Workbook
    Dim ThisWorkbook As Workbook
    Dim NewWorkbookName As String
    Dim relativePath As String
    Dim c As Range
    Dim lastrow As Integer
    Dim OutLookApp As Object
    Dim OutLookMailItem As Object

    Set ThisWorkbook = ActiveWorkbook
    lastrow = Columns("B").Cells(Rows.Count).End(xlUp).Row

    For Each c In Sheets(2).Range("A1:A" & Range("A1").End(xlDown).Row).Cells

        NewWorkbookName = c.Value & ".xlsx"

        Set NewWorkbook = Workbooks.Add
        relativePath = ThisWorkbook.Path & "\" & NewWorkbookName
        ActiveWorkbook.SaveAs Filename:=relativePath

        With ThisWorkbook.Sheets(1)
            If .FilterMode = True Then
                .ShowAllData
            End If

            .Range("A1:X" & lastrow).AutoFilter Field:=2, Criteria1:=c.Value
            .Range("A1:X" & Range("A1").End(xlDown).Row).Copy Workbooks(NewWorkbookName).Sheets(1).Cells(1, 1)
        End With
   
        Workbooks(NewWorkbookName).Close savechanges:=True
     
        Set OutLookApp = CreateObject("Outlook.application")
        Set OutLookMailItem = OutLookApp.CreateItem(0)
        Set Attach = OutLookMailItem.Attachments
     
        With OutLookMailItem
            .To = "abc@gmail.com"
            .Subject = "Put your subject here"
            .Body = "Put your body here"
            Attach.Add relativePath
            .Send
        End With

    Next c

    Application.DisplayAlerts = False
        ThisWorkbook.Sheets(2).Delete
    Application.DisplayAlerts = True

    ThisWorkbook.Sheets(1).Range("A1").AutoFilter
   
    Application.ScreenUpdating = True
 
End Sub
Hi ,

Thanks for details.

The email macro is only sending first file using outlook,However it is not sending other files.

Please help
 
Hi ,

Thanks for details.

The email macro is only sending first file using outlook,However it is not sending other files.

Please help
Hi,

Try replacing .Send with .Display and see if it shows the outlook window for the other emails (it does here)
 
Hi I tried replicatng the code to a different file,the code is breaking file,however it it not picking data . Please help
 

Attachments

  • Booking.com No show- September 2016 MACRO.xlsm
    18.4 KB · Views: 3
Back
Top