Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4
Default Choose two numbers

Please try this - choose two numbers and write them

I have two same size number arrays (numbers between -10 and +10), two
columns apart, one in (B3:K22) and the other in (N3:W22). The following
code finds the (i) largest number in the first row of the first array
and writes it in another column (Z3), and (ii) finds the number in the
corresponding column of the second array and writes that in the next
column (AA3). By writing these two numbers from each row of the arrays
to the two new columns, I end up with two columns of selected numbers
that have same number of rows as the arrays.

Try it with two arrays of random numbers between -10 and +10, to fill
the arrays you can use =ROUND(10.5*(RAND()-RAND()),0)
------------------------------------
Sub FindMaxInfo()
Dim lastrow As Long
Dim lastCol As Long
Dim c1 As Integer
Dim row1 As Integer
Dim mx As Integer
Dim mxCol As Integer
Dim maxRow As Integer

Dim colcnt As Integer


row1 = 3
maxRow = 22
While row1 <= maxRow
c1 = 2 ' starting column of first batch on left
colcnt = 12 ' end column of first batch on left
mx = -33555 ' set some max value that is not in list
While c1 < colcnt
lastCol = ActiveSheet.Cells(Columns.Count, c1).End(xlUp).Column
'MsgBox (ActiveSheet.Cells(row1, c1))
If (ActiveSheet.Cells(row1, c1)) mx Then
mx = (ActiveSheet.Cells(row1, c1))
mxCol = c1
End If
'
'
c1 = c1 + 1

Wend
Cells(row1, 27) = Cells(row1, ((mxCol - 1) + 13))
'
'

row1 = row1 + 1
Wend
End Sub
---------------------------------------------

Now what I want to do is the same for (ii) but for (i) I want to find,
not the largest number in each row of the first array, but I want to
find the number that is most different from the average of the row
(the maximum deviation from the average of the row)

Can you adjust to code to do this?

Eventually I will want to use this code with two same size arrays that
have more columns and rows than in this test case, so the code should
note which parameters to change for this.


Thank you for helping.

Tony.

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,163
Default Choose two numbers

The deviation for a particular row would be given by:
deviation = Abs(ActiveSheet.Cells(row1, c1) -
WorksheetFunction.Average(Range(ActiveSheet.Cells( Row1,c1),
ActiveSheet.Cells(row1, c1+9))))

So use this formula in your code to calculate mx:
If (ActiveSheet.Cells(row1, c1)) mx Then
mx = Abs(ActiveSheet.Cells(row1, c1) -
WorksheetFunction.Average(Range(ActiveSheet.Cells( Row1,c1),
ActiveSheet.Cells(row1, c1+9))))
mxCol = c1
End If

Start by setting mx = 0 (no deviation) to ensure you find the maximum, but
other than that the rest of your code should be able to be the same.
--
- K Dales


"Tony" wrote:

Please try this - choose two numbers and write them

I have two same size number arrays (numbers between -10 and +10), two
columns apart, one in (B3:K22) and the other in (N3:W22). The following
code finds the (i) largest number in the first row of the first array
and writes it in another column (Z3), and (ii) finds the number in the
corresponding column of the second array and writes that in the next
column (AA3). By writing these two numbers from each row of the arrays
to the two new columns, I end up with two columns of selected numbers
that have same number of rows as the arrays.

Try it with two arrays of random numbers between -10 and +10, to fill
the arrays you can use =ROUND(10.5*(RAND()-RAND()),0)
------------------------------------
Sub FindMaxInfo()
Dim lastrow As Long
Dim lastCol As Long
Dim c1 As Integer
Dim row1 As Integer
Dim mx As Integer
Dim mxCol As Integer
Dim maxRow As Integer

Dim colcnt As Integer


row1 = 3
maxRow = 22
While row1 <= maxRow
c1 = 2 ' starting column of first batch on left
colcnt = 12 ' end column of first batch on left
mx = -33555 ' set some max value that is not in list
While c1 < colcnt
lastCol = ActiveSheet.Cells(Columns.Count, c1).End(xlUp).Column
'MsgBox (ActiveSheet.Cells(row1, c1))
If (ActiveSheet.Cells(row1, c1)) mx Then
mx = (ActiveSheet.Cells(row1, c1))
mxCol = c1
End If
'
'
c1 = c1 + 1

Wend
Cells(row1, 27) = Cells(row1, ((mxCol - 1) + 13))
'
'

row1 = row1 + 1
Wend
End Sub
---------------------------------------------

Now what I want to do is the same for (ii) but for (i) I want to find,
not the largest number in each row of the first array, but I want to
find the number that is most different from the average of the row
(the maximum deviation from the average of the row)

Can you adjust to code to do this?

Eventually I will want to use this code with two same size arrays that
have more columns and rows than in this test case, so the code should
note which parameters to change for this.


Thank you for helping.

Tony.


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4
Default Choose two numbers

