Copy cell text in a range until specific text

IreneFoncillas

New Member
Joined
Oct 13, 2020
Messages
13
Office Version
  1. 365
Platform
  1. Windows
Hi all,

I am not able to write a vba code for a specific action I would like to do in excel. I attach two screenshots to explain myself better with an example.

Screenshot_1: I want to write text "Level_1" in column A, until in column B the text "Level_2" is found. And then, do the same but with text "Level_2" until text "Level_3" is found. In this example, there are 5 rows between levels, but sometimes there are more or less rows, so it is not a fixed number.

Screenshot_2: This is the result I want to get, once macro has been run.

Thank you very much!!
 

Attachments

  • Screenshot_1.png
    Screenshot_1.png
    7.5 KB · Views: 44
  • Screenshot_2.png
    Screenshot_2.png
    6.9 KB · Views: 44

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.
Are all the cells between the Levels numbers?
 
Upvote 0
Something like this perhaps
VBA Code:
Sub DoLevel()

    Dim rng     As Range
    Dim c       As Range
    Dim sLev    As String
    
    With ThisWorkbook.Sheets("Sheet1")
        Set rng = Application.Intersect(.UsedRange, .Columns(2))
    End With

    For Each c In rng
        If StrComp(Left(c.Value2, 5), "level", vbTextCompare) = 0 Then
            sLev = c.Value2
            c.Offset(0, -1).Value = ""
            c.Offset(1, -1).Value = c.Value
        Else
            c.Offset(1, -1).Value = sLev
        End If
    Next c
End Sub
 
Upvote 0
If the values between the levels are numbers, you could also use
VBA Code:
Sub IreneFoncillas()
   Dim Rng As Range
   For Each Rng In Range("B1", Range("B" & Rows.Count)).SpecialCells(xlConstants, xlNumbers).Areas
      Rng.Offset(, -1).Value = Rng.Offset(-1).Resize(1)
   Next Rng
End Sub
 
Upvote 0
Thank you both for your responses. I have changed some text in the code you provided to match my data, and they work!!
 
Upvote 0
Glad we could help & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,184
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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