Range with "Large"

  • Thread starter Thread starter art
  • Start date Start date
A

art

Hello all:

Can someone please help me with this. I have the following vba code:

cdamount = WorksheetFunction.Large(Cells(target1,"B"):(Rows(target2,"B")),
amount1)
target1 = Target.Row
target2 = Target.Row + 10

The range for "large" does not work correctly, what should I change to get
this right?

I want to use a worksheet code "Worksheet_SelectionChange" that when I
change my selection it should show me ina differnt cell the "large" of that
specific row which is active.

Please help.

Thanks.
 
cdamount _
= Application.Large(ActiveSheet.Cells(target1, "B").Resize(11, 1), amount1)

or

with activesheet 'whatever???
cdamount _
= Application.Large(.range(.Cells(target1, "B"), .cells(target2,"B")), _
amount1)
end with
 
Cells is returning values. Large wants a range (more precisely, an array).

I am slightly confused about your ambition. However, the following MAY help:


Private Sub Worksheet_SelectionChange(ByVal Target As Range)

myrange = Target.Address & ":" & Target.Offset(10).Address

cdamount = WorksheetFunction.Large(Range(myrange), 1)

MsgBox cdamount
End Sub

I interpretted from your question's code that you wanted Large to apply to
column B.
If you want large applied to a row, instead of a column, just change
Target.Offset(10).Address
to
Target.Offset(,10).Address

If I totally misunderstand what you are trying to do, please add further
information

HTH
 
Here is what I want to do.

I have lets say in A1 through A10 codes (112, 113, 114...) In B1 through K10
is prices , so for EG, code 112 could have in B1 1.99 and in C1 2.99 and D1
3.99 and so on. And they same for each code.

Here is what I want to do. When I select any "code", in cell M1 should state
the "large" number (or whatever I will select, second to largest, third to
largest and so on). So if I select A2, it should state in M1 the highest
price for that code from that row.

I hope I came across clear.

Thanks.
 
Sorry about the delay - my daughter took over ownership of the PC!

Using Dave's (superior) Resize() to change the range


Private Sub Worksheet_SelectionChange(ByVal Target As Range)

Const amt1 = 1 ' 1 = largest value, 2 = 2nd largest etc
Dim myrange
If Target.Count = 1 Then 'one cell selected
Set isect = Application.Intersect(Range("A:A"), Target)
If Not isect Is Nothing Then ' you clicked a cell in column A

myrange = Target.Offset(, 1).Resize(1, 11).Address

If Application.WorksheetFunction.CountA(Range(myrange)) > 0 Then
Debug.Print
Application.WorksheetFunction.Large(Range(myrange), amt1)
Target.Offset(, 12) =
Application.WorksheetFunction.Large(Range(myrange), amt1)
End If
End If
End If
End Sub
 
Thanks, but something is wrong. Which part of this is on one line and which
on the next?

If Application.WorksheetFunction.CountA(Range(myrange)) > 0 Then
Debug.Print
Application.WorksheetFunction.Large(Range(myrange), amt1)
Target.Offset(, 12) =
Application.WorksheetFunction.Large(Range(myrange), amt1)
End If
 
I left a (harmless) debug line in. Now removed from the following:


Private Sub Worksheet_SelectionChange(ByVal Target As Range)

Const amt1 = 1 ' 1 = largest value, 2 = 2nd largest etc
Dim myrange
If Target.Count = 1 Then
Set isect = Application.Intersect(Range("A:A"), Target)
If Not isect Is Nothing Then

myrange = Target.Offset(, 1).Resize(1, 11).Address

If Application.WorksheetFunction.CountA(Range(myrange)) > 0 Then
Target.Offset(, 12) = _
Application.WorksheetFunction.Large(Range(myrange),
amt1)
End If
End If
End If
End Sub

Now to fix the line wrapping problem:

Where the line ends with '= _' in the above, delete the _ and keep pressing
[Delete] until there is only a single space between '=' and 'Application'.

If that doesn't work, My method is to navigate the cursor to the end of the
first line of code that shows red in my module and press [Delete] until the
code wraps the following line into position. Once it does that, I move to
the end of the joined line and press [Enter]. Keep repeating this process
until all code is black (or green if a comment).
 
Thanks it works.

I want to add also a random between code that will give me a random amount
between the lowest and highest point of that row. The formula I would use in
excdel would be: LARGE(B6:K6,RANDBETWEEN(1,COUNTIF(B6:K6,">1"))).

Can you help me finish it off? Thanks so much.

Art.

AltaEgo said:
I left a (harmless) debug line in. Now removed from the following:


Private Sub Worksheet_SelectionChange(ByVal Target As Range)

Const amt1 = 1 ' 1 = largest value, 2 = 2nd largest etc
Dim myrange
If Target.Count = 1 Then
Set isect = Application.Intersect(Range("A:A"), Target)
If Not isect Is Nothing Then

myrange = Target.Offset(, 1).Resize(1, 11).Address

If Application.WorksheetFunction.CountA(Range(myrange)) > 0 Then
Target.Offset(, 12) = _
Application.WorksheetFunction.Large(Range(myrange),
amt1)
End If
End If
End If
End Sub

Now to fix the line wrapping problem:

Where the line ends with '= _' in the above, delete the _ and keep pressing
[Delete] until there is only a single space between '=' and 'Application'.

If that doesn't work, My method is to navigate the cursor to the end of the
first line of code that shows red in my module and press [Delete] until the
code wraps the following line into position. Once it does that, I move to
the end of the joined line and press [Enter]. Keep repeating this process
until all code is black (or green if a comment).



--
Steve

art said:
Thanks, but something is wrong. Which part of this is on one line and
which
on the next?

If Application.WorksheetFunction.CountA(Range(myrange)) > 0 Then
Debug.Print
Application.WorksheetFunction.Large(Range(myrange), amt1)
Target.Offset(, 12) =
Application.WorksheetFunction.Large(Range(myrange), amt1)
End If
 
Application.WorksheetFunction does not have a reference to RandBetween (i.e.
you cannot call it this way). The following piece of code should work.
Place it before the first of the End If lines.


Randomize
mylarge =
Application.WorksheetFunction.Large(Range(myrange), 1)
mysmall =
Application.WorksheetFunction.Small(Range(myrange), 1)
myRandBetween = Int(Rnd() * (mylarge - mysmall + 1) +
mysmall)


To set the value in N1 add the following

Target.Offset(, 13) = myRandBetween


--
Steve

art said:
Thanks it works.

I want to add also a random between code that will give me a random amount
between the lowest and highest point of that row. The formula I would use
in
excdel would be: LARGE(B6:K6,RANDBETWEEN(1,COUNTIF(B6:K6,">1"))).

Can you help me finish it off? Thanks so much.

Art.

AltaEgo said:
I left a (harmless) debug line in. Now removed from the following:


Private Sub Worksheet_SelectionChange(ByVal Target As Range)

Const amt1 = 1 ' 1 = largest value, 2 = 2nd largest etc
Dim myrange
If Target.Count = 1 Then
Set isect = Application.Intersect(Range("A:A"), Target)
If Not isect Is Nothing Then

myrange = Target.Offset(, 1).Resize(1, 11).Address

If Application.WorksheetFunction.CountA(Range(myrange)) > 0
Then
Target.Offset(, 12) = _
Application.WorksheetFunction.Large(Range(myrange),
amt1)
End If
End If
End If
End Sub

Now to fix the line wrapping problem:

Where the line ends with '= _' in the above, delete the _ and keep
pressing
[Delete] until there is only a single space between '=' and
'Application'.

