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
 
As both Rory and Rollis have tried to explain that doesn’t tell us anything we need to know.
We are looking in column C for “yes”
My understanding is that you want to fill down column “L” if that is the case make the change I suggested.
Your picture shows column E, what are we supposed to do with E

Please show the results of the macro AND the results you are expecting.
 
Upvote 0

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
This is what I elaborated with what I understood (your statement in post #1 and screenshot post #3). To be helpful I changed the column references in letters instead of numbers as Alex Blakenburg did.
VBA Code:
Option Explicit
Sub test()
    Dim lr As Long, rng As Range, StartRow As Long, EndRow As Long, StartVal, c
    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 = c.Offset(0, 2).Value   'offset here means same row, 2 columns right = column "E"
            Set c = rng.FindNext(c)
            EndRow = IIf(c.Row > StartRow, c.Row, lr)
            Range(Cells(StartRow, "L"), Cells(EndRow, "L")) = StartVal
        Loop While Not c Is Nothing And c.Address <> firstAddress
    End If
End Sub
 
Last edited:
Upvote 0
This is what I elaborated with what I understood (your statement in post #1 and screenshot post #3). To be helpful I changed the column references in letters instead of numbers as Alex Blakenburg did.
VBA Code:
Option Explicit
Sub test()
    Dim lr As Long, rng As Range, StartRow As Long, EndRow As Long, StartVal, c
    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 = c.Offset(0, 2).Value   'offset here means same row, 2 columns right = column "E"
            Set c = rng.FindNext(c)
            EndRow = IIf(c.Row > StartRow, c.Row, lr)
            Range(Cells(StartRow, "L"), Cells(EndRow, "L")) = StartVal
        Loop While Not c Is Nothing And c.Address <> firstAddress
    End If
End Sub
Capture.PNG
 
Upvote 0
Maybe:
VBA Code:
Option Explicit
Sub test()
    Dim lr As Long, rng As Range, StartRow As Long, EndRow As Long, StartVal, c
    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 = c.Offset(0, 9).Value   'offset here means same row, 9 columns right = column "L"
            Set c = rng.FindNext(c)
            EndRow = IIf(c.Row > StartRow, c.Row - 1, lr)
            Range(Cells(StartRow, "L"), Cells(EndRow, "L")) = StartVal
        Loop While c.Address <> firstAddress
    End If
End Sub
 
Upvote 0
Solution
Maybe:
VBA Code:
Option Explicit
Sub test()
    Dim lr As Long, rng As Range, StartRow As Long, EndRow As Long, StartVal, c
    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 = c.Offset(0, 9).Value   'offset here means same row, 9 columns right = column "L"
            Set c = rng.FindNext(c)
            EndRow = IIf(c.Row > StartRow, c.Row - 1, lr)
            Range(Cells(StartRow, "L"), Cells(EndRow, "L")) = StartVal
        Loop While c.Address <> firstAddress
    End If
End Sub
Indeed, this is what is required, thank you 👍 and thank you all for providing assistance.
 
Upvote 0
🤯 Thanks for the positive feedback(y), glad we were able to help.
 
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