VBA Formula

Vishaal

Well-known Member
Joined
Mar 16, 2019
Messages
530
Office Version
  1. 2010
  2. 2007
Platform
  1. Windows
  2. Web
Hi All

Thanks in advance,

I have the following sheet

Excel 2010 32 bit
A
B
C
D
E
1
S.No.​
25689​
365478​
2569​
25698​
2
2564​
2145​
1425​
1478​
3
4
1​
25​
5​
8​
20​
5
2​
30​
10​
10​
23​
6
3​
35​
20​
12​
26​
7
4​
40​
30​
14​
29​
8
5​
45​
40​
16​
31​
9
6​
50​
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

and need the following result in same sheet

Excel 2010 32 bit
A
B
C
D
E
1
S.No.​
25689​
365478​
2569​
25698​
2
2564​
2145​
1425​
1478​
3
4
1​
5
2​
6
3​
Yes​
7
4​
Yes​
Yes​
8
5​
Yes​
Yes​
9
6​
Yes​
Yes​
10
7​
Yes​
Yes​
11
8​
Yes​
Yes​
12
9​
Yes​
Yes​
13
10​
Yes​
Yes​
14
11​
Yes​
15
12​
Sheet: Sheet1

We have added the "Four Yes" from bottom in every column and removed the all numbers

Pls provide the solution
 
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
 
Upvote 0

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
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:
Upvote 0
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
 
Upvote 0
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
 
Upvote 0
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
 
Upvote 0
.. 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:
Upvote 0
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
 
Upvote 0

Forum statistics

Threads
1,213,536
Messages
6,114,208
Members
448,554
Latest member
Gleisner2

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