當我嘗試運行 sub 時收到型別不匹配 (13) 錯誤。這似乎與我傳遞給函式 DivisionCase 的引數 j 有關。我不明白為什么,請賜教。第二個想法:我需要通過 j byref 嗎?
Function Last_Row(column)
'Find the last Row with data in a column
With ActiveSheet
Last_Row = .Cells(.Rows.Count, column).End(xlUp).Row
End With
End Function
Sub verticalProfileCalc()
Dim currentParaBVC As Variant: currentParaBVC = Null
Dim j As Long
Dim value1 As Variant
Dim value2 As Variant
For j = 2 To Last_Row(11)
value1 = CDec(Cells(j, 12).Value)
value2 = CDec(Cells(j, 13).Value)
Cells(j, 16).Value = CDec(DivisionCase(value1, value2, j, currentParaBVC))
Next j
End Sub
Function DivisionCase(ByVal downchain As Variant, ByVal upchain As Variant, ByVal currentRow As Long, ByRef currentParaBVC As Variant) As Variant
Dim downchainSection As Integer: downchainSection = Cells(currentRow, 14).Value
Dim upchainSection As Integer: upchainSection = Cells(currentRow, 15).Value
Dim verticalProfile As Variant: verticalProfile = CDec(Cells(currentRow, 16).Value)
Dim LineType As String: LineType = Application.VLookup(downchainSection, "CurveSchedule", 4, False)
Dim Grade As Variant
Dim ParabolicLength As Variant: ParabolicLength = CDec(Application.VLookup(downchainSection, "CurveSchedule", 3, False) - Application.VLookup(downchainSection, "CurveSchedule", 2, False))
If currentRow = 2 Then
verticalProfile = CDec(Range("InitialPoint").Value)
'temporarily assignd initvalue to cell P2
ElseIf downchainSection = upchainSection Then
If LineType = "Straight" Then
'do something
ElseIf LineType = "Parabolic" Then
'do something
End If
ElseIf ((upchainSection - downchainSection) = 1) Then
If LineType = "Straight" Then
'do something
ElseIf LineType = "Parabolic" Then
'do something
End If
End If
DivisionCase = CDec(verticalProfile)
End Function
uj5u.com熱心網友回復:
一種可能的解決方案
我用我的一些虛構資料給了你的代碼,這就是我所看到的。這一行:
Dim LineType As String: LineType = Application.VLookup(downchainSection, "CurveSchedule", 4, False)
它在 Type 13 Mismatch 上失敗,因為它應該是:
Dim LineType As String: LineType = Application.WorksheetFunction.VLookup(downchainSection, "CurveSchedule", 4, False)
您的物件參考錯誤;你需要WorksheetFunction. 不過,這可能不是您唯一的問題...
其他可能需要檢查的問題
不過,僅修復上述問題并沒有奏效。我不知道“CurveSchedule”指的是什么——可能是命名范圍或串列物件表?無論哪種方式,VBA 都將其作為寫入的字串讀取。如果它是一個命名范圍,我讓它與ActiveSheet.Range("CurveSchedule"). 所以對我有用的行看起來像這樣:
Dim LineType As String: LineType = Application.WorksheetFunction.VLookup(downchainSection, Range("CurveSchedule"), 4, False)
另外,因為我正在撰寫自己的資料,所以我遇到了 var 的問題,downchainSection因為在分配該單元格時我沒有任何值。大概,你這樣做,所以對你來說可能不是問題。
我不知道這是否是您的問題,但如果這對您沒有幫助,我強烈建議您逐步使用 F8(無論如何在 Windows 上),并準確告訴我們它在哪一行代碼上失敗了。一旦我發現代碼失敗的那一行,我就開始把它改成更簡單的東西(因為我傾向于寫復雜的想法,我比我更好)直到我發現這條線正常作業,然后回到復雜的視圖什么有效。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/407577.html
標籤:
下一篇:使用宏計算來自不同單元格的資料
