Skip to content

Instantly share code, notes, and snippets.

@kardeiz
Created July 3, 2012 22:19
Show Gist options
  • Save kardeiz/3043781 to your computer and use it in GitHub Desktop.
Save kardeiz/3043781 to your computer and use it in GitHub Desktop.
Excel VBA: t-test for summary results
Function ttest_from_sum(x As Range, y As Range, values As Range)
' Set up for dragging over names with lookup
' Change this to fit your needs
Set temp = values.Find(x.Value, LookIn:=xlValues)
avga = CDec(temp.Offset(1, 0).Value)
stda = CDec(temp.Offset(2, 0).Value)
numa = CDec(temp.Offset(3, 0).Value)
Set temp = values.Find(y.Value, LookIn:=xlValues)
avgb = CDec(temp.Offset(1, 0).Value)
stdb = CDec(temp.Offset(2, 0).Value)
numb = CDec(temp.Offset(3, 0).Value)
pooled_sd = Sqr(((stda * stda) / numa) + ((stdb * stdb) / numb))
t_stat = (avga - avgb) / pooled_sd
' Set up for two-tailed probability
t_prob = WorksheetFunction.TDist(Abs(t_stat), (numa + numb) - 2, 2)
ttest_from_sum = Round(t_prob,2)
End Function
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment