Maximum Number of times

J

Jay

Hi,
I have an excel sheet

how to find out the maximum number of times a particular text has been
appeared continuously in a column ?

say i have column like this

A
A
A
B
B
B
A
B
B

I would like to find out whats the maximum number of times the B has
appeared continously in a column with out any other value in between ,
in this case 3 times B appeared continously.

how do i do it the same in excel

any help is appreciated

Regards
Jay
 
N

Norman Jones

Hi Jay,

Try:

'=============>>
Public Function ConsecutiveCount(sStr As String, rng As Range) As Long
Dim rCell As Range
Dim iCtr As Long, jCtr As Long

For Each rCell In rng.Cells
With rCell
If .Value = sStr Then
iCtr = iCtr + 1
jCtr = Application.Max(jCtr, iCtr)
Else
iCtr = 0
End If
End With
Next rCell
ConsecutiveCount = jCtr
End Function
'<<=============
 
N

Norman Jones

Hi Jay,

To optionally allow for case sensitivity, try the following version:

'=============>>
Public Function ConsecutiveCount(sStr As String, rng As Range, _
Optional blCaseSensitive As Boolean = False) As Long
Dim rCell As Range
Dim iCtr As Long, jCtr As Long

For Each rCell In rng.Cells
With rCell
If blCaseSensitive Then
If .Value = sStr Then
iCtr = iCtr + 1
jCtr = Application.Max(jCtr, iCtr)
Else
iCtr = 0
End If
Else
If UCase(.Value) = UCase(sStr) Then
iCtr = iCtr + 1
jCtr = Application.Max(jCtr, iCtr)
Else
iCtr = 0
End If
End If
End With
Next rCell
ConsecutiveCount = jCtr
End Function
'<<=============
 
L

Leo Heuser

Jay said:
Hi,
I have an excel sheet

how to find out the maximum number of times a particular text has been
appeared continuously in a column ?

say i have column like this

A
A
A
B
B
B
A
B
B

I would like to find out whats the maximum number of times the B has
appeared continously in a column with out any other value in between ,
in this case 3 times B appeared continously.

how do i do it the same in excel

any help is appreciated

Regards
Jay

Hi Jay

Here's a formula solution, assuming the text to look for is in F1, and the
cells to investigate are A1:A25. If present, the text appears as the only
entry in a cell.

=MAX(FREQUENCY(IF(A1:A25=F1,COUNTIF(OFFSET(A1,,,ROW(INDIRECT(
"1:"&ROWS(A1:A25)))),"<>"&F1)),ROW(INDIRECT("1:"&ROWS(A1:A25)))-1))

The formula must be entered as one line and finished with
<Shift><Ctrl><Enter>, also if edited later.
If done correctly, Excel will display the formula in the formula bar
enclosed in braces { }. Don't enter these braces yourself, they're Excel's
way of showing, that the formula is an array formula.
 
J

Jay

Leo thanx for your reply

but i m not able to execute the problem am getting error

table is

A
A
A
B
A
B
B
B
B
A

need to find max how many times the B has appeared continuously with
out any break

abvoe case B appeared 4 times in a continously .. this has to be
displyed on other cell.
where should i put that formula in ?

regards
Jay
Leo Heuser wrote
:
 
L

Leo Heuser

Hi Jay

enter "b" (or "B") without quotes in F1, and enter
the formula in any cell you want. Remember to finish
the formula with <Shift><Ctrl><Enter> (all 3 keys
pressed at the same time i.e. press <Shift> and <Ctrl>
and while holding them press <Enter>). Release all
3 keys.
If you copy the formula from the post, select
the cell you want the formula to reside in, click the formula
bar and press <Ctrl>v to paste it to the bar.
Go to the end of the first line and press <Delete> to connect
the 2 lines and finish with <Shift><Ctrl><Enter>

Leo Heuser

Followup to newsgroup only please.
 
J

Jay

Leo

still its giving error...

I entered the way you mentioned here changing the F1 and Cell Range
from A1:A25 to L9:L64999

do i need to change anything in the quote

looking forward for ur reply
regards
Jay
 
J

