VBA to select a certain range of cells based on values in other cells and relative locations to them

deadlock123

New Member
Joined
Oct 21, 2021
Messages
1
Office Version
  1. 365
Platform
  1. Windows
Hello all!

So I have some consumer satisfaction survey data that is not organized at all - no headers. There's no header whatsoever as you can see below.

What I need is to separate the data by the service provider name (e.g., John Doe, Jane Doe). For example, select and copy all the survey data for John Doe (as seen below, from question 1 to question 4), and save it in a separate sheet named John Doe. And do that for many different people whose names appear on the sheet. Quite often, the same name re-appears throughout the sheet, e.g., the survey data for John Doe's other services will appear in the middle of the sheet.

I have the name list of the service providers. So I loop through this list to get each provider's name.

So my manual way was: use Find All for each provider to select the relevant rows - ranging from the three rows above the first occurrence of the name (the Service code: Name row) to the Thank you message row. And cut-paste it to another sheet titled the provider's name, go back to the original sheet and repeat the Find All - Cut Paste again for any other service of the same provider, until there's no remaining data left for the person. Then I'd know that I got all the data for that person. Then I move on to the next provider's name in the name list. And repeat the process.

I cannot help but thinking there should be a way to code this procedure. I just can't figure out how though.

There's a pattern indeed. For each service, first we see the Service Code: Name row, then quarter row, the provider's name row, the welcome message row, and then the survey questions and answer data rows - however many there are, and then finally Thank you message row. I'm imagining that there should be a way to slice these up for each service and aggregate them onto each provider's sheet.

Any tip or advice would be greatly appreciated. Thank you for your time!



Sample to ask .xlsx
ABCDEFGHIJKLMN
1PT1101: Physical Theraphy session
22021 Summer Quarter
3John Doe
4Please give us 5 minutes of your time to fill out this evaluation.
5
61) Please evaluate the service you received.
7StatementStrongly DisagreePercentDisagreePercentNeutralPercentAgreePercentStrongly AgreePercentTotalAverage
8Session started on time 00.00%00.00%00.00%337.50%562.50%84.6
9This service addressed my pain issues. 00.00%00.00%00.00%337.50%562.50%84.6
10
11
122) I need technical support in navigating the scheduling system.
13YesPercentNoPercentN/APercentTotalAverage
14225.00%450.00%225.00%81.7
15
163) Please evaluate the therapist below.
17TherapistStatementStrongly DisagreePercentDisagreePercentNeutralPercentAgreePercentStrongly AgreePercentTotalAverage
18John DoeThe entire treatment plan was reviewed in the first session.00.00%00.00%00.00%225.00%675.00%84.8
19Breaks were approximately 10 minutes/hr in length (e.g. 30 minutes total for a 3-hour class).00.00%00.00%00.00%337.50%562.50%84.6
20
21
224) What is the strength of this therapist?
23TherapistAnswer
24John Doevery good
25knowledge
26Informative
27Clear explanation!
28
29Thank you in advance for your feedback!
30
31WM1048: Work environment consulting
322021 Summer Quarter
33Jane Doe
34Please give us 5 minutes of your time to fill out this evaluation.
35
361) Please evaluate the service you received.
37StatementStrongly DisagreePercentDisagreePercentNeutralPercentAgreePercentStrongly AgreePercentTotalAverage
38Session started on time 00.00%00.00%00.00%450.00%450.00%84.5
39This service addressed my pain issues. 00.00%00.00%00.00%450.00%450.00%84.5
40
41
422) I need technical support in navigating the scheduling system.
43YesPercentNoPercentN/APercentTotalAverage
44225.00%450.00%225.00%81.7
45
463) Please evaluate the therapist below.
47TherapistStatementStrongly DisagreePercentDisagreePercentNeutralPercentAgreePercentStrongly AgreePercentTotalAverage
48Jane DoeThe entire treatment plan was reviewed in the first session.00.00%00.00%00.00%225.00%675.00%84.8
49Breaks were approximately 10 minutes/hr in length (e.g. 30 minutes total for a 3-hour class).00.00%00.00%00.00%337.50%562.50%84.6
50
51
524) What is the strength of this therapist?
53TherapistAnswer
54Jane Doenot good
55so so knowledge
56Informative
57Very friendly!
58
59## Same pattern …##
60## Many other services, some names reappear ##
Sample to ask
 

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
You might consider the following...

VBA Code:
Sub SelectAndCopy()
Dim r As Range
Dim LastRow As Long
Dim firstAddress As String
Dim arr As Variant
Dim i As Long, kount As Long
Dim ws1 As Worksheet, ws2 As Worksheet

Application.ScreenUpdating = False
Set ws1 = Sheets(1)
LastRow = ws1.Cells(Rows.Count, "A").End(xlUp).Row
ReDim arr(1 To LastRow, 1 To 1)
i = 0
kount = 0

With ws1.Range("A1:A" & LastRow)
    Set r = .Find("Please give", LookIn:=xlValues)
    If Not r Is Nothing Then
        firstAddress = r.Address
        Do
            i = i + 1
            kount = kount + 1
            arr(i, 1) = r.Row
            Set r = .FindNext(r)
        Loop While firstAddress <> r.Address
    End If
End With
i = i + 1
arr(i, 1) = LastRow

For i = 1 To kount
    Set ws2 = ThisWorkbook.Sheets.Add(after:=Sheets(Sheets.Count))
    ws1.Activate
    If i < kount Then
        ws1.Range(Rows(arr(i, 1) - 3), Rows(arr(i + 1, 1) - 5)).Copy Destination:=ws2.Range("A1")
        ws2.Name = ws2.Cells(3, 1).Value & i
    Else
        ws1.Range(Rows(arr(i, 1) - 3), Rows(arr(i + 1, 1))).Copy Destination:=ws2.Range("A1")
        ws2.Name = ws2.Cells(3, 1).Value & i
    End If
Next i

Application.ScreenUpdating = True
End Sub

Cheers,

Tony
 
Last edited:
Upvote 0

Forum statistics

Threads
1,215,674
Messages
6,126,144
Members
449,294
Latest member
Jitesh_Sharma

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