VBA help Populate dynamic row cells if criteria equals xxx.

htran4

New Member
Joined
Nov 23, 2020
Messages
1
Office Version
  1. 365
Platform
  1. Windows
Hi, I am trying to populate the cells in column C. The range in column A changes every month so i am trying to find a way to dynamically count the amount of rows and then populate the counted cells in column C.

I've attached the picture and current code but can't figure out the rest.


Sub totn_if_example2()

For Each grades In Range("B2:B11")

If grades = "A" Or grades = "B" Then
grades.Offset(0, 1).Value = "Great work"

ElseIf grades = "C" Then
grades.Offset(0, 1).Value = "Needs Improvement"

ElseIf grades = "D" Then
grades.Offset(0, 1).Value = "Time for a Tutor"

Else
grades.Offset(0, 1).Value = " "


End If

Next grades

End Sub
 

Attachments

  • Capture.PNG
    Capture.PNG
    33 KB · Views: 5

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
Ignoring for a moment that you did not declare your variables, which you really should do, the direct answer to your question is to replace this
For Each grades In Range("B2:B11")
with this
For Each grades In Range("B2:B" & cells(rows.count, 2).end(xlUp).row)

There is also the issue of inherent redundancy if the list grows to be quite long and you override the pre-existing entries in column C. This is also work-around-able but the code line suggestion handles your immediate concern.
 
Upvote 0
Try this

VBA Code:
Sub My_Grades()
'Modified  11/24/2020  1:03:16 AM  EST
Dim Lastrow As Long
Lastrow = Cells(Rows.Count, "B").End(xlUp).Row
Dim i As Long

For i = 1 To Lastrow
    With Cells(i, 3)
        Select Case Cells(i, 2).Value

            Case "A", "B": .Value = "Great Work"
            Case "C": .Value = "Needs Improvement"
            Case "D": .Value = "Time for a Tutor"
        Case Else
            .Value = ""

        End Select
    End With
Next

End Sub
 
Upvote 0

Forum statistics

Threads
1,214,385
Messages
6,119,208
Members
448,874
Latest member
b1step2far

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top