0
我打開了兩個excel表格並根據三個類似的列對它們進行了排序。代碼的那部分起作用並且很好。現在我需要引用兩個不同工作簿中的兩個單元格並將其減去。我有一套很複雜的循環,但我一直在收到錯誤,所以我將其簡化爲一個基本的減法函數。現在使用此腳本,消息框顯示「mprice_delta」而不是數字值。我不知道我做了什麼,但它曾經工作過一次,然後恢復爲顯示文本答案而不是數字。從Excel中的兩個不同的工作簿中減去兩個單元格
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)