-1

I need to copy all the data into a new sheet or new file based on the specified city in the dataset.

Ex.
image
Los Angeles has 4 rows of data,
Chicago has 2 rows of data,
Miami has 5 rows of data should be paste in a new sheet or new book.

Expected output.
expected output

8
  • It appears that Power Query could be a good option for this task. Aoother option might be Filter if available. support.microsoft.com/en-us/office/… Commented Oct 12 at 7:16
  • @Storax With due respect, If possible could you please help me the VBA code. Commented Oct 12 at 7:48
  • 1
    The approach I suggested requires no VBA code — just give it a try. Also, Stack Overflow isn’t a place to get ready-made code; it’s meant for showing your own attempts so others can help you improve them. Commented Oct 12 at 8:12
  • cities data into new sheet or new file. - which is it ?, new sheets in existing workbook or one sheet per city in new workbook, or new workbooks with one sheet for each city ? Commented Oct 12 at 10:12
  • To start with please have a look e.g. here Commented Oct 12 at 10:39

2 Answers 2

1

Like example:

Option Explicit

Sub SplitDataByColumnI()
    Dim wsDest      As Worksheet
    Dim i           As Long, key

    Application.ScreenUpdating = False
    Application.Calculation = xlCalculationManual

    With ThisWorkbook.Worksheets("Sheet1")   ' This worksheet may have a different name for you, please replace it with your name

        Dim lastRow As Long
        lastRow = .Cells(.Rows.Count, "A").End(xlUp).Row

        If lastRow < 2 Then
            MsgBox "No data available for distribution!", vbExclamation
            Exit Sub
        End If

        Dim headers As Variant
        headers = .Range("A1:I1").Value

        Dim arr     As Variant
        arr = .Range("A2:I" & lastRow).Value
    End With

    Dim dict        As Object
    Set dict = CreateObject("Scripting.Dictionary")

    For i = 1 To UBound(arr, 1)
        key = Trim(CStr(arr(i, 9)))

        If key <> "" Then

            If Not dict.exists(key) Then
                dict.Add key, New Collection
            End If

            dict(key).Add Application.Index(arr, i)
        End If

    Next i

    For Each key In dict.keys

        On Error Resume Next
        Set wsDest = ThisWorkbook.Worksheets(key)
        On Error GoTo 0

        If wsDest Is Nothing Then
            Set wsDest = ThisWorkbook.Worksheets.Add(After:=ThisWorkbook.Worksheets(ThisWorkbook.Worksheets.Count))
            wsDest.Name = key
        Else
            wsDest.UsedRange.Clear
        End If

        wsDest.Range("A1:I1").Value = headers

        Dim tempArr()
        ReDim tempArr(1 To dict(key).Count, 1 To 9)

        Dim j       As Long
        
        For i = 1 To dict(key).Count
            
            For j = 1 To 9
                tempArr(i, j) = dict(key)(i)(j)
            Next j
        
        Next i

        wsDest.Range("A2").Resize(UBound(tempArr, 1), UBound(tempArr, 2)).Value = tempArr
        Set wsDest = Nothing
    Next key

    Application.Calculation = xlCalculationAutomatic
    Application.ScreenUpdating = True

    MsgBox "DONE!", vbInformation
End Sub
Sign up to request clarification or add additional context in comments.

Comments

0

Apply a filter for each city in turn and copy visible cells to a new sheet in existing workbook.

Sub SplitData()

    Dim wb As Workbook, ws As Worksheet, wsAll As Worksheet
    Dim rng As Range, bExists As Boolean
    Dim dictCity As Object
    Dim k, r As Long, lastrow As Long, n As Long
    
    Set wb = ThisWorkbook
    Set wsAll = wb.Sheets("Sheet1")
    Set dictCity = CreateObject("Scripting.Dictionary")
   
    With wsAll
        lastrow = .Cells(.Rows.Count, "I").End(xlUp).Row ' City
        Set rng = .Range("A1:I" & lastrow)
        
        ' determine unique city names
        For r = 2 To lastrow
            k = .Cells(r, "I")
            dictCity(k) = 1
            bExists = False
            
            ' clear if existing sheet
            bExists = False
            For Each ws In wb.Sheets
                If ws.Name = k Then
                    ws.Cells.Clear
                    bExists = True
                    Exit For
                End If
            Next
             
          ' create new sheet if not existing
          If Not bExists Then
             n = wb.Sheets.Count
             Set ws = wb.Worksheets.Add(after:=wb.Sheets(n))
             ws.Name = k
          End If
       Next
    End With
       
    ' filter by city and copy
    Application.ScreenUpdating = False
    rng.AutoFilter
    For Each k In dictCity.keys
        rng.AutoFilter 9, k ' col I
        Set ws = wb.Sheets(k)
        rng.Copy ws.Range("A1")
    Next
    rng.AutoFilter
    Application.ScreenUpdating = True
    
    MsgBox dictCity.Count & " cities found", vbInformation
   
End Sub

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.