If that doesn't work, My method is to navigate the cursor to the end of
the
first line of code that shows red in my module and press [Delete] until
the
code wraps the following line into position. Once it does that, I move to
the end of the joined line and press [Enter]. Keep repeating this process
until all code is black (or green if a comment).



--
Steve

art said:
Thanks, but something is wrong. Which part of this is on one line and
which
on the next?

If Application.WorksheetFunction.CountA(Range(myrange)) > 0 Then
Debug.Print
Application.WorksheetFunction.Large(Range(myrange), amt1)
Target.Offset(, 12) =
Application.WorksheetFunction.Large(Range(myrange), amt1)
End If


:

Sorry about the delay - my daughter took over ownership of the PC!

Using Dave's (superior) Resize() to change the range


Private Sub Worksheet_SelectionChange(ByVal Target As Range)

Const amt1 = 1 ' 1 = largest value, 2 = 2nd largest etc
Dim myrange
If Target.Count = 1 Then 'one cell selected
Set isect = Application.Intersect(Range("A:A"), Target)
If Not isect Is Nothing Then ' you clicked a cell in column A

myrange = Target.Offset(, 1).Resize(1, 11).Address

If Application.WorksheetFunction.CountA(Range(myrange)) >
0
Then
Debug.Print
Application.WorksheetFunction.Large(Range(myrange), amt1)
Target.Offset(, 12) =
Application.WorksheetFunction.Large(Range(myrange), amt1)
End If
End If
End If
End Sub



--
Steve

Here is what I want to do.

I have lets say in A1 through A10 codes (112, 113, 114...) In B1
through
K10
is prices , so for EG, code 112 could have in B1 1.99 and in C1 2.99
and
D1
3.99 and so on. And they same for each code.

Here is what I want to do. When I select any "code", in cell M1
should
state
the "large" number (or whatever I will select, second to largest,
third
to
largest and so on). So if I select A2, it should state in M1 the
highest
price for that code from that row.

I hope I came across clear.

Thanks.

:

Cells is returning values. Large wants a range (more precisely, an
array).

I am slightly confused about your ambition. However, the following
MAY
help:


Private Sub Worksheet_SelectionChange(ByVal Target As Range)

myrange = Target.Address & ":" & Target.Offset(10).Address

cdamount = WorksheetFunction.Large(Range(myrange), 1)

MsgBox cdamount
End Sub

I interpretted from your question's code that you wanted Large to
apply
to
column B.
If you want large applied to a row, instead of a column, just
change
Target.Offset(10).Address
to
Target.Offset(,10).Address

If I totally misunderstand what you are trying to do, please add
further
information

HTH

--
Steve

Hello all:

Can someone please help me with this. I have the following vba
code:

cdamount =
WorksheetFunction.Large(Cells(target1,"B"):(Rows(target2,"B")),
amount1)
target1 = Target.Row
target2 = Target.Row + 10

The range for "large" does not work correctly, what should I
change
to
get
this right?

I want to use a worksheet code "Worksheet_SelectionChange" that
when
I
change my selection it should show me ina differnt cell the
"large"
of
that
specific row which is active.

Please help.

Thanks.
 
Just occurred to me you may want to specify a number of decimal places or
leave it open. If so,

Obtain ready rolled code RandomNumbers from
http://www.fontstuff.com/vba/vbatut06.htm

and copy it either below your code or into a general module.


Then insert the following before the first End If of the SelectionChange
code:


mylarge = Application.WorksheetFunction.Large(Range(myrange), 1)
mysmall = Application.WorksheetFunction.Small(Range(myrange), 1)
myRandBetween = RandomNumbers(mysmall,mylarge, x )
'replace 'x' above with your required decimal places
' delete ', x' if you don't care about decimal places.
' but don't miss deleting the comma
Target.Offset(, 13) = myRandBetween
' will set the value in column N of appropriate row




--
Steve

AltaEgo said:
Application.WorksheetFunction does not have a reference to RandBetween
(i.e. you cannot call it this way). The following piece of code should
work.
Place it before the first of the End If lines.


Randomize
mylarge =
Application.WorksheetFunction.Large(Range(myrange), 1)
mysmall =
Application.WorksheetFunction.Small(Range(myrange), 1)
myRandBetween = Int(Rnd() * (mylarge - mysmall + 1) +
mysmall)


To set the value in N1 add the following

Target.Offset(, 13) = myRandBetween


--
Steve

art said:
Thanks it works.

I want to add also a random between code that will give me a random
amount
between the lowest and highest point of that row. The formula I would use
in
excdel would be: LARGE(B6:K6,RANDBETWEEN(1,COUNTIF(B6:K6,">1"))).

Can you help me finish it off? Thanks so much.

Art.

AltaEgo said:
I left a (harmless) debug line in. Now removed from the following:


Private Sub Worksheet_SelectionChange(ByVal Target As Range)

Const amt1 = 1 ' 1 = largest value, 2 = 2nd largest etc
Dim myrange
If Target.Count = 1 Then
Set isect = Application.Intersect(Range("A:A"), Target)
If Not isect Is Nothing Then

myrange = Target.Offset(, 1).Resize(1, 11).Address

If Application.WorksheetFunction.CountA(Range(myrange)) > 0
Then
Target.Offset(, 12) = _
Application.WorksheetFunction.Large(Range(myrange),
amt1)
End If
End If
End If
End Sub

Now to fix the line wrapping problem:

Where the line ends with '= _' in the above, delete the _ and keep
pressing
[Delete] until there is only a single space between '=' and
'Application'.

If that doesn't work, My method is to navigate the cursor to the end of
the
first line of code that shows red in my module and press [Delete] until
the
code wraps the following line into position. Once it does that, I move
to
the end of the joined line and press [Enter]. Keep repeating this
process
until all code is black (or green if a comment).



--
Steve

Thanks, but something is wrong. Which part of this is on one line and
which
on the next?

If Application.WorksheetFunction.CountA(Range(myrange)) > 0 Then
Debug.Print
Application.WorksheetFunction.Large(Range(myrange), amt1)
Target.Offset(, 12) =
Application.WorksheetFunction.Large(Range(myrange), amt1)
End If


:

Sorry about the delay - my daughter took over ownership of the PC!

Using Dave's (superior) Resize() to change the range


Private Sub Worksheet_SelectionChange(ByVal Target As Range)

Const amt1 = 1 ' 1 = largest value, 2 = 2nd largest etc
Dim myrange
If Target.Count = 1 Then 'one cell selected
Set isect = Application.Intersect(Range("A:A"), Target)
If Not isect Is Nothing Then ' you clicked a cell in column A

myrange = Target.Offset(, 1).Resize(1, 11).Address

If Application.WorksheetFunction.CountA(Range(myrange)) >
0
Then
Debug.Print
Application.WorksheetFunction.Large(Range(myrange), amt1)
Target.Offset(, 12) =
Application.WorksheetFunction.Large(Range(myrange), amt1)
End If
End If
End If
End Sub



--
Steve

Here is what I want to do.

I have lets say in A1 through A10 codes (112, 113, 114...) In B1
through
K10
is prices , so for EG, code 112 could have in B1 1.99 and in C1
2.99
and
D1
3.99 and so on. And they same for each code.

Here is what I want to do. When I select any "code", in cell M1
should
state
the "large" number (or whatever I will select, second to largest,
third
to
largest and so on). So if I select A2, it should state in M1 the
highest
price for that code from that row.

I hope I came across clear.

Thanks.

:

Cells is returning values. Large wants a range (more precisely, an
array).

I am slightly confused about your ambition. However, the following
MAY
help:


Private Sub Worksheet_SelectionChange(ByVal Target As Range)

