Need help looping macro












-1














I don't believe this is very difficult but I can't figure it out...



In column B, I have either "Original" or "Add" listed. Starting from B79 and moving upwards, the first time "Original" is displayed I want to draw a border from B#:N# on the bottom.



I don't know how to run proper loops within VBA so below is what I have so far which is missing quite a bit.



Sub Test()    
Range("B79").Select
If Range("B79") = "Original" Then
Selection.End(xlToRight).Select
Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Selection.Borders(xlDiagonalUp).LineStyle = xlNone
Selection.Borders(xlEdgeLeft).LineStyle = xlNone
With Selection.Borders(xlEdgeTop)
.LineStyle = xlDot
.ColorIndex = xlAutomatic
.TintAndShade = 0
.Weight = xlThin
End With
Selection.Borders(xlEdgeBottom).LineStyle = xlNone
Selection.Borders(xlEdgeRight).LineStyle = xlNone
Selection.Borders(xlInsideVertical).LineStyle = xlNone
Selection.Borders(xlInsideHorizontal).LineStyle = xlNone

Else: ActiveCell.Offset(-1, 0).Select
End If

End Sub


Here is my current attempt. I am just trying to get it to highlight the cells.



Sub Test()

Let x = 79
Do While x > 7
If ("B" & x) = "Original" > 0 Then
Selection.End(xlToRight).Select
Else: x = x - 1
End If
Loop

End Sub









share|improve this question




















  • 1




    Researching about "VBA loops" should tell you everything you ever dreamed to know about For...Next, Do...Loop, and every one of the 6 or 7 different (yet very similar) loop constructs in VBA. What have you tried, and how is it failing/not working? Can concatenate a number into a string to make a Range address? If so, then you can do For i = 79 To 1 Step -1....Next, and use the value of i to track the "current row".
    – Mathieu Guindon
    Nov 12 '18 at 18:11












  • I wouldn't be posting on this site if I haven't already tried researching this on my own... I also didn't want to post my fruitless attempts which would cause extra confusion. I've been able to cause a loop but not end it.
    – Matt9080
    Nov 12 '18 at 18:29












  • On the contrary, posting your fruitless attempts shows us that you've tried something and how and why it's failing, as opposed to looking like you're just asking for others to do the work for you - the more effort you've put in, the less your question looks like "gimmeh teh codez" and the more well-received it gets =)
    – Mathieu Guindon
    Nov 12 '18 at 18:31










  • That said you'll want to avoid coding against Selection, for more robust and much simpler code.
    – Mathieu Guindon
    Nov 12 '18 at 18:34
















-1














I don't believe this is very difficult but I can't figure it out...



In column B, I have either "Original" or "Add" listed. Starting from B79 and moving upwards, the first time "Original" is displayed I want to draw a border from B#:N# on the bottom.



I don't know how to run proper loops within VBA so below is what I have so far which is missing quite a bit.



Sub Test()    
Range("B79").Select
If Range("B79") = "Original" Then
Selection.End(xlToRight).Select
Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Selection.Borders(xlDiagonalUp).LineStyle = xlNone
Selection.Borders(xlEdgeLeft).LineStyle = xlNone
With Selection.Borders(xlEdgeTop)
.LineStyle = xlDot
.ColorIndex = xlAutomatic
.TintAndShade = 0
.Weight = xlThin
End With
Selection.Borders(xlEdgeBottom).LineStyle = xlNone
Selection.Borders(xlEdgeRight).LineStyle = xlNone
Selection.Borders(xlInsideVertical).LineStyle = xlNone
Selection.Borders(xlInsideHorizontal).LineStyle = xlNone

Else: ActiveCell.Offset(-1, 0).Select
End If

End Sub


Here is my current attempt. I am just trying to get it to highlight the cells.



Sub Test()

Let x = 79
Do While x > 7
If ("B" & x) = "Original" > 0 Then
Selection.End(xlToRight).Select
Else: x = x - 1
End If
Loop

End Sub









