VBA Formula

Peter_SSs

MrExcel MVP, Moderator
Joined
May 28, 2005
Messages
41,790
Office Version
365
Platform
Windows
No a big issue but I probably should have written that line more directly (& shorter ;))

Rich (BB code):
Sub YesLast4_v3()
  With ActiveSheet.UsedRange.Offset(3, 1)
    <del>.Value = Evaluate(Replace(Replace("if(#="""","""",if(@="""",""Yes"",""""))", "#", .Address), "@", .Offset(4).Address))</del>
    .Value = Evaluate("if(" & .Address & "="""","""",if(" & .Offset(4).Address & "="""",""Yes"",""""))")
  End With
End Sub
 

Vishaal

Active Member
Joined
Mar 16, 2019
Messages
260
Peter_SSs Sir Ji,

i forget one thing with my data, i have changed in column C, in that situation what modification required in your code (#21)because we are using "four Yes" and there is only two value

Excel 2010 32 bit
A
B
C
D
E
1
S.No.​
2
3
4
1​
25​
5​
8​
20​
5
2​
30​
10​
10​
23​
6
3​
35​
12​
26​
7
4​
40​
14​
29​
8
5​
45​
16​
31​
9
6​
50​
18​
33​
10
7​
55​
19​
36​
11
8​
60​
39​
12
9​
65​
41​
13
10​
70​
42​
14
11​
43​
15
12​
Sheet: Sheet1

advice pls

and if we need "Three or one" yes what modification required in your following code

Sub YesLast4_v3()
With ActiveSheet.UsedRange.Offset(3, 1)
.Value = Evaluate("if(" & .Address & "="""","""",if(" & .Offset(4).Address & "="""",""Yes"",""""))")
End With
End Sub

 
Last edited:

Rick Rothstein

MrExcel MVP
Joined
Apr 18, 2011
Messages
35,426
Office Version
2010
Platform
Windows
I have modified the code I posted earlier and I believe it will now do what you want correctly...
Code:
Sub YesLast4Max()
  Dim LR As Long, C As Long
  For C = 2 To Cells(4, Columns.Count).End(xlToLeft).Column
    If Len(Cells(4, C)) Then
      LR = Columns(C).Find("*", , xlValues, , xlRows, xlPrevious).Row
      With Range(Cells(4, C), Cells(LR, C))
        .Value = Evaluate("IF(ROW(" & .Address & ")>MAX(3," & LR & "-4),""Yes"","""")")
      End With
    End If
  Next
End Sub
 

Peter_SSs

MrExcel MVP, Moderator
Joined
May 28, 2005
Messages
41,790
Office Version
365
Platform
Windows
and if we need "Three or one" yes
What does that mean?

What are the expected results for your last sample data?
 
Last edited:

Vishaal

Active Member
Joined
Mar 16, 2019
Messages
260
What does that mean?

What are the expected results for your last sample data?
same as previous, my problem is in column c, there is only two entry and if we use code, it will paste four yes, which is wrong

advice pls advice on if we need "Three or one" yes what modification required in your following code
 

Vishaal

Active Member
Joined
Mar 16, 2019
Messages
260
I have modified the code I posted earlier and I believe it will now do what you want correctly...
Code:
Sub YesLast4Max()
  Dim LR As Long, C As Long
  For C = 2 To Cells(4, Columns.Count).End(xlToLeft).Column
    If Len(Cells(4, C)) Then
      LR = Columns(C).Find("*", , xlValues, , xlRows, xlPrevious).Row
      With Range(Cells(4, C), Cells(LR, C))
        .Value = Evaluate("IF(ROW(" & .Address & ")>MAX(3," & LR & "-4),""Yes"","""")")
      End With
    End If
  Next
End Sub

Thanks Rick Rothstein Sir Ji, its working for me, thanks for your support.

One query is still pending and that is if we need "Three or one" yes what modification required in your following code

i mean only "One Yes" not four
 

Peter_SSs

MrExcel MVP, Moderator
Joined
May 28, 2005
Messages
41,790
Office Version
365
Platform
Windows
.. my problem is in column c, there is only two entry and if we use code, it will paste four yes, ..
Not with my code ....

Before:

Excel Workbook
ABCDE
1S.No.25689365478256925698
22564214514251478
3
41255820
5230101023
63351226
74401429
85451631
96501833
107551936
1186039
1296541
13107042
141143
1512
Yes





After:

Excel Workbook
ABCDE
1S.No.25689365478256925698
22564214514251478
3
41Yes
52Yes
63
74Yes
85Yes
96Yes
107YesYes
118YesYes
129YesYes
1310YesYes
1411Yes
1512
Yes





.. that is unless those 'empty' cells actually contain space characters??
 
Last edited:

Rick Rothstein

MrExcel MVP
Joined
Apr 18, 2011
Messages
35,426
Office Version
2010
Platform
Windows
One query is still pending and that is if we need "Three or one" yes what modification required in your following code

i mean only "One Yes" not four
Use this code instead of what I gave you earlier and replace the red number 4 with the number of yeses you want...
Code:
Sub YesLast4Max()
  Dim LR As Long, C As Long, Yeses As Long
  Yeses = [B][COLOR="#FF0000"][SIZE=2]4[/SIZE][/COLOR][/B]
  For C = 2 To Cells(4, Columns.Count).End(xlToLeft).Column
    If Len(Cells(4, C)) Then
      LR = Columns(C).Find("*", , xlValues, , xlRows, xlPrevious).Row
      With Range(Cells(4, C), Cells(LR, C))
        .Value = Evaluate("IF(ROW(" & .Address & ")>MAX(3," & LR & "-" & Yeses & "),""Yes"","""")")
      End With
    End If
  Next
End Sub
 

Forum statistics

Threads
1,078,340
Messages
5,339,646
Members
399,318
Latest member
kryten68

Some videos you may like

This Week's Hot Topics

Top