Click here to Skip to main content
15,910,878 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

i am struck with one problem in VBA script.

i want to develop a macro for my following problem

i have two columns dont know the no of rows... each column contains some numbers.
Suppose A1:A10 there age values similarly B1:B10 there are respective weight values

on A11 i want to find the max of A1:A10 and At B11 it should display corresponding weight value of MAX(A1:A10)..

here A1:A10 i mentioned for example but in real data i dont know no of rows.

Can any help out me on this... along with the what is the formula use for EXECElsheet
Posted

1 solution

A good way of figuring out references etc. is to record a macro in the workbook and then look at the completed macro to find the commands required to solve the problem. For example in the following macro I had values in the first four rows of column A. Starting from A1 I used <End><down> to get to the last row, moved down 1 and entered the max formula. The resulting macro is:

VB
Sub Macro1()
'
' Macro1 Macro
'
    Selection.End(xlDown).Select
    Range("A5").Select
    ActiveCell.FormulaR1C1 = "=MAX(R[-4]C:R[-1]C)"
    Range("A6").Select
End Sub

A little experimentation should help to figure out how to make that work for any case.
 
Share this answer
 
Comments
Arun India 28-Jun-12 11:10am    
Thanks for the reply... but if we use the above code .... we need to know the no of rows... but currently i don't know how many rows to put at the MAX(R[-4]C:R[-1]C). More over at B6 i should get the corresponding B column value of MAX(A1:A5).. which is not there in your code..
Richard MacCutchan 28-Jun-12 11:30am    
I did not claim this as a full solution but offered it as a suggestion for how to get some information from Excel itself. The MAX() formula obviously needs modifying to include the starting cell as well, and the formula can go in any cell that you desire. You just need to do some work to finalise it.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900