Problem executing code vba

sofas

Well-known Member
Joined
Sep 11, 2022
Messages
559
Office Version
  1. 2021
  2. 2019
Platform
  1. Windows
hi how can i modify the code to search in column (C )And put the result in an column (L)

VBA Code:
Sub test()
    Dim lr As Long, rng As Range, StartRow As Long, EndRow As Long, StartVal, c
    lr = Cells(Rows.Count, 1).End(xlUp).Row
    Set rng = Range(Cells(1, 1), Cells(lr, 1))
    
    Set c = Columns(1).Find("yes", after:=Cells(lr, 1), LookIn:=xlValues, LookAt:=xlWhole)
    If Not c Is Nothing Then
        firstAddress = c.Address
        Do
            StartRow = c.Row
            StartVal = c.Offset(0, 5).Value
            Set c = rng.FindNext(c)
            EndRow = IIf(c.Row > StartRow, c.Row, lr)
            Range(Cells(StartRow - 0, 12), Cells(EndRow - 0, 12)) = StartVal
        Loop While Not c Is Nothing And c.Address <> firstAddress
    End If
End Sub
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
Since I have no idea about the layout of your data I'll take a guess. Change the search line to this where 3 = column C :
VBA Code:
Set c = Columns(3).Find("yes", After:=Cells(lr, 3), LookIn:=xlValues, LookAt:=xlWhole)
 
Upvote 0
Since I have no idea about the layout of your data I'll take a guess. Change the search line to this where 3 = column C :
VBA Code:
Set c = Columns(3).Find("yes", After:=Cells(lr, 3), LookIn:=xlValues, LookAt:=xlWhole)
I want to repeat the value provided there is a word yes
5.png
 
Upvote 0
So what? What's wrong with my suggestion?
A screenshot is of no help if you don't show at least the whole used range and say what goes where.
 
Upvote 0
So what? What's wrong with my suggestion?
A screenshot is of no help if you don't show at least the whole used range and say what goes where.
Sorry for not being able to communicate the idea. I just want when checking for the presence of the word Yes in column A, the corresponding value in column L is repeated several times until reaching the same word Yes, then re-execute the same command on the corresponding value, the other word yes, and so on
 
Upvote 0
I just want when checking for the presence of the word Yes in column A, the corresponding value in column L is repeated
Your current code appears to do exactly that. If you want to look in column C instead of column A, then you have been given code for that. If that code is not doing what you want, then you need to explain in what way it is not working.
 
Upvote 0
See if this helps:

VBA Code:
Sub test()
    Dim lr As Long, rng As Range, StartRow As Long, EndRow As Long, StartVal, c as Range    
    Dim firstAddress As String
    
    lr = Cells(Rows.Count, "C").End(xlUp).Row
    Set rng = Range(Cells(1, "C"), Cells(lr, "C"))
    
    Set c = Columns("C").Find("yes", after:=Cells(lr, "C"), LookIn:=xlValues, LookAt:=xlWhole)
    If Not c Is Nothing Then
        firstAddress = c.Address
        Do
            StartRow = c.Row
            StartVal = Cells(StartRow, "E").Value
            Set c = rng.FindNext(c)
            EndRow = IIf(c.Row > StartRow, c.Row, lr)
            Range(Cells(StartRow - 0, "L"), Cells(EndRow - 0, "L")) = StartVal
        Loop While c.Address <> firstAddress
    End If
End Sub
 
Upvote 0
See if this helps:

VBA Code:
Sub test()
    Dim lr As Long, rng As Range, StartRow As Long, EndRow As Long, StartVal, c as Range   
    Dim firstAddress As String
   
    lr = Cells(Rows.Count, "C").End(xlUp).Row
    Set rng = Range(Cells(1, "C"), Cells(lr, "C"))
   
    Set c = Columns("C").Find("yes", after:=Cells(lr, "C"), LookIn:=xlValues, LookAt:=xlWhole)
    If Not c Is Nothing Then
        firstAddress = c.Address
        Do
            StartRow = c.Row
            StartVal = Cells(StartRow, "E").Value
            Set c = rng.FindNext(c)
            EndRow = IIf(c.Row > StartRow, c.Row, lr)
            Range(Cells(StartRow - 0, "L"), Cells(EndRow - 0, "L")) = StartVal
        Loop While c.Address <> firstAddress
    End If
End Sub
Thank you, the code works fine when copying the values from column E. I want the same values as those in column L It is repeated with an existing condition in column C
 
Upvote 0
I have logged off for the night. If you are saying that you want to copy the value from the yes row and L column down the L column, then just change the StartVal line from column E to column L
VBA Code:
  StartVal = Cells(StartRow, "L").Value
 
Upvote 0
I have logged off for the night. If you are saying that you want to copy the value from the yes row and L column down the L column, then just change the StartVal line from column E to column L
VBA Code:
  StartVal = Cells(StartRow, "L").Value
 
Upvote 0

Forum statistics

Threads
1,224,802
Messages
6,181,054
Members
453,014
Latest member
Chris258

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