myrange = Target.Address & ":" & Target.Offset(10).Address

cdamount = WorksheetFunction.Large(Range(myrange), 1)

MsgBox cdamount
End Sub

I interpretted from your question's code that you wanted Large to
apply
to
column B.
If you want large applied to a row, instead of a column, just
change
Target.Offset(10).Address
to
Target.Offset(,10).Address

If I totally misunderstand what you are trying to do, please add
further
information

HTH

--
Steve

Hello all:

Can someone please help me with this. I have the following vba
code:

cdamount =
WorksheetFunction.Large(Cells(target1,"B"):(Rows(target2,"B")),
amount1)
target1 = Target.Row
target2 = Target.Row + 10

The range for "large" does not work correctly, what should I
change
to
get
this right?

I want to use a worksheet code "Worksheet_SelectionChange" that
when
I
change my selection it should show me ina differnt cell the
"large"
of
that
specific row which is active.

Please help.

Thanks.
 
I want the random to give me a random number only from the prices that are on
that row, so if I have on that row, 1.19 1,49, 1.89, so the random should
only choose between these three numbers.

Thanks for your help

AltaEgo said:
Application.WorksheetFunction does not have a reference to RandBetween (i.e.
you cannot call it this way). The following piece of code should work.
Place it before the first of the End If lines.


Randomize
mylarge =
Application.WorksheetFunction.Large(Range(myrange), 1)
mysmall =
Application.WorksheetFunction.Small(Range(myrange), 1)
myRandBetween = Int(Rnd() * (mylarge - mysmall + 1) +
mysmall)


To set the value in N1 add the following

Target.Offset(, 13) = myRandBetween


--
Steve

art said:
Thanks it works.

I want to add also a random between code that will give me a random amount
between the lowest and highest point of that row. The formula I would use
in
excdel would be: LARGE(B6:K6,RANDBETWEEN(1,COUNTIF(B6:K6,">1"))).

Can you help me finish it off? Thanks so much.

Art.

AltaEgo said:
I left a (harmless) debug line in. Now removed from the following:


Private Sub Worksheet_SelectionChange(ByVal Target As Range)

Const amt1 = 1 ' 1 = largest value, 2 = 2nd largest etc
Dim myrange
If Target.Count = 1 Then
Set isect = Application.Intersect(Range("A:A"), Target)
If Not isect Is Nothing Then

myrange = Target.Offset(, 1).Resize(1, 11).Address

If Application.WorksheetFunction.CountA(Range(myrange)) > 0
Then
Target.Offset(, 12) = _
Application.WorksheetFunction.Large(Range(myrange),
amt1)
End If
End If
End If
End Sub

Now to fix the line wrapping problem:

Where the line ends with '= _' in the above, delete the _ and keep
pressing
[Delete] until there is only a single space between '=' and
'Application'.

If that doesn't work, My method is to navigate the cursor to the end of
the
first line of code that shows red in my module and press [Delete] until
the
code wraps the following line into position. Once it does that, I move to
the end of the joined line and press [Enter]. Keep repeating this process
until all code is black (or green if a comment).



--
Steve

Thanks, but something is wrong. Which part of this is on one line and
which
on the next?

If Application.WorksheetFunction.CountA(Range(myrange)) > 0 Then
Debug.Print
Application.WorksheetFunction.Large(Range(myrange), amt1)
Target.Offset(, 12) =
Application.WorksheetFunction.Large(Range(myrange), amt1)
End If


:

Sorry about the delay - my daughter took over ownership of the PC!

Using Dave's (superior) Resize() to change the range


Private Sub Worksheet_SelectionChange(ByVal Target As Range)

Const amt1 = 1 ' 1 = largest value, 2 = 2nd largest etc
Dim myrange
If Target.Count = 1 Then 'one cell selected
Set isect = Application.Intersect(Range("A:A"), Target)
If Not isect Is Nothing Then ' you clicked a cell in column A

myrange = Target.Offset(, 1).Resize(1, 11).Address

If Application.WorksheetFunction.CountA(Range(myrange)) >
0
Then
Debug.Print
Application.WorksheetFunction.Large(Range(myrange), amt1)
Target.Offset(, 12) =
Application.WorksheetFunction.Large(Range(myrange), amt1)
End If
End If
End If
End Sub



--
Steve

Here is what I want to do.

I have lets say in A1 through A10 codes (112, 113, 114...) In B1
through
K10
is prices , so for EG, code 112 could have in B1 1.99 and in C1 2.99
and
D1
3.99 and so on. And they same for each code.

Here is what I want to do. When I select any "code", in cell M1
should
state
the "large" number (or whatever I will select, second to largest,
third
to
largest and so on). So if I select A2, it should state in M1 the
highest
price for that code from that row.

I hope I came across clear.

Thanks.

:

Cells is returning values. Large wants a range (more precisely, an
array).

I am slightly confused about your ambition. However, the following
MAY
help:


Private Sub Worksheet_SelectionChange(ByVal Target As Range)

myrange = Target.Address & ":" & Target.Offset(10).Address

cdamount = WorksheetFunction.Large(Range(myrange), 1)

MsgBox cdamount
End Sub

I interpretted from your question's code that you wanted Large to
apply
to
column B.
If you want large applied to a row, instead of a column, just
change
Target.Offset(10).Address
to
Target.Offset(,10).Address

If I totally misunderstand what you are trying to do, please add
further
information

HTH

--
Steve

Hello all:

Can someone please help me with this. I have the following vba
code:

cdamount =
WorksheetFunction.Large(Cells(target1,"B"):(Rows(target2,"B")),
amount1)
target1 = Target.Row
target2 = Target.Row + 10

The range for "large" does not work correctly, what should I
change
to
get
this right?

I want to use a worksheet code "Worksheet_SelectionChange" that
when
I
change my selection it should show me ina differnt cell the
"large"
of
that
specific row which is active.

Please help.

Thanks.
 
The code from my other response should fix that.

Don't forget, if you don't need to use Large() and Small(), you can use

mysmall = Application.WorksheetFunction.Min(Range(myrange))
mylarge = Application.WorksheetFunction.Max(Range(myrange))

Also...

On second thoughts, try this. It uses enough of a mix between Large() and
Min() that you should be able to understand how to vary construction.
Note: the use of RandomNumbers() without the optional number of decimals
specified.



Private Sub Worksheet_SelectionChange(ByVal Target As Range)

Const amt1 = 1 ' 1 = largest value, 2 = 2nd largest etc
Dim myRange
Dim mySmall As Long
Dim myLarge As Long
Dim myRandBetween As Long
If Target.Count = 1 Then
Set isect = Application.Intersect(Range("A:A"), Target)
If Not isect Is Nothing Then

myRange = Target.Offset(, 1).Resize(1, 11).Address

If Application.WorksheetFunction.CountA(Range(myRange)) > 0 Then
myLarge =
Application.WorksheetFunction.Large(Range(myRange), amt1)
Target.Offset(, 12) = myLarge
Randomize
mySmall =
Application.WorksheetFunction.Min(Range(myRange))
myRandBetween = RandomNumbers(mySmall, myLarge)
Target.Offset(, 13) = myRandBetween
End If
End If
End If
End Sub
Public Function RandomNumbers(Lowest As Long, Highest As Long, _
Optional Decimals As Integer)
Application.Volatile 'Remove this line to "freeze" the numbers
If IsMissing(Decimals) Or Decimals = 0 Then
Randomize
RandomNumbers = Int((Highest + 1 - Lowest) * Rnd + Lowest)
Else
Randomize
RandomNumbers = Round((Highest - Lowest) * Rnd + Lowest, Decimals)
End If
End Function

