VBA problems with finding max values en copying names to a different worksheet.

TimExcel123

New Member
Joined
May 30, 2019
Messages
2
Hi guys,

My name is Tim and I am new to this Forum.
I experience a lot of trouble while designing my first code in Excel VBA. The problem is as following: in column B(worksheet 1), I inserted the names of some students. in column C, I inserted the marks of the corresponding students (see table 1).

In worksheet 2 table 2, I want cells A1:A3 to display the names of the 3 best students. After watching a lot of Youtube-tutorials, I still did not manage tot figure out how to do it. Is there anyone who could help me designing this code in VBA?

Thanks in advance!
-------------------------------------------------------

Table 1 worksheet 1:

StudentMark
Alex
9,5
Sophie7
Nick8
Paul4
Helena9
jan6

<tbody>
</tbody>

table 2 worksheet 2:
Best students
?
?
?

<tbody>
</tbody>
 

Some videos you may like

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.

DanteAmor

Well-known Member
Joined
Dec 3, 2018
Messages
12,585
Office Version
2007
Platform
Windows
Using the Large formula you can find the first 3 places.

Code:
Sub finding_max_values()
    Dim i As Long, r As Long
    r = Sheets("Sheet1").Range("B" & Rows.Count).End(xlUp).Row
    For[COLOR=#0000ff] i [/COLOR]= 1 To 3
        Sheets("Sheet2").Range("A" & i + 1).Value = Evaluate _
            ("=INDEX(Sheet1!B1:B" & r & ",SUMPRODUCT((LARGE(Sheet1!C2:C" & r & "+(ROW(C2:C" & r & ")/1000)," &[COLOR=#0000ff] i [/COLOR]& _
            ")=Sheet1!C2:C" & r & "+(ROW(C2:C" & r & ")/1000))*(ROW(C2:C" & r & "))))")
    Next
End Sub
---
Or copying the data from sheet1 to sheet2, sorting and deleting data from 4 down, that way only the first 3 are left.


Code:
Sub Macro6()
    Sheets("Sheet1").Range("B:C").Copy Sheets("Sheet2").Range("A1")
    Sheets("Sheet2").Range("A1").CurrentRegion.Sort key1:=Sheets("Sheet2").Range("B1"), order1:=xlDescending, Header:=xlYes
    Sheets("Sheet2").Range("A5:B" & Rows.Count).ClearContents
End Sub
 

TimExcel123

New Member
Joined
May 30, 2019
Messages
2
Dear Dante Amor,

Thanks so much for helping me!
Your answer is really helpful!

Regards, Tim
 

DanteAmor

Well-known Member
Joined
Dec 3, 2018
Messages
12,585
Office Version
2007
Platform
Windows
I'm glad to help you. Thanks for the feedback.
 

Subscribe on YouTube

Watch MrExcel Video

Forum statistics

Threads
1,106,625
Messages
5,512,473
Members
408,899
Latest member
cve60069

This Week's Hot Topics

  • Sort code advice please
    Hi, I have the code below which im trying to edit but getting a little stuck. This was the original code which worked fine,columns A-F would sort...
  • SUMPRODUCT with nested If statement
    Hi everyone, Hope you're all well. I'm hoping someone will be able to point me in the right direction with a problem I'm having with a SUMPRODUCT...
  • VBA - simple sort is killing me!
    Hello all! This should be so easy, but not for me, apparently! I have a table of data that can be of varying lengths and widths. My current macro...
  • Compare Two Lists
    I have two Lists and I need to be able to Identify differences between them. List 100 comes from a workbook - the other is downloaded form the...
  • Formula that deducts points for each code I input.
    I am trying to create a formula that will have each student in my class start at 100 points and then for each code that I enter (PP for Poor...
  • Conditional formatting formula required for day of week and a value
    Hi, I have a really simple spreadsheet where column A is the date, column B is the activity total shown as a number and column C states the day of...
Top