share|improve this question




















  • 1




    Researching about "VBA loops" should tell you everything you ever dreamed to know about For...Next, Do...Loop, and every one of the 6 or 7 different (yet very similar) loop constructs in VBA. What have you tried, and how is it failing/not working? Can concatenate a number into a string to make a Range address? If so, then you can do For i = 79 To 1 Step -1....Next, and use the value of i to track the "current row".
    – Mathieu Guindon
    Nov 12 '18 at 18:11












  • I wouldn't be posting on this site if I haven't already tried researching this on my own... I also didn't want to post my fruitless attempts which would cause extra confusion. I've been able to cause a loop but not end it.
    – Matt9080
    Nov 12 '18 at 18:29












  • On the contrary, posting your fruitless attempts shows us that you've tried something and how and why it's failing, as opposed to looking like you're just asking for others to do the work for you - the more effort you've put in, the less your question looks like "gimmeh teh codez" and the more well-received it gets =)
    – Mathieu Guindon
    Nov 12 '18 at 18:31










  • That said you'll want to avoid coding against Selection, for more robust and much simpler code.
    – Mathieu Guindon
    Nov 12 '18 at 18:34














-1












-1








-1







I don't believe this is very difficult but I can't figure it out...



In column B, I have either "Original" or "Add" listed. Starting from B79 and moving upwards, the first time "Original" is displayed I want to draw a border from B#:N# on the bottom.



I don't know how to run proper loops within VBA so below is what I have so far which is missing quite a bit.



Sub Test()    
Range("B79").Select
If Range("B79") = "Original" Then
Selection.End(xlToRight).Select
Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Selection.Borders(xlDiagonalUp).LineStyle = xlNone
Selection.Borders(xlEdgeLeft).LineStyle = xlNone
With Selection.Borders(xlEdgeTop)
.LineStyle = xlDot
.ColorIndex = xlAutomatic
.TintAndShade = 0
.Weight = xlThin
End With
Selection.Borders(xlEdgeBottom).LineStyle = xlNone
Selection.Borders(xlEdgeRight).LineStyle = xlNone
Selection.Borders(xlInsideVertical).LineStyle = xlNone
Selection.Borders(xlInsideHorizontal).LineStyle = xlNone

Else: ActiveCell.Offset(-1, 0).Select
End If

End Sub


Here is my current attempt. I am just trying to get it to highlight the cells.



Sub Test()

Let x = 79
Do While x > 7
If ("B" & x) = "Original" > 0 Then
Selection.End(xlToRight).Select
Else: x = x - 1
End If
Loop

End Sub









share|improve this question















I don't believe this is very difficult but I can't figure it out...



In column B, I have either "Original" or "Add" listed. Starting from B79 and moving upwards, the first time "Original" is displayed I want to draw a border from B#:N# on the bottom.



I don't know how to run proper loops within VBA so below is what I have so far which is missing quite a bit.



Sub Test()    
Range("B79").Select
If Range("B79") = "Original" Then
Selection.End(xlToRight).Select
Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Selection.Borders(xlDiagonalUp).LineStyle = xlNone
Selection.Borders(xlEdgeLeft).LineStyle = xlNone
With Selection.Borders(xlEdgeTop)
.LineStyle = xlDot
.ColorIndex = xlAutomatic
.TintAndShade = 0
.Weight = xlThin
End With
Selection.Borders(xlEdgeBottom).LineStyle = xlNone
Selection.Borders(xlEdgeRight).LineStyle = xlNone
Selection.Borders(xlInsideVertical).LineStyle = xlNone
Selection.Borders(xlInsideHorizontal).LineStyle = xlNone

Else: ActiveCell.Offset(-1, 0).Select
End If

End Sub


Here is my current attempt. I am just trying to get it to highlight the cells.



Sub Test()

Let x = 79
Do While x > 7
If ("B" & x) = "Original" > 0 Then
Selection.End(xlToRight).Select
Else: x = x - 1
End If
Loop

End Sub






excel vba loops






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 12 '18 at 21:09







Matt9080

















asked Nov 12 '18 at 17:56









Matt9080Matt9080

255