Its time for me to cook, relax and sleep. If you need further help, just
ask.


--
Steve

art said:
I want the random to give me a random number only from the prices that are
on
that row, so if I have on that row, 1.19 1,49, 1.89, so the random should
only choose between these three numbers.

Thanks for your help

AltaEgo said:
Application.WorksheetFunction does not have a reference to RandBetween
(i.e.
you cannot call it this way). The following piece of code should work.
Place it before the first of the End If lines.


Randomize
mylarge =
Application.WorksheetFunction.Large(Range(myrange), 1)
mysmall =
Application.WorksheetFunction.Small(Range(myrange), 1)
myRandBetween = Int(Rnd() * (mylarge - mysmall + 1) +
mysmall)


To set the value in N1 add the following

Target.Offset(, 13) = myRandBetween


--
Steve

art said:
Thanks it works.

I want to add also a random between code that will give me a random
amount
between the lowest and highest point of that row. The formula I would
use
in
excdel would be: LARGE(B6:K6,RANDBETWEEN(1,COUNTIF(B6:K6,">1"))).

Can you help me finish it off? Thanks so much.

Art.

:

I left a (harmless) debug line in. Now removed from the following:


Private Sub Worksheet_SelectionChange(ByVal Target As Range)

Const amt1 = 1 ' 1 = largest value, 2 = 2nd largest etc
Dim myrange
If Target.Count = 1 Then
Set isect = Application.Intersect(Range("A:A"), Target)
If Not isect Is Nothing Then

myrange = Target.Offset(, 1).Resize(1, 11).Address

If Application.WorksheetFunction.CountA(Range(myrange)) >
0
Then
Target.Offset(, 12) = _

Application.WorksheetFunction.Large(Range(myrange),
amt1)
End If
End If
End If
End Sub

Now to fix the line wrapping problem:

Where the line ends with '= _' in the above, delete the _ and keep
pressing
[Delete] until there is only a single space between '=' and
'Application'.

If that doesn't work, My method is to navigate the cursor to the end
of
the
first line of code that shows red in my module and press [Delete]
until
the
code wraps the following line into position. Once it does that, I move
to
the end of the joined line and press [Enter]. Keep repeating this
process
until all code is black (or green if a comment).



--
Steve

Thanks, but something is wrong. Which part of this is on one line
and
which
on the next?

If Application.WorksheetFunction.CountA(Range(myrange)) > 0 Then
Debug.Print
Application.WorksheetFunction.Large(Range(myrange), amt1)
Target.Offset(, 12) =
Application.WorksheetFunction.Large(Range(myrange), amt1)
End If


:

Sorry about the delay - my daughter took over ownership of the PC!

Using Dave's (superior) Resize() to change the range


Private Sub Worksheet_SelectionChange(ByVal Target As Range)

Const amt1 = 1 ' 1 = largest value, 2 = 2nd largest etc
Dim myrange
If Target.Count = 1 Then 'one cell selected
Set isect = Application.Intersect(Range("A:A"), Target)
If Not isect Is Nothing Then ' you clicked a cell in column
A

myrange = Target.Offset(, 1).Resize(1, 11).Address

If Application.WorksheetFunction.CountA(Range(myrange))

0
Then
Debug.Print
Application.WorksheetFunction.Large(Range(myrange), amt1)
Target.Offset(, 12) =
Application.WorksheetFunction.Large(Range(myrange), amt1)
End If
End If
End If
End Sub



--
Steve

Here is what I want to do.

I have lets say in A1 through A10 codes (112, 113, 114...) In B1
through
K10
is prices , so for EG, code 112 could have in B1 1.99 and in C1
2.99
and
D1
3.99 and so on. And they same for each code.

Here is what I want to do. When I select any "code", in cell M1
should
state
the "large" number (or whatever I will select, second to largest,
third
to
largest and so on). So if I select A2, it should state in M1 the
highest
price for that code from that row.

I hope I came across clear.

Thanks.

:

Cells is returning values. Large wants a range (more precisely,
an
array).

I am slightly confused about your ambition. However, the
following
MAY
help:


Private Sub Worksheet_SelectionChange(ByVal Target As Range)

myrange = Target.Address & ":" & Target.Offset(10).Address

cdamount = WorksheetFunction.Large(Range(myrange), 1)

MsgBox cdamount
End Sub

I interpretted from your question's code that you wanted Large
to
apply
to
column B.
If you want large applied to a row, instead of a column, just
change
Target.Offset(10).Address
to
Target.Offset(,10).Address

If I totally misunderstand what you are trying to do, please add
further
information

HTH

--
Steve

Hello all:

Can someone please help me with this. I have the following vba
code:

cdamount =
WorksheetFunction.Large(Cells(target1,"B"):(Rows(target2,"B")),
amount1)
target1 = Target.Row
target2 = Target.Row + 10

The range for "large" does not work correctly, what should I
change
to
get
this right?

I want to use a worksheet code "Worksheet_SelectionChange"
that
when
I
change my selection it should show me ina differnt cell the
"large"
of
that
specific row which is active.

Please help.

Thanks.
 
Just read the question again. I totally missed the point!

Let me think out loud:
1) count valid values
2) store values in array
3) find random between 1 and the count (call it n)
4) pull nth number from array.

OK

How do you store number in the range. Do they always fill from the left-most
cell?

for example

1.19,1.49,1.89,1.12,blank,blank,blank...

or can they be stored:

blank, 1.19,blank,1.49,1.89,1.12,blank,blank...


--
Steve

art said:
I want the random to give me a random number only from the prices that are
on
that row, so if I have on that row, 1.19 1,49, 1.89, so the random should
only choose between these three numbers.

Thanks for your help

AltaEgo said:
Application.WorksheetFunction does not have a reference to RandBetween
(i.e.
you cannot call it this way). The following piece of code should work.
Place it before the first of the End If lines.


Randomize
mylarge =
Application.WorksheetFunction.Large(Range(myrange), 1)
mysmall =
Application.WorksheetFunction.Small(Range(myrange), 1)
myRandBetween = Int(Rnd() * (mylarge - mysmall + 1) +
mysmall)


To set the value in N1 add the following

Target.Offset(, 13) = myRandBetween


--
Steve

art said:
Thanks it works.

I want to add also a random between code that will give me a random
amount
between the lowest and highest point of that row. The formula I would
use
in
excdel would be: LARGE(B6:K6,RANDBETWEEN(1,COUNTIF(B6:K6,">1"))).

Can you help me finish it off? Thanks so much.

Art.

:

I left a (harmless) debug line in. Now removed from the following:


Private Sub Worksheet_SelectionChange(ByVal Target As Range)

Const amt1 = 1 ' 1 = largest value, 2 = 2nd largest etc
Dim myrange
If Target.Count = 1 Then
Set isect = Application.Intersect(Range("A:A"), Target)
If Not isect Is Nothing Then

myrange = Target.Offset(, 1).Resize(1, 11).Address

If Application.WorksheetFunction.CountA(Range(myrange)) >
0
Then
Target.Offset(, 12) = _

Application.WorksheetFunction.Large(Range(myrange),
amt1)
End If
End If
End If
End Sub

Now to fix the line wrapping problem:

Where the line ends with '= _' in the above, delete the _ and keep
pressing
[Delete] until there is only a single space between '=' and
'Application'.

If that doesn't work, My method is to navigate the cursor to the end
of
the
first line of code that shows red in my module and press [Delete]
until
the
code wraps the following line into position. Once it does that, I move
to
the end of the joined line and press [Enter]. Keep repeating this
process
until all code is black (or green if a comment).



