Copy datas between 2 keywords and looping until all are extracted

ceclay

Board Regular
Joined
Dec 4, 2019
Messages
58
Office Version
  1. 2016
Platform
  1. Windows
I have this VBA code below which will copy the data between Top * Model and Opens: . My problem with the code below is it only will copy the first set (Top Asian Model) and will not proceed with the next set (Top Australian Model).

VBA Code:
Sub Top_Model()
Application.ScreenUpdating = False
  Dim rStart As Range, rEnd As Range
  Dim r As Long
  Dim last As Long, t As Long
 

 
  Set wph = Sheets("Sheet1")
  Set wso = Sheets("Sheet2")
 
    Set rStart = wph.Columns("A").Find(What:="Top * Model", LookAt:=xlWhole, MatchCase:=False)
  If Not rStart Is Nothing Then
    Set rEnd = wph.Columns("A").Find(What:="Opens:", After:=rStart, LookAt:=xlWhole, SearchDirection:=xlNext, MatchCase:=False)
    If Not rEnd Is Nothing Then
      r = rStart.Row
      Do While r + 2 < rEnd.Row
        wso.Range("A" & Rows.Count).End(xlUp).Offset(1).Resize(, 3).Value = Application.Transpose(wph.Range("A" & r).Resize(3).Value)
        r = r + 3
      Loop
    End If
  End If
 
  wso.Activate
 Application.ScreenUpdating = True
End Sub

Below is the sample list of data and it is dynamic. sometimes there is Top European Model, Top African Model etc,

Test.xlsm
A
3Top Asian Model
4Country
5Height
6Chiharu
7China
86.2
9Ming
10Taiwan
116.1
12Zhao Lei
13Japan
146.25
15Du Juan
16China
176.3
18Liu Wen
19South Korea
205.9
21Opens:
22Greatest Comedian
23Mr Bean
24Josie Long
25Michaela Coel
26Mindy Kaling
27Jordan Brookes
28Matt Berry
29Closes:
30Top Australian Model
31Country
32Height
33Bridget
34Melbourne
355.9
36Kelly
37New South Wales
386.2
39Bambi
40Victoria
416.3
42Opens:
Sheet1


Below is my desired outcome
Test.xlsm
ABC
2Top Asian ModelCountryHeight
3ChiharuChina6.2
4MingTaiwan6.1
5Zhao LeiJapan6.25
6Du JuanChina6.3
7Liu WenSouth Korea5.9
8Top Australian ModelCountryHeight
9BridgetMelbourne5.9
10KellyNew South Wales6.2
11BambiVictoria6.3
Sheet2
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
According to your attachment a VBA demonstration for starters :​
VBA Code:
Sub Demo1()
    Dim W, V(), L&, B As Boolean, R&
        W = Sheet1.UsedRange.Value2
        ReDim V(1 To UBound(W) \ 3, 1 To 3)
    For L = 1 To UBound(W)
            If W(L, 1) = "Opens:" Then B = False Else If W(L, 1) Like "Top *" Then B = True
        If B Then
            R = R + 1
            V(R, 1) = W(L, 1)
            V(R, 2) = W(L + 1, 1)
            L = L + 2
            V(R, 3) = W(L, 1)
        End If
    Next
    With Sheet2
        .[A1:C1].Resize(R).Value2 = V
         If .UsedRange.Rows.Count > R Then .UsedRange.Rows(R + 1 & ":" & .UsedRange.Rows.Count).Clear
        .UsedRange.Columns.AutoFit
    End With
End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,214,649
Messages
6,120,733
Members
448,987
Latest member
marion_davis

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