我正在做一個遊戲練習我的編碼的Excel表,因爲我上次使用VBA已經兩年了,而且我的訓練很基礎。如果你不介意看我的代碼,並給我一個可能發生的事情的想法,我將不勝感激。對於代碼的重複性抱歉。下面是代碼的功能和代碼本身的描述。VBA功能正常工作,但導致Excel崩潰
基本上,我正在閱讀C9:G9中視頻遊戲角色的每個天賦的內容。它會讀取每一個以查看它是否與用戶選擇的類別相符,如果它符合,那麼它將獲得與C10:G10中的天賦相關的稀有性。獲得該信息後,它使用索引函數從另一張表中讀取包含與天賦類別和人才稀缺程度相關的百分比(例如「Common Defense」)的值。通過首先搜索按字母順序排序的天賦列表(行值),然後按照從C10:G10指示的稀有度來分配列值來決定行列索引值。
該代碼似乎正在執行我想要的方式,但每當我嘗試將此函數向下拖動幾行excel時,它會導致程序凍結並崩潰。
Function TalentCalc(category As String) As Single
Application.Volatile
Dim Rarity As String
Dim TableVal As Single
Dim CategoryRow As Single
Dim RarityCol As Single
For i = 1 To 12 Step 1
If category = Cells(3 + i, "M") Then
CategoryRow = i
i = 11
End If
Next i
If Cells(9, "C") = category Then
Rarity = Cells(10, "C")
If Rarity = "Common" Then
RarityCol = 1
ElseIf Rarity = "Rare" Then
RarityCol = 2
ElseIf Rarity = "Epic" Then
RarityCol = 3
Else
MsgBox ("Pick a rarity.")
End If
TableVal = WorksheetFunction.Index(Worksheets("Talents").Range("B2:D13"), CategoryRow, RarityCol)
TalentCalc = TalentCalc + TableVal
End If
If Cells(9, "D") = category Then
Rarity = Cells(10, "D")
If Rarity = "Common" Then
RarityCol = 1
ElseIf Rarity = "Rare" Then
RarityCol = 2
ElseIf Rarity = "Epic" Then
RarityCol = 3
Else
MsgBox ("Pick a rarity.")
End If
TableVal = WorksheetFunction.Index(Worksheets("Talents").Range("B2:D13"), CategoryRow, RarityCol)
TalentCalc = TalentCalc + TableVal
End If
If Cells(9, "E") = category Then
Rarity = Cells(10, "E")
If Rarity = "Common" Then
RarityCol = 1
ElseIf Rarity = "Rare" Then
RarityCol = 2
ElseIf Rarity = "Epic" Then
RarityCol = 3
Else
MsgBox ("Pick a rarity.")
End If
TableVal = WorksheetFunction.Index(Worksheets("Talents").Range("B2:D13"), CategoryRow, RarityCol)
TalentCalc = TalentCalc + TableVal
End If
If Cells(9, "F") = category Then
Rarity = Cells(10, "F")
If Rarity = "Common" Then
RarityCol = 1
ElseIf Rarity = "Rare" Then
RarityCol = 2
ElseIf Rarity = "Epic" Then
RarityCol = 3
Else
MsgBox ("Pick a rarity.")
End If
TableVal = WorksheetFunction.Index(Worksheets("Talents").Range("B2:D13"), CategoryRow, RarityCol)
TalentCalc = TalentCalc + TableVal
End If
If Cells(9, "G") = category Then
Rarity = Cells(10, "G")
If Rarity = "Common" Then
RarityCol = 1
ElseIf Rarity = "Rare" Then
RarityCol = 2
ElseIf Rarity = "Epic" Then
RarityCol = 3
Else
MsgBox ("Pick a rarity.")
End If
TableVal = WorksheetFunction.Index(Worksheets("Talents").Range("B2:D13"), CategoryRow, RarityCol)
TalentCalc = TalentCalc + TableVal
End If
End Function
* 「撞車」 *:具體是什麼意思? Excel關閉?你會得到一個特定的錯誤信息?它變得沒有反應? – trincot
在for循環中將'i = 11'更改爲'Exit For'。 –
或者直接從代碼中移除'i = 11'。 – Vityata