--
Steve

Thanks, but something is wrong. Which part of this is on one line
and
which
on the next?

If Application.WorksheetFunction.CountA(Range(myrange)) > 0 Then
Debug.Print
Application.WorksheetFunction.Large(Range(myrange), amt1)
Target.Offset(, 12) =
Application.WorksheetFunction.Large(Range(myrange), amt1)
End If


:

Sorry about the delay - my daughter took over ownership of the PC!

Using Dave's (superior) Resize() to change the range


Private Sub Worksheet_SelectionChange(ByVal Target As Range)

Const amt1 = 1 ' 1 = largest value, 2 = 2nd largest etc
Dim myrange
If Target.Count = 1 Then 'one cell selected
Set isect = Application.Intersect(Range("A:A"), Target)
If Not isect Is Nothing Then ' you clicked a cell in column
A

myrange = Target.Offset(, 1).Resize(1, 11).Address

If Application.WorksheetFunction.CountA(Range(myrange))

0
Then
Debug.Print
Application.WorksheetFunction.Large(Range(myrange), amt1)
Target.Offset(, 12) =
Application.WorksheetFunction.Large(Range(myrange), amt1)
End If
End If
End If
End Sub



--
Steve

Here is what I want to do.

I have lets say in A1 through A10 codes (112, 113, 114...) In B1
through
K10
is prices , so for EG, code 112 could have in B1 1.99 and in C1
2.99
and
D1
3.99 and so on. And they same for each code.

Here is what I want to do. When I select any "code", in cell M1
should
state
the "large" number (or whatever I will select, second to largest,
third
to
largest and so on). So if I select A2, it should state in M1 the
highest
price for that code from that row.

I hope I came across clear.

Thanks.

:

Cells is returning values. Large wants a range (more precisely,
an
array).

I am slightly confused about your ambition. However, the
following
MAY
help:


Private Sub Worksheet_SelectionChange(ByVal Target As Range)

myrange = Target.Address & ":" & Target.Offset(10).Address

cdamount = WorksheetFunction.Large(Range(myrange), 1)

MsgBox cdamount
End Sub

I interpretted from your question's code that you wanted Large
to
apply
to
column B.
If you want large applied to a row, instead of a column, just
change
Target.Offset(10).Address
to
Target.Offset(,10).Address

If I totally misunderstand what you are trying to do, please add
further
information

HTH

--
Steve

Hello all:

Can someone please help me with this. I have the following vba
code:

cdamount =
WorksheetFunction.Large(Cells(target1,"B"):(Rows(target2,"B")),
amount1)
target1 = Target.Row
target2 = Target.Row + 10

The range for "large" does not work correctly, what should I
change
to
get
this right?

I want to use a worksheet code "Worksheet_SelectionChange"
that
when
I
change my selection it should show me ina differnt cell the
"large"
of
that
specific row which is active.

Please help.

Thanks.
 
Art
I did not see a response to my last message and need to go out for a while
so assumed
I needed to take into account random blank values.

The code below picks a random from the list and puts it in column N of the
target row.

So, selecting a single cell in column A will cause:
- Maximum value of B:L in target row to be place in column M of target row
- A random value selected from the target row to be placed in column N of
the target row.

Change
Target.Offset(, 13) = GetRand(Target.Row)
to
Target.Offset(, x) = GetRand(Target.Row)

to place in another column where x is the desired column number
Example:
Target.Offset(, 14) = GetRand(Target.Row)
will put it in column O

'Code:
'***********************************************************
Const FirstCol = "B"
Const LastCol = "L"
Option Base 1

Private Sub Worksheet_SelectionChange(ByVal Target As Range)

Const amt1 = 1 ' 1 = largest value, 2 = 2nd largest etc
Dim myRange
Dim mySmall As Long
Dim myLarge As Long
Dim myRandBetween As Long
If Target.Count = 1 Then
Set isect = Application.Intersect(Range("A:A"), Target)
If Not isect Is Nothing Then

myRange = Target.Offset(, 1).Resize(1, 11).Address

If Application.WorksheetFunction.CountA(Range(myRange)) > 0 Then
myLarge =
Application.WorksheetFunction.Large(Range(myRange), amt1)
Target.Offset(, 12) = myLarge
Randomize
mySmall =
Application.WorksheetFunction.Min(Range(myRange))
myRandBetween = RandomNumbers(mySmall, myLarge)
Target.Offset(, 13) = GetRand(Target.Row)
End If
End If
End If
End Sub

'The following code assumes your data can appear anywhere
' within the range. If zeros or negative values are included,
'you will need to modify the If c>0 as appropriate.
' Note constants at top of module that refer to columns.

Function GetRand(InRow As Long)

Dim randRange
Dim i As Integer, x As Integer
Dim rtnArr()
randRange = FirstCol & InRow & ":" & LastCol & InRow

Randomize
For Each c In Range(randRange)
If c > 0 Then
i = i + 1
ReDim Preserve rtnArr(i)
rtnArr(i) = c.Value
End If
Next c

x = Int((i - 1 + 1) * Rnd() + 1)

GetRand = rtnArr(x)

End Function
'**************************************************************

--
Steve

art said:
I want the random to give me a random number only from the prices that are
on
that row, so if I have on that row, 1.19 1,49, 1.89, so the random should
only choose between these three numbers.

Thanks for your help

AltaEgo said:
Application.WorksheetFunction does not have a reference to RandBetween
(i.e.
you cannot call it this way). The following piece of code should work.
Place it before the first of the End If lines.


Randomize
mylarge =
Application.WorksheetFunction.Large(Range(myrange), 1)
mysmall =
Application.WorksheetFunction.Small(Range(myrange), 1)
myRandBetween = Int(Rnd() * (mylarge - mysmall + 1) +
mysmall)


To set the value in N1 add the following

Target.Offset(, 13) = myRandBetween


--
Steve

art said:
Thanks it works.

I want to add also a random between code that will give me a random
amount
between the lowest and highest point of that row. The formula I would
use
in
excdel would be: LARGE(B6:K6,RANDBETWEEN(1,COUNTIF(B6:K6,">1"))).

Can you help me finish it off? Thanks so much.

Art.

:

I left a (harmless) debug line in. Now removed from the following:


Private Sub Worksheet_SelectionChange(ByVal Target As Range)

Const amt1 = 1 ' 1 = largest value, 2 = 2nd largest etc
Dim myrange
If Target.Count = 1 Then
Set isect = Application.Intersect(Range("A:A"), Target)
If Not isect Is Nothing Then

myrange = Target.Offset(, 1).Resize(1, 11).Address

If Application.WorksheetFunction.CountA(Range(myrange)) >
0
Then
Target.Offset(, 12) = _

Application.WorksheetFunction.Large(Range(myrange),
amt1)
End If
End If
End If
End Sub

Now to fix the line wrapping problem:

Where the line ends with '= _' in the above, delete the _ and keep
pressing
[Delete] until there is only a single space between '=' and
'Application'.

If that doesn't work, My method is to navigate the cursor to the end
of
the
first line of code that shows red in my module and press [Delete]
until
the
code wraps the following line into position. Once it does that, I move
to
the end of the joined line and press [Enter]. Keep repeating this
process
until all code is black (or green if a comment).



--
Steve

Thanks, but something is wrong. Which part of this is on one line
and
which
on the next?

If Application.WorksheetFunction.CountA(Range(myrange)) > 0 Then
Debug.Print
Application.WorksheetFunction.Large(Range(myrange), amt1)
Target.Offset(, 12) =
Application.WorksheetFunction.Large(Range(myrange), amt1)
End If


:

