确定三个值之间百分比差异的代码

Code to determining percent difference between three values

我想找出单个列中三个值之间的百分比差异并将百分比差异转移到新列

Lets say I have the following values to compare: 
**Value1** = 5 
**Value2** = 8 
**Value3** = 2

我需要从 Value1 和 Value3 中减去 Value2,还需要从 Value3 中减去 Value1。

我想要一个简单的方法来找到一式三份的最大值并减去剩余的两个值以找到百分比差异,然后找到第二高的值并减去最低的值以找到其百分比差异。

也许我处理这一切都是错误的。任何帮助是极大的赞赏。

****CODE*****

import openpyxl
from openpyxl.worksheet.datavalidation import DataValidation

wb = openpyxl.load_workbook('05.16.17 CaMKII Sean RD.xlsx', read_only= False, data_only = True)


sheet = wb['Sheet1']

### Protein Max Value for Triplicate 

for i in range(2,3):
    sheet['D{}'.format(i)] = '=max(C2:C4)'.format(i)
for i in range(2,3):
    sheet['E{}'.format(i)] = '=((D2-C2)/(D2+C2) * 100)'.format(i)
for i in range(3,4):
    sheet['E{}'.format(i)] = '=((D2-C3)/(D2+C3) * 100)'.format(i)
for i in range(4,5):
    sheet['E{}'.format(i)] = '=((C3-C2)/(C2+C3) * 100)'.format(i)


wb.save('camdatapolishedoutput.xlsx')

我没有清楚地理解您的代码,但您似乎在寻找 FormatPercent 公式。而且您还必须更改代码的许多部分。下面我已经更正了很多,但只要我不知道你到底想做什么,剩下的就是你的了。

 Dim wb as Workbook, sheet as Worksheet
 Dim wsArrD2 As Range, wsArrC2 As Range, wsArrC3 As Range, wsArrC4 As Range
 Dim yourdesktopaddress as String
 yourdesktopaddress = CreateObject("WScript.Shell").specialfolders("Desktop")
 Set wb = Application.Workbooks.Open(yourdesktopaddress & ".16.17 CaMKII Sean RD.xlsx")
 ''This code finds your Desktop address and opens the specific file.

 Set sheet = wb.Sheets("Sheet1")
 'I assume you were looking for information from that file's Sheet1 sheet.

 Set wsArrD2 = sheet.Range("D2")
 Set wsArrC2 = sheet.Range("C2")
 Set wsArrC3 = sheet.Range("C3")
 Set wsArrC4 = sheet.Range("C4")

 Dim maxC as Long, PercentVar1 as String
 Dim PercentVar2 as String, PercentVar4 as String
'You should define your '%' variables as String otherwise you will get an Run Time 13 Error'

 maxC = Evaluate("=MAX(C2:C4)")
 PercentVar1 = FormatPercent((wsArrD2 - wsArrC2) / (wsArrD2 + wsArrC2), 2)
 PercentVar2 = FormatPercent((wsArrD2 - wsArrC3) / (wsArrD2 + wsArrC3), 2)
 PercentVar3 = FormatPercent((wsArrC3 - wsArrC2) / (wsArrC3 + wsArrC2), 2)

In this formula you don't need to multiply it by 100 because: FormatPercent Returns an expression formatted as a percentage (that is, multiplied by 100) with a trailing % character.

For more info please visit FormatPercent Function (Visual Basic) on msdm.microsoft