F
F
figna13202020-07-19 22:24:58
Visual Basic
figna1320, 2020-07-19 22:24:58

How to make a loop for a macro to check the entire column?

There is a macro that fills cells by condition, how to make it check not a certain number of cells, but the entire column? The column will be fixed

Sub Result()
Dim cell As Range
For Each cell In Range("A1:A1000").Cells
If cell.Value >= 3000 Then
cell.Value = "1"
ElseIf cell.Value >= 2000 Then
cell. Value = "2"
ElseIf cell.Value >= 1000 Then
cell.Value = "3"
ElseIf cell.Value >= 700 Then
cell.Value = "4"
ElseIf cell.Value >= 500 Then
cell.Value = "5"
ElseIf cell.Value >= 300 Then
cell.Value = "7"
ElseIf cell.Value >= 100 Then
cell.
cell.Value = "10"
ElseIf cell.Value >= 20 Then
cell.Value = "12"
ElseIf cell.Value <= 19 Then
cell.Value = "15"
End If
Next
End Sub

Answer the question

In order to leave comments, you need to log in

1 answer(s)
B
BasiC2k, 2020-07-20
@BasiC2k

At the beginning of the function add:
Dim lastrow as Long
lastrow = Cells(Rows.Count,"A").End(xlUp).Row
And change
Range("A1:A1000").Cells
to
Range("A1:A" & lastrow ).Cells
Will work if column A does not contain empty cells.
Many other ways to determine the last row of a table: https://techarks.ru/qa/vba/kak-poluchit-kolichestv...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question