Sorry about the delay - my daughter took over ownership of the PC!

Using Dave's (superior) Resize() to change the range


Private Sub Worksheet_SelectionChange(ByVal Target As Range)

Const amt1 = 1 ' 1 = largest value, 2 = 2nd largest etc
Dim myrange
If Target.Count = 1 Then 'one cell selected
Set isect = Application.Intersect(Range("A:A"), Target)
If Not isect Is Nothing Then ' you clicked a cell in column
A

myrange = Target.Offset(, 1).Resize(1, 11).Address

If Application.WorksheetFunction.CountA(Range(myrange))

0
Then
Debug.Print
Application.WorksheetFunction.Large(Range(myrange), amt1)
Target.Offset(, 12) =
Application.WorksheetFunction.Large(Range(myrange), amt1)
End If
End If
End If
End Sub



--
Steve

Here is what I want to do.

I have lets say in A1 through A10 codes (112, 113, 114...) In B1
through
K10
is prices , so for EG, code 112 could have in B1 1.99 and in C1
2.99
and
D1
3.99 and so on. And they same for each code.

Here is what I want to do. When I select any "code", in cell M1
should
state
the "large" number (or whatever I will select, second to largest,
third
to
largest and so on). So if I select A2, it should state in M1 the
highest
price for that code from that row.

I hope I came across clear.

Thanks.

:

Cells is returning values. Large wants a range (more precisely,
an
array).

I am slightly confused about your ambition. However, the
following
MAY
help:


Private Sub Worksheet_SelectionChange(ByVal Target As Range)

myrange = Target.Address & ":" & Target.Offset(10).Address

cdamount = WorksheetFunction.Large(Range(myrange), 1)

MsgBox cdamount
End Sub

I interpretted from your question's code that you wanted Large
to
apply
to
column B.
If you want large applied to a row, instead of a column, just
change
Target.Offset(10).Address
to
Target.Offset(,10).Address

If I totally misunderstand what you are trying to do, please add
further
information

HTH

--
Steve

Hello all:

Can someone please help me with this. I have the following vba
code:

cdamount =
WorksheetFunction.Large(Cells(target1,"B"):(Rows(target2,"B")),
amount1)
target1 = Target.Row
target2 = Target.Row + 10

The range for "large" does not work correctly, what should I
change
to
get
this right?

I want to use a worksheet code "Worksheet_SelectionChange"
that
when
I
change my selection it should show me ina differnt cell the
"large"
of
that
specific row which is active.

Please help.

Thanks.
 
Hello:

Thanks for taking your time to help me.

Let me give you the code that I have currently. Note some differences:

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
'set max for scroll bar, according to active cell price range
myrange1 = Target.Offset(, 1).Resize(1, 11).Address
Number = WorksheetFunction.CountIf(Range(myrange1), ">1")
ActiveSheet.ScrollBar1.Max = Number
'set A1 to scrollbar value
amount1 = ActiveSheet.ScrollBar1.Value
ActiveSheet.Cells(1, 1).Value = amount
'show price according to selected range in scroll bar
amt1 = Range("a1") ' 1 = largest value, 2 = 2nd largest etc
Dim myRange

If Target.Count = 1 Then 'one cell selected
Set isect = Application.Intersect(Range("A6:A26"), Target)
If Not isect Is Nothing Then ' you clicked a cell in column A
If Target.Value = "" Then Exit Sub

myRange = Target.Offset(, 1).Resize(1, 11).Address

If Application.WorksheetFunction.CountA(Range(myRange)) > 0 Then
Debug.Print Application.WorksheetFunction.Large(Range(myRange),
amt1)
Range("b5") =
Application.WorksheetFunction.Large(Range(myRange), amt1)

'Randomize

mySmall = Application.WorksheetFunction.Min(Range(myRange))
myRandBetween = RandomNumbers(mySmall, myLarge)
Range("F5") = GetRand(Target.Row)

'Range("F5") = myRandBetween
End If
End If
End If
End Sub

Now everything works except the random. What I have in the ranges B6 to K26
is all the prices. but the prices start from the left, so there is no such
case where its:

blank, 1.19,blank,1.49,1.89,1.12,blank,blank...

Its always

1.19,1.49,1.89,1.12,blank,blank,blank...

Now, I want the random to give me a random amount, but it should only give
me a number from that row. So if I have in one row,
1.19,1.49,1.89,1.12,blank,blank,blank...
it should give me either 1.19 or 1.49 or 1.89 or 1.12 and nothing else. Only
these numbers.

Thank you so much for your help. I really appriciate it.




AltaEgo said:
Art
I did not see a response to my last message and need to go out for a while
so assumed
I needed to take into account random blank values.

The code below picks a random from the list and puts it in column N of the
target row.

So, selecting a single cell in column A will cause:
- Maximum value of B:L in target row to be place in column M of target row
- A random value selected from the target row to be placed in column N of
the target row.

Change
Target.Offset(, 13) = GetRand(Target.Row)
to
Target.Offset(, x) = GetRand(Target.Row)

to place in another column where x is the desired column number
Example:
Target.Offset(, 14) = GetRand(Target.Row)
will put it in column O

'Code:
'***********************************************************
Const FirstCol = "B"
Const LastCol = "L"
Option Base 1

Private Sub Worksheet_SelectionChange(ByVal Target As Range)

Const amt1 = 1 ' 1 = largest value, 2 = 2nd largest etc
Dim myRange
Dim mySmall As Long
Dim myLarge As Long
Dim myRandBetween As Long
If Target.Count = 1 Then
Set isect = Application.Intersect(Range("A:A"), Target)
If Not isect Is Nothing Then

myRange = Target.Offset(, 1).Resize(1, 11).Address

If Application.WorksheetFunction.CountA(Range(myRange)) > 0 Then
myLarge =
Application.WorksheetFunction.Large(Range(myRange), amt1)
Target.Offset(, 12) = myLarge
Randomize
mySmall =
Application.WorksheetFunction.Min(Range(myRange))
myRandBetween = RandomNumbers(mySmall, myLarge)
Target.Offset(, 13) = GetRand(Target.Row)
End If
End If
End If
End Sub

'The following code assumes your data can appear anywhere
' within the range. If zeros or negative values are included,
'you will need to modify the If c>0 as appropriate.
' Note constants at top of module that refer to columns.

Function GetRand(InRow As Long)

Dim randRange
Dim i As Integer, x As Integer
Dim rtnArr()
randRange = FirstCol & InRow & ":" & LastCol & InRow

Randomize
For Each c In Range(randRange)
If c > 0 Then
i = i + 1
ReDim Preserve rtnArr(i)
rtnArr(i) = c.Value
End If
Next c

x = Int((i - 1 + 1) * Rnd() + 1)

GetRand = rtnArr(x)

End Function
'**************************************************************

--
Steve

art said:
I want the random to give me a random number only from the prices that are
on
that row, so if I have on that row, 1.19 1,49, 1.89, so the random should
only choose between these three numbers.

Thanks for your help

AltaEgo said:
Application.WorksheetFunction does not have a reference to RandBetween
(i.e.
you cannot call it this way). The following piece of code should work.
Place it before the first of the End If lines.


Randomize
mylarge =
Application.WorksheetFunction.Large(Range(myrange), 1)
mysmall =
Application.WorksheetFunction.Small(Range(myrange), 1)
myRandBetween = Int(Rnd() * (mylarge - mysmall + 1) +
mysmall)


To set the value in N1 add the following

Target.Offset(, 13) = myRandBetween


--
Steve

Thanks it works.

