r/vba • • 12d ago

Solved loop through a specific range and check if cell is not equal to the one before

Essentially, I want to loop the following code:


If Worksheets("Tabelle1").Range("A11") <> Worksheets("Tabelle1").Range("A10") Then

With Worksheets("Tabelle1").Range("A11:G11").Borders(xlEdgeTop)

.LineStyle = xlContinuous

.Weight = xlMedium

.ColorIndex = 0

End With

End If

If Worksheets("Tabelle1").Range("A12") <> Worksheets("Tabelle1").Range("A11") Then

With Worksheets("Tabelle1").Range("A12:G12").Borders(xlEdgeTop)

.LineStyle = xlContinuous

.Weight = xlMedium

.ColorIndex = 0

End With

End If

[...]

If Worksheets("Tabelle1").Range("A64") <> Worksheets("Tabelle1").Range("A63") Then

With Worksheets("Tabelle1").Range("A64:G64").Borders(xlEdgeTop)

.LineStyle = xlContinuous

.Weight = xlMedium

.ColorIndex = 0

End With

End If

End Sub

This is what I'm currently working with, which is obviously just not very elegant and efficient. I'm assuming there is a way to loop through this, but I'm not proficient enough with VBA to figure it out myself. Can anyone give me some advice?

Thank you in advance.

4 Upvotes

13 comments sorted by

2

u/fanpages 240 12d ago edited 12d ago

...I'm assuming there is a way to loop through this...

What is the context of the looping?

Rows, columns, all cells in a (named) range, tables, list objects, worksheets, and/or workbooks, or something else?

Your thread title suggests: "loop through a specific range and check if cell is not equal to the one before".

How is the subroutine border() executed now? Manually, or from a different subroutine/function?

How is the 'specific range' going to be established?

Will the 'specific range' always be fixed, or will it vary?


...I'm not proficient enough with VBA to figure it out myself...

What have you tried already?

1

u/aloilisia 12d ago

Essentially, I have a table that covers four priority areas (0, 1, 2, 3) with different names. These priorities change monthly - so each name cycles through them every month. I basically just want a bold line separating each priority. I've tried making it work with conditional formatting, but without success.

I want the macro to loop through each row (until AG) within the range of A10:AG64.

There's rows being hidden when they are empty so each month there's quite a bit of change, which results in a different table each month, which is why I wrote the code I've written so far. It works just fine for my purposes, it's just a bit long for something that can probably be done in a loop.

I hope that makes sense? I find it a bit difficult to describe everything in english, since I'm not a native speaker

1

u/ankokudaishogun 11d ago

I want the macro to loop through each row (until AG) within the range of A10:AG64.

AG is columns, not rows though.

That said, if I understand correctly your needs: loop through rows 10 to 64, if the previous row's A cell is different then apply formatting to the row from column A to column AG.

Option Explicit
Public Sub LoopTheLoop()

    Const START_ROW As Long = 10
    Const LAST_ROW As Long = 64
    Const FIRST_COLUMN As String = "A"
    Const LAST_COLUMN As String = "AG"

    Dim Index As Long

    With ThisWorkbook.Worksheets.Item("Tabelle1")
        For Index = START_ROW - 1 To LAST_ROW
            If Not .Range(FIRST_COLUMN & Index).Value2 = .Range(FIRST_COLUMN & Index - 1).Value2 Then
                With .Range(FIRST_COLUMN & Index, LAST_COLUMN & Index).Borders(xlEdgeTop)
                    .LineStyle = xlContinuous
                    .Weight = xlMedium
                    .ColorIndex = 0
                End With
            End If
        Next
    End With
End Sub

Of course I see other already have answered your question, but an extra option is never bad.

1

u/idontreallyknow6969 1 12d ago

I think I see what you’re doing. If it’s from row 11 to 64 I’d just do a for loop something like

Dim ws as Worksheet

Set ws = ThisWorkbook.Sheets(“Tabelle1”)

Dim formatRange as Range

Dim i as Long

For i = 11 to 64

If ws.Range(“A” & i).Value <> ws.Range(“A” & i - 1).Value Then

Set formatRange = ws.Range(“A” & i & “:G” & i)

With formatRange

your formatting

End With

End If

Next i

2

u/aloilisia 12d ago edited 12d ago

This works perfectly! Thank you very much, you're a saint.

1

u/aloilisia 11d ago

Solution verified

1

u/reputatorbot 11d ago

You have awarded 1 point to idontreallyknow6969.


I am a bot - please contact the mods with any questions

1

u/idontreallyknow6969 1 12d ago

This assumes your table is always A10:G64

0

u/[deleted] 12d ago

[deleted] — view deleted comment

1

u/fanpages 240 11d ago

FYI: "About Clippy(Points)"


...When the OP is satisfied with an answer that is given to their question, they can award a ClippyPoint by responding to the comment with:

Solution Verified

This will let Clippy know that the individual that the OP responded is be awarded a point. Clippy reads the current users flair and adds one point. Clippy also changes the post flair to 'solved'. The OP has the option to award as many points per thread as they like...


1

u/aloilisia 11d ago

Thank you! I wasn't sure how it works here

1

u/TastiSqueeze 3 12d ago edited 12d ago

Define the variables as needed. Code below seems to work. You may want to tweak it a bit as this will insert a line after your last row of data.

Sub lineit()
    For Each usedcell In Sheets("Tabelle1").Range("A11:A65") ' only use A11:A65 if this is the exact range to always modify
        If usedcell.Value <> usedcell.Offset(1, 0).Value Then
           With Worksheets("Tabelle1").Range(Cells(usedcell.Row + 1, 1), Cells(usedcell.Row + 1, 7)).Borders(xlEdgeTop)
              .LineStyle = xlContinuous
              .Weight = xlMedium
              .ColorIndex = 0
           End With
        End If
    Next usedcell
End Sub

1

u/aloilisia 12d ago

This gives every visible row a bold line and not just the ones where it changes from 0 to 1, from 1 to 2 etc

1

u/TastiSqueeze 3 12d ago

Double check that you moved the macro properly and especially that the "if usedcell.value" row is correct. Then check your data to see if each row is actually different from the previous row. There is a good chance your data in each cell is different in some aspect.