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:

[TABLE="width: 100"]
<tbody>[TR]
[TD]Student[/TD]
[TD]Mark[/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]Alex
[/TD]
[TD]9,5[/TD]
[/TR]
[TR]
[TD]Sophie[/TD]
[TD]7[/TD]
[/TR]
[TR]
[TD]Nick[/TD]
[TD]8[/TD]
[/TR]
[TR]
[TD]Paul[/TD]
[TD]4[/TD]
[/TR]
[TR]
[TD]Helena[/TD]
[TD]9[/TD]
[/TR]
[TR]
[TD]jan[/TD]
[TD]6[/TD]
[/TR]
</tbody>[/TABLE]

table 2 worksheet 2:
[TABLE="width: 100"]
<tbody>[TR]
[TD]Best students[/TD]
[/TR]
[TR]
[TD]?[/TD]
[/TR]
[TR]
[TD]?[/TD]
[/TR]
[TR]
[TD]?[/TD]
[/TR]
</tbody>[/TABLE]
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
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
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,284
Members
452,630
Latest member
OdubiYouth

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top