I want to add also a random between code that will give me a random
amount
between the lowest and highest point of that row. The formula I would
use
in
excdel would be: LARGE(B6:K6,RANDBETWEEN(1,COUNTIF(B6:K6,">1"))).

Can you help me finish it off? Thanks so much.

Art.

:

I left a (harmless) debug line in. Now removed from the following:


Private Sub Worksheet_SelectionChange(ByVal Target As Range)

Const amt1 = 1 ' 1 = largest value, 2 = 2nd largest etc
Dim myrange
If Target.Count = 1 Then
Set isect = Application.Intersect(Range("A:A"), Target)
If Not isect Is Nothing Then

myrange = Target.Offset(, 1).Resize(1, 11).Address

If Application.WorksheetFunction.CountA(Range(myrange)) >
0
Then
Target.Offset(, 12) = _

Application.WorksheetFunction.Large(Range(myrange),
amt1)
End If
End If
End If
End Sub

Now to fix the line wrapping problem:

Where the line ends with '= _' in the above, delete the _ and keep
pressing
[Delete] until there is only a single space between '=' and
'Application'.

If that doesn't work, My method is to navigate the cursor to the end
of
the
first line of code that shows red in my module and press [Delete]
until
the
code wraps the following line into position. Once it does that, I move
to
the end of the joined line and press [Enter]. Keep repeating this
process
until all code is black (or green if a comment).



--
Steve

Thanks, but something is wrong. Which part of this is on one line
and
which
on the next?

If Application.WorksheetFunction.CountA(Range(myrange)) > 0 Then
Debug.Print
Application.WorksheetFunction.Large(Range(myrange), amt1)
Target.Offset(, 12) =
Application.WorksheetFunction.Large(Range(myrange), amt1)
End If


:

Sorry about the delay - my daughter took over ownership of the PC!

Using Dave's (superior) Resize() to change the range


Private Sub Worksheet_SelectionChange(ByVal Target As Range)

Const amt1 = 1 ' 1 = largest value, 2 = 2nd largest etc
Dim myrange
If Target.Count = 1 Then 'one cell selected
Set isect = Application.Intersect(Range("A:A"), Target)
If Not isect Is Nothing Then ' you clicked a cell in column
A

myrange = Target.Offset(, 1).Resize(1, 11).Address

If Application.WorksheetFunction.CountA(Range(myrange))

0
Then
Debug.Print
Application.WorksheetFunction.Large(Range(myrange), amt1)
Target.Offset(, 12) =
Application.WorksheetFunction.Large(Range(myrange), amt1)
End If
End If
End If
End Sub



--
Steve

Here is what I want to do.

I have lets say in A1 through A10 codes (112, 113, 114...) In B1
through
K10
is prices , so for EG, code 112 could have in B1 1.99 and in C1
2.99
and
D1
3.99 and so on. And they same for each code.

Here is what I want to do. When I select any "code", in cell M1
should
state
the "large" number (or whatever I will select, second to largest,
third
to
largest and so on). So if I select A2, it should state in M1 the
highest
price for that code from that row.

I hope I came across clear.

Thanks.

:

Cells is returning values. Large wants a range (more precisely,
an
array).

I am slightly confused about your ambition. However, the
following
MAY
help:


Private Sub Worksheet_SelectionChange(ByVal Target As Range)

myrange = Target.Address & ":" & Target.Offset(10).Address

cdamount = WorksheetFunction.Large(Range(myrange), 1)

MsgBox cdamount
End Sub

I interpretted from your question's code that you wanted Large
to
apply
to
column B.
If you want large applied to a row, instead of a column, just
change
Target.Offset(10).Address
to
Target.Offset(,10).Address

If I totally misunderstand what you are trying to do, please add
further
information
 
I note you are getting values from ScrollBar1. My test sheet is not set up
with a scroll bar so I will not take the time to fully debug and re-write
it. The following will help you understand what the code is doing and to lay
it out more efficiently.

You need to step through your code and see what it is doing. Is what is
happening occurring when and where you want?
To step through:
1) Click on the first line of your code and press F9. The line of code will
highlight brown
2) Go back to your worksheet and click one of the target cells.
3) Press F8 to step through code line-by-line
4) Move your mouse over variable to look at the values they hold
5) Make a mental note what is happening.

6) Repeat above but click on a cell outside the target range.

7) Repeat above but, select a range of cells.

Look for superfluous code (did we add something that no longer applies)

Comment anything out and test thoroughly before deleting.

To comment something out simply add a single inverted comma - '

Comment:

I did not read all of your code but, it is a waste of PC resources running
unwanted code on every selection change. You should aim to run any lines of
code code when you select a single cell within your target range. So, start
thinking about where within your code you should locate your amendments:
Look carefully where you should place your additional code in relation to:

If Target.Count = 1 Then 'one cell selected
Set isect = Application.Intersect(Range("A6:A26"), Target)
If Not isect Is Nothing Then ' you clicked a cell in column A


NOW To get the random working:

Change existing code:

mySmall = Application.WorksheetFunction.Min(Range(myRange))
myRandBetween = RandomNumbers(mySmall, myLarge)
Range("F5") = GetRand(Target.Row)

'Range("F5") = myRandBetween

so it reads (i.e get rid of superfluous code)

Range("F5") = GetRand(Target.Row)



Your module should now look like

Const FirstCol = "B"
Const LastCol = "L"
Option Base 1

Private Sub Worksheet_SelectionChange(ByVal Target As Range)

' your SelectionChange code (with any new modifications)

End Sub

'after the SelectionChange End Sub copy the following

Function GetRand(InRow As Long)

Dim randRange
Dim i As Integer, x As Integer
Dim rtnArr()
randRange = FirstCol & InRow & ":" & LastCol & InRow

Randomize
For Each c In Range(randRange)
If c > 0 Then
i = i + 1
ReDim Preserve rtnArr(i)
rtnArr(i) = c.Value
End If
Next c

x = Int((i - 1 + 1) * Rnd() + 1)

GetRand = rtnArr(x)

End Function


Whist I am trying to get you to do some of the modifications yourself
(learning experience), don't be reluctant to get back to me if you need help
or have problems understanding something.


--
Steve

art said:
Hello:

Thanks for taking your time to help me.

Let me give you the code that I have currently. Note some differences:

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
'set max for scroll bar, according to active cell price range
myrange1 = Target.Offset(, 1).Resize(1, 11).Address
Number = WorksheetFunction.CountIf(Range(myrange1), ">1")
ActiveSheet.ScrollBar1.Max = Number
'set A1 to scrollbar value
amount1 = ActiveSheet.ScrollBar1.Value
ActiveSheet.Cells(1, 1).Value = amount
'show price according to selected range in scroll bar
amt1 = Range("a1") ' 1 = largest value, 2 = 2nd largest etc
Dim myRange

If Target.Count = 1 Then 'one cell selected
Set isect = Application.Intersect(Range("A6:A26"), Target)
If Not isect Is Nothing Then ' you clicked a cell in column A
If Target.Value = "" Then Exit Sub

myRange = Target.Offset(, 1).Resize(1, 11).Address

If Application.WorksheetFunction.CountA(Range(myRange)) > 0
Then
Debug.Print Application.WorksheetFunction.Large(Range(myRange),
amt1)
Range("b5") =
Application.WorksheetFunction.Large(Range(myRange), amt1)

'Randomize

mySmall = Application.WorksheetFunction.Min(Range(myRange))
myRandBetween = RandomNumbers(mySmall, myLarge)
Range("F5") = GetRand(Target.Row)

'Range("F5") = myRandBetween
End If
End If
End If
End Sub

Now everything works except the random. What I have in the ranges B6 to
K26
is all the prices. but the prices start from the left, so there is no such
case where its:

