我打开了两张 Excel 表格,并根据所有三张都相似的列对它们进行了排序。这部分代码有效并且很好。现在我需要引用两个不同工作簿中的两个单元格并将它们相减。我有一组复杂的循环,但我不断出错,所以我将它剥离为一个基本的减法函数。使用此脚本,消息框现在显示“mprice_delta”而不是数值。我不知道我做了什么,但它工作了一次,然后又恢复为显示文本答案而不是数字。
Dim objTPG, objTPG_prior
Set objXL = CreateObject("Excel.Application")
objXL.Visible = True
objXL.DisplayAlerts = False
Set objTPG = objXL.Application.Workbooks.Open(FilePath & "TPG.csv")
Const xlAscending = 1
Const xlYes = 1
Set objTPGSHEET = objTPG.Worksheets(1)
Set objRange = objTPGSHEET.UsedRange
Set objRange2 = objTPGSHEET.Range("E2:E9999")
objRange.Sort objRange2, xlAscending,,,,,, xlYes
Set objTPGPRIOR = objXL.Application.Workbooks.Open(FilePath & "TPG Prior.csv")
'REMEMBER TO MAKE SHEETS RUN IN BACKGROUND
Set objTPGPRIORSHEET = objTPGPRIOR.Worksheets(1)
Set objRange = objTPGPRIORSHEET.UsedRange
Set objRange2 = objTPGPRIORSHEET.Range("E2:E9999")
objRange.Sort objRange2, xlAscending,,,,,, xlYes
Set objEXPO = objXL.Application.Workbooks.Open(FinalExposure)
Set objEXPOSHEET = objEXPO.Worksheets(1)
Set objRange = objEXPOSHEET.UsedRange
Set objRange2 = objEXPOSHEET.Range("I2:I9999")
objRange.Sort objRange2, xlAscending,,,,,, xlYes
Function mysub(x,y)
mysub = x - y
End Function
Dim mprice_delta
mprice_delta = mysub(objTPG.Cells(2,10).Value, objTPGPRIOR.Cells(2,9).Value)
MsgBox(mprice_delta)