Jay

here how i entered the forumula

MAX(FREQUENCY(IF(L9:L64999=B,COUNTIF(OFFSET(L9,,,ROW(INDIRECT(1:"&ROWS(L9:L64999)))),"<>"&B)),ROW(INDIRECT("1:"&ROWS(L9:L64999)))-1))

regrds

Jay
 
L

Leo Heuser

Jay said:
Leo

still its giving error...

I entered the way you mentioned here changing the F1 and Cell Range
from A1:A25 to L9:L64999

do i need to change anything in the quote

looking forward for ur reply
regards
Jay


Hi Jay

You are welcome to attach a copy of your workbook to
a personal mail, and I'll take a look at it. Which version
of Excel are you using?

leo.heuser at adslhome.dk

Regards
Leo Heuser
 
L

Leo Heuser

If you enter B *directly* into the formula, you have to tell
Excel, that it's a string ("B"), so the formula becomes:

=MAX(FREQUENCY(IF(L9:L64999="B",COUNTIF(OFFSET(L9,,,ROW(INDIRECT(1:"&ROWS(L9:L64999)))),"<>"&"B")),ROW(INDIRECT("1:"&ROWS(L9:L64999)))-1))

If you get errors of some kind, please disclose
what the error says.

Leo Heuser


Followup to newsgroup only please.
 
J

Jay

error is :

Formula You typed Contains an error

For information about fixing common formula problems Click Help
To Get Assistance in Entering a function click OK then click Function
on the Insert Menu
If you are not trying to enter a formula , avoid using an equal sign or
minus sign or preded it with a singl quotation mark ( ' )

regards
Jay
 
J

Jay

leo,
i have one more request dont mind !

how do i put last cells value in another cell ?

as i keep on adding the rows so last cells value changes i want the
same value to be displayed on another cell how do i do this ?

regards
 
J

Jay

Leo,

i have stock market investment spread sheet where in

say : Initial Investment : $1000.00

After Day 1 Trading : $1020.00
After Day 2 Trading : $1068.00
After Day 3 Trading : $1120.00
After Day 4 Trading : $1080.00
After Day 5 Trading : $980.00
After Day 6 Trading : $1020.00
After Day 7 Trading : $1060.00

I would like to calculate and display this in another cell how much max
% we have lost in an sequence against our maximum profit max :
$1120.00

Forumula i have is $1120-$980/$1120 * 100 = 12.5% i want this to be
displayed in another cell. only maximum % lost in any trading sequence
just like previous problem

thanx in advance
regards
 
L

Leo Heuser

Jay said:
leo,
i have one more request dont mind !

how do i put last cells value in another cell ?

as i keep on adding the rows so last cells value changes i want the
same value to be displayed on another cell how do i do this ?

regards
Jay

Jay

Here's one way:

=INDEX(L9:L64999,MAX(IF(ISBLANK(L9:L64999),0,ROW(L9:L64999)-ROW(L9)+1)))

Also an array formula to be entered with <Shift><Ctrl><Enter>

Regards
Leo Heuser
 
L

Leo Heuser

Jay said:
Leo,

i have stock market investment spread sheet where in

say : Initial Investment : $1000.00

After Day 1 Trading : $1020.00
After Day 2 Trading : $1068.00
After Day 3 Trading : $1120.00
After Day 4 Trading : $1080.00
After Day 5 Trading : $980.00
After Day 6 Trading : $1020.00
After Day 7 Trading : $1060.00

I would like to calculate and display this in another cell how much max
% we have lost in an sequence against our maximum profit max :
$1120.00

Forumula i have is $1120-$980/$1120 * 100 = 12.5% i want this to be
displayed in another cell. only maximum % lost in any trading sequence
just like previous problem

thanx in advance
regards
Jay

Jay

One way assuming maximum profit in K1 and tradings in J3:J65000

=(K1-MIN(J3:J65000))/K1

Format the cell as %

Regards
Leo Heuser
 
J

Jay

leo
thanks a lot for the solutions to myproblems.
last cell value got soloved

but Max % lost in trading is not solved.

as u know trading flactuates every day so here in this case i need to
find out max % lost in any consecutive days say

we had $1200 Max and minimum $980 as days progress we may surpass
$1200 ...it shouldnt take next maximum value my intention is to find
out one maximum lost % continuous losing from

example :

here is my initial bank $1000

1. $1100
2. $1150
3. $1200 Max Bank
4. $1090
5. $980 Min Bank at this point we have lost max % to the maximum
bank.
6. $1100
7. $1250

we may lose max % to maximum in the future ...what max % lost has to be
the value displayed in another cell.

hope u can help me

sorry to disturd u

thanx a lot for what ever u did for me till now

regards
Jay
 
L

Leo Heuser

Jay said:
leo
thanks a lot for the solutions to myproblems.
last cell value got soloved

but Max % lost in trading is not solved.

as u know trading flactuates every day so here in this case i need to
find out max % lost in any consecutive days say

we had $1200 Max and minimum $980 as days progress we may surpass
$1200 ...it shouldnt take next maximum value my intention is to find
out one maximum lost % continuous losing from

example :

here is my initial bank $1000

1. $1100
2. $1150
3. $1200 Max Bank
4. $1090
5. $980 Min Bank at this point we have lost max % to the maximum
bank.
6. $1100
7. $1250

we may lose max % to maximum in the future ...what max % lost has to be
the value displayed in another cell.

hope u can help me

sorry to disturd u

thanx a lot for what ever u did for me till now

regards
Jay

Jay

I'm not sure, that I fully understand, what you're after, but
here's my guess. With data in A2:A200 and initial amount in A2.

=IF(INDEX(A2:A200,MAX(IF(ISBLANK(A2:A200),0,ROW(A2:A200)-
ROW(A2)+1)))=MAX(A2:A200),0,(MAX(A2:A200)-INDEX(A2:A200,
MAX(IF((A2:A200<MAX(A2:A200))*(A2:A200<>""),ROW(A2:A200)-
ROW(A2)+1))))/MAX(A2:A200))

Again in one line, entered with <Shift><Ctrl><Enter> and the cell
formatted as %.

If I'm wrong, please make a larger example, which shows all
the principles with calculated results.

Regards
Leo Heuser
 
J

Jay

Wht i m looking for is

I would like to find out in a tenure of trading Maximum % Lost say

$1000
$1200
$1350
$1450 - Max 1
$1200
$1450
$1200
$1100
$1000
$900 - Min 1
$1000
$1200
$1350
$1450
$1500 - Max 2
$1200
$1100
$1000
$800 - Min 2

1st Max and Min - $1450-900/$1450 = 37.94% Lost this will be
displayed 1st
2nd Max and Min - $1500-800/$1500= 46% lost this will displayed noth
the first one as we have lost more than first Max Min .

its about checking each trading day Max bank minimum bank - minimum
bank should be taken after the maximum bank attained not the previous
minimum

Max-Min/Max * 100 = Max % Lost after attaining Maximum bank.

Minimum should be taken after the maximum bank attained as explained
above

what ever lost has to be Maximum % lost in all the trading ....

i think its clear now

regards
jay



% lost to maximum bank attained here we attained $1450
 
L

Leo Heuser

Jay said:
Wht i m looking for is

I would like to find out in a tenure of trading Maximum % Lost say

$1000
$1200
$1350
$1450 - Max 1
$1200
$1450
$1200
$1100
$1000
$900 - Min 1
$1000
$1200
$1350
$1450
$1500 - Max 2
$1200
$1100
$1000
$800 - Min 2

1st Max and Min - $1450-900/$1450 = 37.94% Lost this will be
displayed 1st
2nd Max and Min - $1500-800/$1500= 46% lost this will displayed noth
the first one as we have lost more than first Max Min .

its about checking each trading day Max bank minimum bank - minimum
bank should be taken after the maximum bank attained not the previous
minimum

Max-Min/Max * 100 = Max % Lost after attaining Maximum bank.

Minimum should be taken after the maximum bank attained as explained
above

what ever lost has to be Maximum % lost in all the trading ....

i think its clear now

regards
jay

Jay

I don't think it's possible to make a formula solution without helper cells
and/or named formulae, so I have dropped it and instead made a user
defined function (UDF). It has the added advantage, that you are able
to find the max % lost for an arbitrary (sequentially) period of time.

Here's how to implement it:

1. Copy the code below (see note!)
2. Enter the VBA-editor with <Alt><F11>
3. Doubleclick the project in the project window
at the left side of the screen. (if it isn't visible, use <Ctrl>r)
4. Choose the menu Insert > Module
5. Doubleclick the new module in the project window
6. Paste the code to the right hand window.
7. Return to the sheet with <Alt><F11>
8. Save the workbook.

From the workbook:
Assuming data in A2:A4000 (more than 10 years, so you probably
haven't filled all cells in the range :)

In any cell outside column A (e.g. G1) enter:

=maxbank(a2:a4000)

G1 displays the result for all entered values (e.g. a2:a167)

=maxbank(a23:a129)

G1 displays the result for the period spanning cells a23 through a129.

As you enter new data in a168 and down, G1 will display the
largest % loss for the various groups (a group being data from
one max value to the next, or from the last max value to the
last entry)


Regards
Leo Heuser


Note:
Because of the spaces in front of the lines, the code may not work
when copied and pasted. If you experience that open the toolbar
"Edit", select all code in the module window and press the button
"Outdent" repeatedly until *all* lines match the left border of the
window.
The code may loose in readability, but it works :)


Function MaxBank(BankRange As Range) As Double
'Leo Heuser, Sep. 16, 2006
Dim BankRangeValue As Variant
Dim Counter As Long
Dim Counter1 As Long
Dim CountElement As Long
Dim GetMaxiValue As Double
Dim GetMiniValue As Double
Dim MaxiBankRow() As Long
Dim MiniBankValue() As Double
Dim Result() As Double

Set BankRange = BankRange.Columns(1)

If IsEmpty(BankRange.Cells(1, 1). _
Offset(BankRange.Rows.Count)) Then
Set BankRange = Range(BankRange.Cells(1, 1), _
ActiveSheet.Cells(ActiveSheet.Rows.Count, _
BankRange.Column).End(xlUp))
End If

If BankRange.Rows.Count = 1 Then
MaxBank = 0
GoTo Finito
End If

BankRangeValue = BankRange.Value

GetMaxiValue = BankRangeValue(1, 1)

ReDim MaxiBankRow(1 To UBound(BankRangeValue, 1))

CountElement = 1

MaxiBankRow(CountElement) = CountElement

For Counter = 2 To UBound(BankRangeValue, 1) - 1
If BankRangeValue(Counter, 1) > GetMaxiValue Then
If BankRangeValue(Counter, 1) > _
BankRangeValue(Counter + 1, 1) Then
CountElement = CountElement + 1
GetMaxiValue = BankRangeValue(Counter, 1)
MaxiBankRow(CountElement) = Counter
End If
End If
Next Counter

MaxiBankRow(CountElement + 1) = Counter

ReDim Preserve MaxiBankRow(1 To CountElement + 1)
ReDim MiniBankValue(1 To UBound(MaxiBankRow))
ReDim Result(1 To UBound(MaxiBankRow))

For Counter = 1 To UBound(MiniBankValue) - 1
GetMiniValue = BankRangeValue(MaxiBankRow(Counter), 1)

For Counter1 = MaxiBankRow(Counter) To MaxiBankRow(Counter + 1)
If BankRangeValue(Counter1, 1) < GetMiniValue Then
GetMiniValue = BankRangeValue(Counter1, 1)
End If
Next Counter1

MiniBankValue(Counter) = GetMiniValue
Next Counter

MiniBankValue(Counter) = _
BankRangeValue(UBound(BankRangeValue, 1), 1)

For Counter = 1 To UBound(Result)
Result(Counter) = (BankRangeValue(MaxiBankRow(Counter), 1) - _
MiniBankValue(Counter)) / _
BankRangeValue(MaxiBankRow(Counter), 1)
Next Counter

MaxBank = Application.Max(Result)

Finito:

End Function
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top