Do Loop through a table with an If Statement in Excel VBA -Code Included

Опубликовано: 29 Сентябрь 2024
на канале: EverydayVBA
49,641
257

Grab the Free VBA Quick Reference Guide
https://www.chrisjterrell.com/excel-v...
A subscriber to the youtube channel requested this video. The subscriber wanted to use a Do Loop to go through a table of data and if column 1 was the same as the row above column 2 would not change. If it was the same it would do nothing but if it was different then combine column 1 and 2 in column 2.

Code:
==========
Sub DowithIf()

rw = 11
cl = 2
erw = 1000

Do While rw _ erw
Cells(rw, cl + 1).HorizontalAlignment = xlCenter

If Cells(rw, cl) __ Cells(rw - 1, cl) Then
Cells(rw, cl + 1) = Cells(rw, cl) & " - " & Cells(rw, cl + 1)
Cells(rw, cl + 1).Interior.Color = 13431551
ElseIf Cells(rw, cl) = "" Then
Exit Do
End If


rw = rw + 1
Loop

Columns(2).Clear

Columns(3).AutoFit

End Sub