Forum Discussion

jaydub66's avatar
jaydub66
Copper Contributor
Nov 14, 2019
Solved

Looking for way to extract data from Excel sheet and place into new sheet

I have a large spreadsheet of data from a registration system that I'm trying to figure out a way to extract the "coach" data from only and place it into a new sheet. Below is a representation of the...
  • jaydub66 

    First of all, it's always good to upload a sample excel file instead of image so that we are not required to retype your data to test the solution.

     

    Based on your sample data, you may try something like this to get the unique coach names on another sheet.

    Sub GetUniqueCoachNames()
    Dim wsData      As Worksheet
    Dim wsReport    As Worksheet
    Dim lr          As Long
    Dim col         As Long
    Dim i           As Long
    Dim j           As Long
    Dim arr         As Variant
    Dim arrOut()    As Variant
    Dim dict        As Object
    Dim it          As Variant
    
    Application.ScreenUpdating = False
    
    Set wsData = Worksheets("Sheet1")   'Sheet with Data
    
    On Error Resume Next
    Set wsReport = Worksheets("Unique Coach Names")     'Name of Sheet with unique coach names that would be inserted
    wsReport.Range("A1").CurrentRegion.Offset(1).Clear
    On Error GoTo 0
    
    If wsReport Is Nothing Then
        Set wsReport = Worksheets.Add(after:=wsData)
        wsReport.Name = "Unique Coach Names"
        With wsReport.Range("A1:C1")
            .Value = Array("Coach Name", "Coach Email", "Coach Phone")
            .Borders.Color = vbBlack
            .Font.Size = 12
            .Font.Bold = True
        End With
    End If
    
    lr = wsData.Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    
    Set dict = CreateObject("Scripting.Dictionary")
    
    For col = 1 To 4 Step 3
        arr = wsData.Range(wsData.Cells(2, col), wsData.Cells(lr, col + 2)).Value
        For i = 1 To UBound(arr, 1)
            If arr(i, 1) <> "" Then
                dict.Item(arr(i, 1) & "^" & arr(i, 2) & "^" & arr(i, 3)) = ""
            End If
        Next i
    Next col
    
    ReDim arrOut(1 To dict.Count, 1 To 3)
    
    For Each it In dict.keys
        j = j + 1
        arrOut(j, 1) = Split(it, "^")(0)
        arrOut(j, 2) = Split(it, "^")(1)
        arrOut(j, 3) = Split(it, "^")(2)
    Next it
    
    With wsReport.Range("A2").Resize(j, 3)
        .Value = arrOut
        .Borders.Color = vbBlack
    End With
    
    wsReport.Select
    wsReport.UsedRange.Columns.AutoFit
    Application.ScreenUpdating = True
    
    End Sub

     

    In the attached, click the button called "Get Unique Coach Names" on Sheet1 to run the code.

     

     

     

Resources