blank, 1.19,blank,1.49,1.89,1.12,blank,blank...

Its always

1.19,1.49,1.89,1.12,blank,blank,blank...

Now, I want the random to give me a random amount, but it should only give
me a number from that row. So if I have in one row,
1.19,1.49,1.89,1.12,blank,blank,blank...
it should give me either 1.19 or 1.49 or 1.89 or 1.12 and nothing else.
Only
these numbers.

Thank you so much for your help. I really appriciate it.




AltaEgo said:
Art
I did not see a response to my last message and need to go out for a
while
so assumed
I needed to take into account random blank values.

The code below picks a random from the list and puts it in column N of
the
target row.

So, selecting a single cell in column A will cause:
- Maximum value of B:L in target row to be place in column M of target
row
- A random value selected from the target row to be placed in column N of
the target row.

Change
Target.Offset(, 13) = GetRand(Target.Row)
to
Target.Offset(, x) = GetRand(Target.Row)

to place in another column where x is the desired column number
Example:
Target.Offset(, 14) = GetRand(Target.Row)
will put it in column O

'Code:
'***********************************************************
Const FirstCol = "B"
Const LastCol = "L"
Option Base 1

Private Sub Worksheet_SelectionChange(ByVal Target As Range)

Const amt1 = 1 ' 1 = largest value, 2 = 2nd largest etc
Dim myRange
Dim mySmall As Long
Dim myLarge As Long
Dim myRandBetween As Long
If Target.Count = 1 Then
Set isect = Application.Intersect(Range("A:A"), Target)
If Not isect Is Nothing Then

myRange = Target.Offset(, 1).Resize(1, 11).Address

If Application.WorksheetFunction.CountA(Range(myRange)) > 0
Then
myLarge =
Application.WorksheetFunction.Large(Range(myRange), amt1)
Target.Offset(, 12) = myLarge
Randomize
mySmall =
Application.WorksheetFunction.Min(Range(myRange))
myRandBetween = RandomNumbers(mySmall, myLarge)
Target.Offset(, 13) = GetRand(Target.Row)
End If
End If
End If
End Sub

'The following code assumes your data can appear anywhere
' within the range. If zeros or negative values are included,
'you will need to modify the If c>0 as appropriate.
' Note constants at top of module that refer to columns.

Function GetRand(InRow As Long)

Dim randRange
Dim i As Integer, x As Integer
Dim rtnArr()
randRange = FirstCol & InRow & ":" & LastCol & InRow

Randomize
For Each c In Range(randRange)
If c > 0 Then
i = i + 1
ReDim Preserve rtnArr(i)
rtnArr(i) = c.Value
End If
Next c

x = Int((i - 1 + 1) * Rnd() + 1)

GetRand = rtnArr(x)

End Function
'**************************************************************

--
Steve

art said:
I want the random to give me a random number only from the prices that
are
on
that row, so if I have on that row, 1.19 1,49, 1.89, so the random
should
only choose between these three numbers.

Thanks for your help

:

Application.WorksheetFunction does not have a reference to RandBetween
(i.e.
you cannot call it this way). The following piece of code should work.
Place it before the first of the End If lines.


Randomize
mylarge =
Application.WorksheetFunction.Large(Range(myrange), 1)
mysmall =
Application.WorksheetFunction.Small(Range(myrange), 1)
myRandBetween = Int(Rnd() * (mylarge - mysmall +
1) +
mysmall)


To set the value in N1 add the following

Target.Offset(, 13) = myRandBetween


--
Steve

Thanks it works.

I want to add also a random between code that will give me a random
amount
between the lowest and highest point of that row. The formula I
would
use
in
excdel would be: LARGE(B6:K6,RANDBETWEEN(1,COUNTIF(B6:K6,">1"))).

Can you help me finish it off? Thanks so much.

Art.

:

I left a (harmless) debug line in. Now removed from the following:


Private Sub Worksheet_SelectionChange(ByVal Target As Range)

Const amt1 = 1 ' 1 = largest value, 2 = 2nd largest etc
Dim myrange
If Target.Count = 1 Then
Set isect = Application.Intersect(Range("A:A"), Target)
If Not isect Is Nothing Then

myrange = Target.Offset(, 1).Resize(1, 11).Address

If Application.WorksheetFunction.CountA(Range(myrange))

0
Then
Target.Offset(, 12) = _

Application.WorksheetFunction.Large(Range(myrange),
amt1)
End If
End If
End If
End Sub

Now to fix the line wrapping problem:

Where the line ends with '= _' in the above, delete the _ and keep
pressing
[Delete] until there is only a single space between '=' and
'Application'.

If that doesn't work, My method is to navigate the cursor to the
end
of
the
first line of code that shows red in my module and press [Delete]
until
the
code wraps the following line into position. Once it does that, I
move
to
the end of the joined line and press [Enter]. Keep repeating this
process
until all code is black (or green if a comment).



--
Steve

Thanks, but something is wrong. Which part of this is on one line
and
which
on the next?

If Application.WorksheetFunction.CountA(Range(myrange)) > 0 Then
Debug.Print
Application.WorksheetFunction.Large(Range(myrange), amt1)
Target.Offset(, 12) =
Application.WorksheetFunction.Large(Range(myrange), amt1)
End If


:

Sorry about the delay - my daughter took over ownership of the
PC!

Using Dave's (superior) Resize() to change the range


Private Sub Worksheet_SelectionChange(ByVal Target As Range)

Const amt1 = 1 ' 1 = largest value, 2 = 2nd largest etc
Dim myrange
If Target.Count = 1 Then 'one cell selected
Set isect = Application.Intersect(Range("A:A"), Target)
If Not isect Is Nothing Then ' you clicked a cell in
column
A

myrange = Target.Offset(, 1).Resize(1, 11).Address

If
Application.WorksheetFunction.CountA(Range(myrange))

0
Then
Debug.Print
Application.WorksheetFunction.Large(Range(myrange), amt1)
Target.Offset(, 12) =
Application.WorksheetFunction.Large(Range(myrange), amt1)
End If
End If
End If
End Sub



--
Steve

Here is what I want to do.

I have lets say in A1 through A10 codes (112, 113, 114...) In
B1
through
K10
is prices , so for EG, code 112 could have in B1 1.99 and in
C1
2.99
and
D1
3.99 and so on. And they same for each code.

Here is what I want to do. When I select any "code", in cell
M1
should
state
the "large" number (or whatever I will select, second to
largest,
third
to
largest and so on). So if I select A2, it should state in M1
the
highest
price for that code from that row.

I hope I came across clear.

Thanks.

:

Cells is returning values. Large wants a range (more
precisely,
an
array).

I am slightly confused about your ambition. However, the
following
MAY
help:


Private Sub Worksheet_SelectionChange(ByVal Target As Range)

myrange = Target.Address & ":" & Target.Offset(10).Address

cdamount = WorksheetFunction.Large(Range(myrange), 1)

MsgBox cdamount
End Sub

I interpretted from your question's code that you wanted
Large
to
apply
to
column B.
If you want large applied to a row, instead of a column, just
change
Target.Offset(10).Address
to
Target.Offset(,10).Address

If I totally misunderstand what you are trying to do, please
add
further
information
 
Thanks for your time. I did what you told me but on error still comes up. The
randomnumbers is highlighted and the popup box says "sub or function not
defined". Seems there is no such predefind function. Is the randomnumbers
supposed to be defined in the custom function as well?

Thank so much.

P.S. I don't want to sound pushy or silly, but would you mind if we discuss
it by email, it would be much easier and faster.

Thanks any way you help me.
 
Back
Top