SOLVED

Do Until Loop Code

%3CLINGO-SUB%20id%3D%22lingo-sub-3280207%22%20slang%3D%22en-US%22%3EDo%20Until%20Loop%20Code%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3280207%22%20slang%3D%22en-US%22%3E%3CP%3EHello%2C%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI'm%20trying%20to%20create%20a%20Do%20Until%20code%20in%20VBA%20that%20will%20populate%20a%20certain%20value%20in%20column%20A%20until%20a%20criteria%20is%20met%20in%20column%20B%2C%20after%20which%20it%20will%20populate%20a%20second%20value.%20I%20think%20this%20should%20be%20a%20relatively%20quick%20Do%20Until%20code%2C%20but%20unsure%20how%20to%20create%20it.%26nbsp%3B%3C%2FP%3E%3CP%3EBelow%20is%20a%20quick%20example.%26nbsp%3B%3C%2FP%3E%3CP%3EStarting%20at%20row%206%2C%20populate%20column%20A%20with%20F2%20until%20B%3D%22Entity_ID%22.%20After%20that%2C%20populate%20column%20A%20with%20F3%20until%20the%20end%20of%20the%20data%20set.%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22csigman_0-1649432319892.png%22%20style%3D%22width%3A%20400px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F362433i6621C32C0EF0B730%2Fimage-size%2Fmedium%3Fv%3Dv2%26amp%3Bpx%3D400%22%20role%3D%22button%22%20title%3D%22csigman_0-1649432319892.png%22%20alt%3D%22csigman_0-1649432319892.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-3280207%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EExcel%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EMacros%20and%20VBA%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-3285728%22%20slang%3D%22en-US%22%3ERe%3A%20Do%20Until%20Loop%20Code%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3285728%22%20slang%3D%22en-US%22%3EThank%20you!%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-3285120%22%20slang%3D%22en-US%22%3ERe%3A%20Do%20Until%20Loop%20Code%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3285120%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F1357867%22%20target%3D%22_blank%22%3E%40csigman%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EYou%20attached%20a%20.xlsx%20workbook%2C%20so%20it%20wasn't%20macro-enabled.%3C%2FP%3E%0A%3CP%3EThe%20layout%20of%20the%20worksheet%20is%20different%20from%20that%20in%20your%20first%20post%2C%20so%20the%20macro%20has%20to%20be%20updated%20to%20match%20the%20layout.%3C%2FP%3E%0A%3CP%3ESee%20the%20attached%20.xlsm%20workbook.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-3285100%22%20slang%3D%22en-US%22%3ERe%3A%20Do%20Until%20Loop%20Code%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3285100%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F127945%22%20target%3D%22_blank%22%3E%40Hans%20Vogelaar%3C%2FA%3E%26nbsp%3BThank%20you%2C%20I'm%20getting%20an%20error%20when%20I%20try%20to%20run%20the%20code%20(Run-time%20error%201004%20Application%20defined%20or%20object%20defined%20error)%20at%20the%20line%20beginning%20with%20%22For%20r%22.%20I%20did%20update%20the%20columns%20to%20align%20with%20the%20data%20locations%20and%20ensure%20the%20excel%20is%20macro%20enabled.%20Attached%20the%20excel%20I'm%20working%20in.%20I'm%20not%20sure%20what%20is%20causing%20it%2C%20do%20you%20have%20any%20ideas%3F%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-3284699%22%20slang%3D%22en-US%22%3ERe%3A%20Do%20Until%20Loop%20Code%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3284699%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F1357867%22%20target%3D%22_blank%22%3E%40csigman%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EUse%20this%20version%20of%20the%20macro%3A%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-visual-basic%22%3E%3CCODE%3ESub%20FillA()%0A%20%20%20%20Dim%20r%20As%20Long%0A%20%20%20%20Dim%20m%20As%20Long%0A%20%20%20%20Dim%20t%20As%20Long%0A%20%20%20%20Dim%20s%20As%20String%0A%20%20%20%20Application.ScreenUpdating%20%3D%20False%0A%20%20%20%20t%20%3D%202%0A%20%20%20%20s%20%3D%20Range(%22F%22%20%26amp%3B%20t).Value%0A%20%20%20%20m%20%3D%20Range(%22B%22%20%26amp%3B%20Rows.Count).End(xlUp).Row%0A%20%20%20%20For%20r%20%3D%206%20To%20m%0A%20%20%20%20%20%20%20%20If%20Range(%22B%22%20%26amp%3B%20r).Value%20%3D%20%22Entity_ID%22%20Then%0A%20%20%20%20%20%20%20%20%20%20%20%20t%20%3D%20t%20%2B%201%0A%20%20%20%20%20%20%20%20%20%20%20%20s%20%3D%20Range(%22F%22%20%26amp%3B%20t).Value%0A%20%20%20%20%20%20%20%20End%20If%0A%20%20%20%20%20%20%20%20Range(%22A%22%20%26amp%3B%20r).Value%20%3D%20s%0A%20%20%20%20Next%20r%0A%20%20%20%20Application.ScreenUpdating%20%3D%20True%0AEnd%20Sub%3C%2FCODE%3E%3C%2FPRE%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-3284669%22%20slang%3D%22en-US%22%3ERe%3A%20Do%20Until%20Loop%20Code%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3284669%22%20slang%3D%22en-US%22%3EHello%20-%20I%20now%20have%20a%20situation%20where%20I%20need%20to%20update%20the%20state%20name%20to%20a%20new%20name%20every%20time%20I%20reach%20the%20%22entity_ID%22%20cell.%20There%20could%20be%20up%20to%208%20different%20states%20that%20need%20to%20be%20populated%20down%20column%20A.%20Is%20this%20achievable%20with%20the%20formula%3F%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-3280548%22%20slang%3D%22en-US%22%3ERe%3A%20Do%20Until%20Loop%20Code%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3280548%22%20slang%3D%22en-US%22%3EThank%20you!%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-3280219%22%20slang%3D%22en-US%22%3ERe%3A%20Do%20Until%20Loop%20Code%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3280219%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F1357867%22%20target%3D%22_blank%22%3E%40csigman%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EFor%20example%3A%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-visual-basic%22%3E%3CCODE%3ESub%20FillA()%0A%20%20%20%20Dim%20r%20As%20Long%0A%20%20%20%20Dim%20m%20As%20Long%0A%20%20%20%20Dim%20s%20As%20String%0A%20%20%20%20Application.ScreenUpdating%20%3D%20False%0A%20%20%20%20s%20%3D%20Range(%22F2%22).Value%0A%20%20%20%20m%20%3D%20Range(%22B%22%20%26amp%3B%20Rows.Count).End(xlUp).Row%0A%20%20%20%20For%20r%20%3D%206%20To%20m%0A%20%20%20%20%20%20%20%20If%20Range(%22B%22%20%26amp%3B%20r).Value%20%3D%20%22Entity_ID%22%20Then%0A%20%20%20%20%20%20%20%20%20%20%20%20s%20%3D%20Range(%22F3%22).Value%0A%20%20%20%20%20%20%20%20End%20If%0A%20%20%20%20%20%20%20%20Range(%22A%22%20%26amp%3B%20r).Value%20%3D%20s%0A%20%20%20%20Next%20r%0A%20%20%20%20Application.ScreenUpdating%20%3D%20True%0AEnd%20Sub%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3EYou%20could%20also%20use%20a%20formula.%20In%20A6%3A%3C%2FP%3E%0A%3CP%3E%3DIF(COUNTIF(B%246%3AB6%2C%22Entity_ID%22)%2C%24F%243%2C%24F%242)%3C%2FP%3E%0A%3CP%3EDouble-click%20the%20fill%20handle%20of%20A6%20to%20fill%20down.%3C%2FP%3E%3C%2FLINGO-BODY%3E
Occasional Contributor

