H4: =IF(J4="","AAA",IF($D$3="A","AAA",IF($D$3="B","BBB","")))
Copy H4 down
I assume you mean you changed D3 not D4?
It works fine for me
Did you copy H4 down?
Sub test()
Dim c, rng As Range
Dim lastrow, max As Integer
max = Application.WorksheetFunction.max(Columns("J"))
lastrow = Columns("J").Cells(Rows.Count).End(xlUp).Row
Set rng = Range("H4:H" & lastrow)
For Each c In rng.Cells
If c.Offset(0, 2).Value = max Then
c.FormulaLocal = "=REPT($D$3,3)"
c.Value = c.Value
End If
Next c
End Sub
Hi @pencari
Maybe something like this:
Code:Sub test() Dim c, rng As Range Dim lastrow, max As Integer max = Application.WorksheetFunction.max(Columns("J")) lastrow = Columns("J").Cells(Rows.Count).End(xlUp).Row Set rng = Range("H4:H" & lastrow) For Each c In rng.Cells If c.Offset(0, 2).Value = max Then c.FormulaLocal = "=REPT($D$3,3)" c.Value = c.Value End If Next c End Sub
When you add 2 to J and change D3 to "B", run macro and you get D3 repeated 3 times "BBB"
If you then change D3 to "C" and add 3 to J, run the macro and you get "CCC" and so on
View attachment 36345
Sub test()
Dim c, rng As Range
Dim lastrow, max As Integer
max = Application.WorksheetFunction.max(Columns("J"))
lastrow = 86
Set rng = Range("H4:H" & lastrow)
For Each c In rng.Cells
c.Value = c.Value
If c.Offset(0, 2).Value = max Or c.Offset(0, 2).Value = "" Then
c.FormulaLocal = "=repetir($D$3;3)"
End If
Next c
End Sub
@pencari
If you are planning to have 1,2,3,4,5... in column J it is not a good ideia to use IF's as you need to keep adding conditions to accommodate the new entries
In this case you can run the code below after adding the new number to column "J" and before changing "D3"... so if you were to add "2" to column "J", run the code after adding "2" and then change "D3" to B. This time i had the macro leave the formula in the cells.
You can change the formula if you wish.
Code:Sub test() Dim c, rng As Range Dim lastrow, max As Integer max = Application.WorksheetFunction.max(Columns("J")) lastrow = 86 Set rng = Range("H4:H" & lastrow) For Each c In rng.Cells c.Value = c.Value If c.Offset(0, 2).Value = max Or c.Offset(0, 2).Value = "" Then c.FormulaLocal = "=repetir($D$3;3)" End If Next c End Sub
Hi @pencari
Here you go... run macro to convert to values the contents of "H" which have something in J. The rest remains with formula and will show result when something is entered in J