255








  • 1




    Researching about "VBA loops" should tell you everything you ever dreamed to know about For...Next, Do...Loop, and every one of the 6 or 7 different (yet very similar) loop constructs in VBA. What have you tried, and how is it failing/not working? Can concatenate a number into a string to make a Range address? If so, then you can do For i = 79 To 1 Step -1....Next, and use the value of i to track the "current row".
    – Mathieu Guindon
    Nov 12 '18 at 18:11












  • I wouldn't be posting on this site if I haven't already tried researching this on my own... I also didn't want to post my fruitless attempts which would cause extra confusion. I've been able to cause a loop but not end it.
    – Matt9080
    Nov 12 '18 at 18:29












  • On the contrary, posting your fruitless attempts shows us that you've tried something and how and why it's failing, as opposed to looking like you're just asking for others to do the work for you - the more effort you've put in, the less your question looks like "gimmeh teh codez" and the more well-received it gets =)
    – Mathieu Guindon
    Nov 12 '18 at 18:31










  • That said you'll want to avoid coding against Selection, for more robust and much simpler code.
    – Mathieu Guindon
    Nov 12 '18 at 18:34














  • 1




    Researching about "VBA loops" should tell you everything you ever dreamed to know about For...Next, Do...Loop, and every one of the 6 or 7 different (yet very similar) loop constructs in VBA. What have you tried, and how is it failing/not working? Can concatenate a number into a string to make a Range address? If so, then you can do For i = 79 To 1 Step -1....Next, and use the value of i to track the "current row".
    – Mathieu Guindon
    Nov 12 '18 at 18:11












  • I wouldn't be posting on this site if I haven't already tried researching this on my own... I also didn't want to post my fruitless attempts which would cause extra confusion. I've been able to cause a loop but not end it.
    – Matt9080
    Nov 12 '18 at 18:29












  • On the contrary, posting your fruitless attempts shows us that you've tried something and how and why it's failing, as opposed to looking like you're just asking for others to do the work for you - the more effort you've put in, the less your question looks like "gimmeh teh codez" and the more well-received it gets =)
    – Mathieu Guindon
    Nov 12 '18 at 18:31










  • That said you'll want to avoid coding against Selection, for more robust and much simpler code.
    – Mathieu Guindon
    Nov 12 '18 at 18:34








1




1




Researching about "VBA loops" should tell you everything you ever dreamed to know about For...Next, Do...Loop, and every one of the 6 or 7 different (yet very similar) loop constructs in VBA. What have you tried, and how is it failing/not working? Can concatenate a number into a string to make a Range address? If so, then you can do For i = 79 To 1 Step -1....Next, and use the value of i to track the "current row".
– Mathieu Guindon
Nov 12 '18 at 18:11






Researching about "VBA loops" should tell you everything you ever dreamed to know about For...Next, Do...Loop, and every one of the 6 or 7 different (yet very similar) loop constructs in VBA. What have you tried, and how is it failing/not working? Can concatenate a number into a string to make a Range address? If so, then you can do For i = 79 To 1 Step -1....Next, and use the value of i to track the "current row".
– Mathieu Guindon
Nov 12 '18 at 18:11














I wouldn't be posting on this site if I haven't already tried researching this on my own... I also didn't want to post my fruitless attempts which would cause extra confusion. I've been able to cause a loop but not end it.
– Matt9080
Nov 12 '18 at 18:29






I wouldn't be posting on this site if I haven't already tried researching this on my own... I also didn't want to post my fruitless attempts which would cause extra confusion. I've been able to cause a loop but not end it.
– Matt9080
Nov 12 '18 at 18:29














On the contrary, posting your fruitless attempts shows us that you've tried something and how and why it's failing, as opposed to looking like you're just asking for others to do the work for you - the more effort you've put in, the less your question looks like "gimmeh teh codez" and the more well-received it gets =)
– Mathieu Guindon
Nov 12 '18 at 18:31




On the contrary, posting your fruitless attempts shows us that you've tried something and how and why it's failing, as opposed to looking like you're just asking for others to do the work for you - the more effort you've put in, the less your question looks like "gimmeh teh codez" and the more well-received it gets =)
– Mathieu Guindon
Nov 12 '18 at 18:31












That said you'll want to avoid coding against Selection, for more robust and much simpler code.
– Mathieu Guindon
Nov 12 '18 at 18:34




That said you'll want to avoid coding against Selection, for more robust and much simpler code.
– Mathieu Guindon
Nov 12 '18 at 18:34












1 Answer
1






active

oldest