Hello, 

 

I'm trying to create a Do Until code in VBA that will populate a certain value in column A until a criteria is met in column B, after which it will populate a second value. I think this should be a relatively quick Do Until code, but unsure how to create it. 

Below is a quick example. 

Starting at row 6, populate column A with F2 until B="Entity_ID". After that, populate column A with F3 until the end of the data set. 

 

csigman_0-1649432319892.png

 

7 Replies
best response confirmed by Grahmfs13 (Microsoft)
Solution

@csigman 

For example:

Sub FillA()
    Dim r As Long
    Dim m As Long
    Dim s As String
    Application.ScreenUpdating = False
    s = Range("F2").Value
    m = Range("B" & Rows.Count).End(xlUp).Row
    For r = 6 To m
        If Range("B" & r).Value = "Entity_ID" Then
            s = Range("F3").Value
        End If
        Range("A" & r).Value = s
    Next r
    Application.ScreenUpdating = True
End Sub

You could also use a formula. In A6:

=IF(COUNTIF(B$6:B6,"Entity_ID"),$F$3,$F$2)

Double-click the fill handle of A6 to fill down.

Thank you!
Hello - I now have a situation where I need to update the state name to a new name every time I reach the "entity_ID" cell. There could be up to 8 different states that need to be populated down column A. Is this achievable with the formula?

@csigman 

Use this version of the macro:

Sub FillA()
    Dim r As Long
    Dim m As Long
    Dim t As Long
    Dim s As String
    Application.ScreenUpdating = False
    t = 2
    s = Range("F" & t).Value
    m = Range("B" & Rows.Count).End(xlUp).Row
    For r = 6 To m
        If Range("B" & r).Value = "Entity_ID" Then
            t = t + 1
            s = Range("F" & t).Value
        End If
        Range("A" & r).Value = s
    Next r
    Application.ScreenUpdating = True
End Sub

@Hans Vogelaar Thank you, I'm getting an error when I try to run the code (Run-time error 1004 Application defined or object defined error) at the line beginning with "For r". I did update the columns to align with the data locations and ensure the excel is macro enabled. Attached the excel I'm working in. I'm not sure what is causing it, do you have any ideas?

@csigman 

You attached a .xlsx workbook, so it wasn't macro-enabled.

The layout of the worksheet is different from that in your first post, so the macro has to be updated to match the layout.

See the attached .xlsm workbook.

Thank you!