Median of row with VBA, without excel built-in functions

Technology

New Member
Joined
Apr 29, 2021
Messages
25
Office Version
  1. 365
Platform
  1. Windows
Row 2 has the following values from left to right:
0​
0​
0​
0​
0​
0​
0​
0​
0​
0​
0​
0​
1​
1​
1​
1​
1​
1​
1​
1​
1​
1​
1​
1​
1​
1​
1​
1​
1​
2​
2​
2​
2​
2​
2​
2​
2​
2​
2​
3​
3​
3​
3​
3​
3​
3​
3​
3​
3​
3​
3​
3​
3​
3​
3​
4​
4​
4​
4​
4​
4​
4​
4​
4​
4​
4​
4​
4​
4​
4​
5​
5​
5​
5​
5​
5​
5​
5​
5​
6​
6​
6​
6​
6​
6​
6​
6​
6​
6​
6​
6​
6​
6​
6​
6​
6​
6​
6​
6​
6​
7​
7​
7​
7​
7​
7​
7​
7​
7​
7​
7​
7​
8​
8​
8​
8​
8​
8​
8​
8​
8​
8​
8​
8​
8​
9​
9​
9​
9​
9​
9​
9​
9​
10​
10​
10​
10​
10​
Result needs to be placed in A3.

Any help will be greatly appreciated.
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
VBA Code:
Sub DoTheThing()

Dim sht As Worksheet
Dim LastCol As Long

Set sht = ActiveSheet

LastCol = sht.Cells(2, sht.Columns.Count).End(xlToLeft).Column

If LastCol Mod 2 = 1 Then

sht.Cells(3, 1) = sht.Cells(2, (LastCol / 2) + 1)

Else

sht.Cells(3, 1) = sht.Cells(2, (LastCol / 2))

End If
End Sub
 
Last edited:
Upvote 0
I believe the code suggested in the previous post could return incorrect results. For example, try running it after changing cell BR2 to 5 instead of 4.

If your data is always 138 columns like your one sample then all you need is
VBA Code:
Sub median_v1()
  Range("A3").Value = (Range("BQ2").Value + Range("BR2").Value) / 2
End Sub

However if the number of columns can vary then try
VBA Code:
Sub median_v2()
  Dim LastCol As Long
  
  LastCol = Cells(2, Columns.Count).End(xlToLeft).Column
  If LastCol Mod 2 = 1 Then
    Range("A3").Value = Cells(2, (LastCol + 1) / 2).Value
  Else
    Range("A3").Value = Application.Sum(Cells(2, LastCol / 2).Resize(, 2)) / 2
  End If
End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,215,048
Messages
6,122,862
Members
449,097
Latest member
dbomb1414

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