votes


















1














Use a for next loop and don't select, this should do what you need. Make sure you read this code and understand how it relates back to your original code.



Sub Test()
Dim X As Long
For X = 79 To 1 Step -1 'Step -1 makes it go backwards
If Range("B" & X).Text = "Original" Then 'Notice I am not actually selecting anything in this code, I don't need to in order to manipulate it
With Range("B" & X).End(xlToRight)
For Each Border In .Borders 'Loop the borders so you don't have to name each one
Border.LineStyle = xlNone
Next
With .Borders(xlEdgeTop)
.LineStyle = xlDot
.ColorIndex = xlAutomatic
.TintAndShade = 0
.Weight = xlThin
End With
End With
End If
Next
End Sub





share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53267612%2fneed-help-looping-macro%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    Use a for next loop and don't select, this should do what you need. Make sure you read this code and understand how it relates back to your original code.



    Sub Test()
    Dim X As Long
    For X = 79 To 1 Step -1 'Step -1 makes it go backwards
    If Range("B" & X).Text = "Original" Then 'Notice I am not actually selecting anything in this code, I don't need to in order to manipulate it
    With Range("B" & X).End(xlToRight)
    For Each Border In .Borders 'Loop the borders so you don't have to name each one
    Border.LineStyle = xlNone
    Next
    With .Borders(xlEdgeTop)
    .LineStyle = xlDot
    .ColorIndex = xlAutomatic
    .TintAndShade = 0
    .Weight = xlThin
    End With
    End With
    End If
    Next
    End Sub





    share|improve this answer




























      1














      Use a for next loop and don't select, this should do what you need. Make sure you read this code and understand how it relates back to your original code.



      Sub Test()
      Dim X As Long
      For X = 79 To 1 Step -1 'Step -1 makes it go backwards
      If Range("B" & X).Text = "Original" Then 'Notice I am not actually selecting anything in this code, I don't need to in order to manipulate it
      With Range("B" & X).End(xlToRight)
      For Each Border In .Borders 'Loop the borders so you don't have to name each one
      Border.LineStyle = xlNone
      Next
      With .Borders(xlEdgeTop)
      .LineStyle = xlDot
      .ColorIndex = xlAutomatic
      .TintAndShade = 0
      .Weight = xlThin
      End With
      End With
      End If
      Next
      End Sub





      share|improve this answer


























        1












        1








        1






        Use a for next loop and don't select, this should do what you need. Make sure you read this code and understand how it relates back to your original code.



        Sub Test()
        Dim X As Long
        For X = 79 To 1 Step -1 'Step -1 makes it go backwards
        If Range("B" & X).Text = "Original" Then 'Notice I am not actually selecting anything in this code, I don't need to in order to manipulate it
        With Range("B" & X).End(xlToRight)
        For Each Border In .Borders 'Loop the borders so you don't have to name each one
        Border.LineStyle = xlNone
        Next
        With .Borders(xlEdgeTop)
        .LineStyle = xlDot
        .ColorIndex = xlAutomatic
        .TintAndShade = 0
        .Weight = xlThin
        End With
        End With
        End If
        Next
        End Sub





        share|improve this answer














        Use a for next loop and don't select, this should do what you need. Make sure you read this code and understand how it relates back to your original code.



        Sub Test()
        Dim X As Long
        For X = 79 To 1 Step -1 'Step -1 makes it go backwards
        If Range("B" & X).Text = "Original" Then 'Notice I am not actually selecting anything in this code, I don't need to in order to manipulate it
        With Range("B" & X).End(xlToRight)
        For Each Border In .Borders 'Loop the borders so you don't have to name each one
        Border.LineStyle = xlNone
        Next
        With .Borders(xlEdgeTop)
        .LineStyle = xlDot
        .ColorIndex = xlAutomatic
        .TintAndShade = 0
        .Weight = xlThin
        End With
        End With
        End If
        Next
        End Sub






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 12 '18 at 23:38

























        answered Nov 12 '18 at 23:32









        Dan DonoghueDan Donoghue

        4,6111631




        4,6111631






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53267612%2fneed-help-looping-macro%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Florida Star v. B. J. F.

            Danny Elfman

            Lugert, Oklahoma