Thank's K Dales for your super fast response.

I can't wait to try out your code tonight as soon as I get back from
work.
Thanks again - Tony

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4
Default Choose two numbers

I ran the new code below. Why did i get a "compile error: syntax
error" on line
mx = Abs(ActiveSheet.Cells(row1, c1)-
------------------------------------
Sub FindMaxInfo()
Dim lastrow As Long
Dim lastCol As Long
Dim c1 As Integer
Dim row1 As Integer
Dim mx As Integer
Dim mxCol As Integer
Dim maxRow As Integer

Dim colcnt As Integer


row1 = 3
maxRow = 22
While row1 <= maxRow
c1 = 2
colcnt = 12
mx = -33 ' set some minimum value that is not in list
mxCol = c1
While c1 < colcnt
lastCol = ActiveSheet.Cells(Columns.Count, c1).End(xlUp).Column
'MsgBox (ActiveSheet.Cells(row1, c1))
If (ActiveSheet.Cells(row1, c1)) mx Then
mx = Abs(ActiveSheet.Cells(row1, c1)-
WorksheetFunction.Average(Range(ActiveSheet.Cells( Row1,c1),
ActiveSheet.Cells(row1, c1+9))))
mxCol = c1
End If
'
'
c1 = c1 + 1

Wend
Cells(row1, 27) = Cells(row1, ((mxCol - 1) + 13))
'
'

row1 = row1 + 1
Wend
End Sub
----------

?

Tony

  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 414
Default Choose two numbers

The line has been wrapped by your newsreader. Try:

'----------------------------------------
Sub FindMaxInfo()
Dim lastrow As Long
Dim lastCol As Long
Dim c1 As Integer
Dim row1 As Integer
Dim mx As Integer
Dim mxCol As Integer
Dim maxRow As Integer

Dim colcnt As Integer

row1 = 3
maxRow = 22
While row1 <= maxRow
c1 = 2
colcnt = 12
mx = -33 ' set some minimum value that is not in list
mxCol = c1
While c1 < colcnt
lastCol = ActiveSheet.Cells(Columns.Count, c1).End(xlUp).Column
'MsgBox (ActiveSheet.Cells(row1, c1))
If (ActiveSheet.Cells(row1, c1)) mx Then
mx = Abs(ActiveSheet.Cells(row1, c1) - _
WorksheetFunction.Average(Range _
(ActiveSheet.Cells(row1, c1), _
ActiveSheet.Cells(row1, c1 + 9))))
mxCol = c1
End If
'
'
c1 = c1 + 1

Wend
Cells(row1, 27) = Cells(row1, ((mxCol - 1) + 13))
'
'
row1 = row1 + 1
Wend
End Sub
'-----------------------------------------------------

Hope this helps
Rowan

Tony wrote:
I ran the new code below. Why did i get a "compile error: syntax
error" on line
mx = Abs(ActiveSheet.Cells(row1, c1)-
------------------------------------
Sub FindMaxInfo()
Dim lastrow As Long
Dim lastCol As Long
Dim c1 As Integer
Dim row1 As Integer
Dim mx As Integer
Dim mxCol As Integer
Dim maxRow As Integer

Dim colcnt As Integer


row1 = 3
maxRow = 22
While row1 <= maxRow
c1 = 2
colcnt = 12
mx = -33 ' set some minimum value that is not in list
mxCol = c1
While c1 < colcnt
lastCol = ActiveSheet.Cells(Columns.Count, c1).End(xlUp).Column
'MsgBox (ActiveSheet.Cells(row1, c1))
If (ActiveSheet.Cells(row1, c1)) mx Then
mx = Abs(ActiveSheet.Cells(row1, c1)-
WorksheetFunction.Average(Range(ActiveSheet.Cells( Row1,c1),
ActiveSheet.Cells(row1, c1+9))))
mxCol = c1
End If
'
'
c1 = c1 + 1

Wend
Cells(row1, 27) = Cells(row1, ((mxCol - 1) + 13))
'
'

row1 = row1 + 1
Wend
End Sub
----------

?

Tony



  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4
Default Choose two numbers

Yes - Thank you Rowan. That was it. Now it works

Best

Tony

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Trying to randomly choose a number from a set list of numbers RJTheDestroyer Excel Worksheet Functions 1 April 25th 09 06:19 AM
How to choose and add the highest 7 numbers out of 10? perfection Excel Discussion (Misc queries) 2 April 12th 08 07:25 PM
Randomly Choose Two Numbers dennis Excel Discussion (Misc queries) 2 September 7th 06 04:43 AM
How can I choose top two numbers in a column? Doug Excel Worksheet Functions 3 May 1st 05 11:04 PM
How do I choose to only add the first 3 numbers in a range? Tom Ogilvy Excel Programming 0 September 15th 04 02:24 PM


All times are GMT +1. The time now is 10:18 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"