重新來
Private isUpdating As Boolean
Private Sub CommandButton39_Click()
' 向上滾動
If TextBox109.Top < 0 Then
' 每次向上滾動 10 像素
TextBox109.Top = TextBox109.Top + 10
End If
End Sub
Private Sub CommandButton40_Click()
' 向下滾動
' 檢查 Label 的底部邊界是否超出可見範圍,以避免 Label 滾出視窗
If TextBox109.Top + TextBox109.Height > Me.Height Then
' 每次向下滾動 10 像素
TextBox109.Top = TextBox109.Top - 10
End If
End Sub
Private Sub Frame1_Click()
End Sub
Private Sub Label22_Click()
End Sub
Private Sub UserForm_Initialize()
Dim rng As Range
Set rng = ThisWorkbook.Sheets("武將表").Range("F12:F121")
' 初始化所有武將 ComboBox
InitComboBox Me.ComboBox1, rng
InitComboBox Me.ComboBox2, rng
InitComboBox Me.ComboBox3, rng
InitComboBox Me.ComboBox4, rng
InitComboBox Me.ComboBox5, rng
InitComboBox Me.ComboBox6, rng
' 初始化兵種 ComboBox
InitTypeComboBox Me.ComboBox19, ThisWorkbook.Sheets("武將表").Range("M11:Q11")
InitTypeComboBox Me.ComboBox20, ThisWorkbook.Sheets("武將表").Range("M11:Q11")
' 初始化特定範圍 TextBox 的值為空白
ClearTextBox 7, 12
End Sub
Private Sub InitComboBox(cmb As MSForms.comboBox, rng As Range)
Dim cell As Range
cmb.Clear
For Each cell In rng
If Not IsEmpty(cell.value) Then
cmb.AddItem cell.value
End If
Next cell
End Sub
Private Sub InitTypeComboBox(cmb As MSForms.comboBox, rng As Range)
Dim cell As Range
cmb.Clear
For Each cell In rng
If Not IsEmpty(cell.value) Then
cmb.AddItem cell.value
End If
Next cell
End Sub
Private Sub ClearTextBox(startIndex As Integer, endIndex As Integer)
Dim i As Integer
For i = startIndex To endIndex
Me.Controls("TextBox" & i).value = ""
Next i
End Sub
Private Sub ComboBox_Change(comboBox As MSForms.comboBox, displayTextBox As MSForms.TextBox, associatedTextBoxStart As Integer, displayTextBox67 As MSForms.TextBox)
If Not isUpdating Then
UpdateComboBoxes (IIf(comboBox.name Like "ComboBox[1-3]", 1, 2))
UpdateTextBox comboBox, displayTextBox
UpdateTextBox67WithComboBox comboBox, displayTextBox67
UpdateAssociatedTextBoxes comboBox, associatedTextBoxStart
End If
Dim txtIndex As Integer: txtIndex = CInt(Mid(comboBox.name, Len(comboBox.name)))
Me.Controls("TextBox" & (txtIndex + 6)).value = IIf(comboBox.value = "", "", "10000")
End Sub
Private Sub ComboBox1_Change(): ComboBox_Change Me.ComboBox1, Me.TextBox1, 13, Me.TextBox67: End Sub
Private Sub ComboBox2_Change(): ComboBox_Change Me.ComboBox2, Me.TextBox2, 19, Me.TextBox68: End Sub
Private Sub ComboBox3_Change(): ComboBox_Change Me.ComboBox3, Me.TextBox3, 25, Me.TextBox69: End Sub
Private Sub ComboBox4_Change(): ComboBox_Change Me.ComboBox4, Me.TextBox4, 31, Me.TextBox70: End Sub
Private Sub ComboBox5_Change(): ComboBox_Change Me.ComboBox5, Me.TextBox5, 37, Me.TextBox71: End Sub
Private Sub ComboBox6_Change(): ComboBox_Change Me.ComboBox6, Me.TextBox6, 43, Me.TextBox72: End Sub
Private Sub UpdateComboBoxes(group As Integer)
Dim items As Collection
Set items = New Collection
Dim i As Integer, j As Integer, k As Integer
Dim combos(1 To 3) As MSForms.comboBox
Dim selectedValues As Collection
Dim currentValue As String
isUpdating = True
Set selectedValues = New Collection
For i = 1 To 3
Set combos(i) = Me.Controls("ComboBox" & ((group - 1) * 3 + i))
Next i
' 收集所有已選擇的值
For i = 1 To 3
If combos(i).value <> "" Then
On Error Resume Next
selectedValues.Add combos(i).value, CStr(combos(i).value)
On Error GoTo 0
End If
Next i
' 更新可選項
For i = 1 To 3
currentValue = combos(i).value
InitComboBox combos(i), ThisWorkbook.Sheets("武將表").Range("F12:F121")
' 移除已選擇的值
For j = 1 To selectedValues.Count
If CStr(selectedValues(j)) <> currentValue Then
For k = combos(i).ListCount - 1 To 0 Step -1
If combos(i).List(k) = selectedValues(j) Then
combos(i).RemoveItem k
End If
Next k
End If
Next j
combos(i).value = currentValue
Next i
isUpdating = False
End Sub
Private Sub UpdateTextBox(cmb As MSForms.comboBox, txt As MSForms.TextBox)
Dim ws As Worksheet
Dim found As Range
Dim r As Range
Set ws = ThisWorkbook.Sheets("武將表")
Set r = ws.Range("F12:F121")
Set found = r.Find(What:=cmb.value, LookIn:=xlValues, LookAt:=xlWhole)
If Not found Is Nothing Then
txt.value = ws.Cells(found.Row, "AM").value
Else
txt.value = "未找到對應的值"
End If
End Sub
Private Sub UpdateAssociatedTextBoxes(cmb As MSForms.comboBox, startTextBoxIndex As Integer)
Dim ws As Worksheet
Dim found As Range
Dim r As Range
Dim rowIndex As Long
Dim colIndex As Integer
Dim txt As MSForms.TextBox
Set ws = ThisWorkbook.Sheets("武將表")
Set r = ws.Range("F12:F121")
Set found = r.Find(What:=cmb.value, LookIn:=xlValues, LookAt:=xlWhole)
If Not found Is Nothing Then
rowIndex = found.Row
For colIndex = 0 To 5
Set txt = Me.Controls("TextBox" & startTextBoxIndex + colIndex)
txt.value = ws.Cells(rowIndex, 30 + colIndex).value
Next colIndex
Else
For colIndex = 0 To 5
Set txt = Me.Controls("TextBox" & startTextBoxIndex + colIndex)
txt.value = ""
Next colIndex
End If
End Sub
Private Sub UpdateTextBox67WithComboBox(cmb As MSForms.comboBox, txt As MSForms.TextBox)
Dim ws As Worksheet
Dim r As Range
Dim found As Range
Set ws = ThisWorkbook.Sheets("武將表")
Set r = ws.Range("F12:F121")
Set found = r.Find(What:=cmb.value, LookIn:=xlValues, LookAt:=xlWhole)
If Not found Is Nothing Then
txt.value = ws.Cells(found.Row, "G").value
Else
txt.value = "未找到對應值"
End If
End Sub
Private Sub ComboBox19_Change()
UpdateProficiencyTextBoxes Me.ComboBox19, Me.ComboBox1, Me.ComboBox2, Me.ComboBox3, Me.TextBox49, Me.TextBox50, Me.TextBox51
End Sub
Private Sub ComboBox20_Change()
UpdateProficiencyTextBoxes Me.ComboBox20, Me.ComboBox4, Me.ComboBox5, Me.ComboBox6, Me.TextBox52, Me.TextBox53, Me.TextBox54
End Sub
Private Sub UpdateProficiencyTextBoxes(typeCombo As MSForms.comboBox, warriorCombo1 As MSForms.comboBox, warriorCombo2 As MSForms.comboBox, warriorCombo3 As MSForms.comboBox, txt1 As MSForms.TextBox, txt2 As MSForms.TextBox, txt3 As MSForms.TextBox)
Dim ws As Worksheet
Dim colOffset As Integer
Set ws = ThisWorkbook.Sheets("武將表")
colOffset = Application.WorksheetFunction.Match(typeCombo.value, ws.Range("M11:Q11"), 0) - 1
UpdateTextBoxForWarrior ws, warriorCombo1, colOffset, txt1
UpdateTextBoxForWarrior ws, warriorCombo2, colOffset, txt2
UpdateTextBoxForWarrior ws, warriorCombo3, colOffset, txt3
End Sub
Private Sub UpdateTextBoxForWarrior(ws As Worksheet, warriorCombo As MSForms.comboBox, colOffset As Integer, txt As MSForms.TextBox)
Dim r As Range
Dim found As Range
Set r = ws.Range("F12:F121")
Set found = r.Find(What:=warriorCombo.value, LookIn:=xlValues, LookAt:=xlWhole)
If Not found Is Nothing Then
txt.value = ws.Cells(found.Row, "M").Offset(0, colOffset).value
Else
txt.value = ""
End If
End Sub
Private Sub LimitTextBoxValue(txt As MSForms.TextBox)
Dim inputValue As Integer
inputValue = Val(txt.value)
If inputValue > 5 Then
txt.value = "5"
End If
End Sub
Private Sub UpdateTextBox_55()
Me.TextBox55.value = GetTotal(Me.CheckBox1, Me.CheckBox7, Me.TextBox61)
End Sub
Private Sub UpdateTextBox_56()
Me.TextBox56.value = GetTotal(Me.CheckBox2, Me.CheckBox8, Me.TextBox62)
End Sub
Private Sub UpdateTextBox_57()
Me.TextBox57.value = GetTotal(Me.CheckBox3, Me.CheckBox9, Me.TextBox63)
End Sub
Private Sub UpdateTextBox_58()
Me.TextBox58.value = GetTotal(Me.CheckBox4, Me.CheckBox10, Me.TextBox64)
End Sub
Private Sub UpdateTextBox_59()
Me.TextBox59.value = GetTotal(Me.CheckBox5, Me.CheckBox11, Me.TextBox65)
End Sub
Private Sub UpdateTextBox_60()
Me.TextBox60.value = GetTotal(Me.CheckBox6, Me.CheckBox12, Me.TextBox66)
End Sub
Private Function GetTotal(chk1 As MSForms.CheckBox, chk2 As MSForms.CheckBox, txtSrc As MSForms.TextBox) As String
Dim total As Integer
total = 0
If chk1.value = True Then total = total + 10
If chk2.value = True Then total = total + 10
total = total + (Val(txtSrc.value) * 10)
GetTotal = CStr(total)
End Function
Private Sub TextBox61_Change(): LimitTextBoxValue Me.TextBox61: UpdateTextBox_55: End Sub
Private Sub CheckBox1_Click(): UpdateTextBox_55: End Sub
Private Sub CheckBox7_Click(): UpdateTextBox_55: End Sub
Private Sub TextBox62_Change(): LimitTextBoxValue Me.TextBox62: UpdateTextBox_56: End Sub
Private Sub CheckBox2_Click(): UpdateTextBox_56: End Sub
Private Sub CheckBox8_Click(): UpdateTextBox_56: End Sub
Private Sub TextBox63_Change(): LimitTextBoxValue Me.TextBox63: UpdateTextBox_57: End Sub
Private Sub CheckBox3_Click(): UpdateTextBox_57: End Sub
Private Sub CheckBox9_Click(): UpdateTextBox_57: End Sub
Private Sub TextBox64_Change(): LimitTextBoxValue Me.TextBox64: UpdateTextBox_58: End Sub
Private Sub CheckBox4_Click(): UpdateTextBox_58: End Sub
Private Sub CheckBox10_Click(): UpdateTextBox_58: End Sub
Private Sub TextBox65_Change(): LimitTextBoxValue Me.TextBox65: UpdateTextBox_59: End Sub
Private Sub CheckBox5_Click(): UpdateTextBox_59: End Sub
Private Sub CheckBox11_Click(): UpdateTextBox_59: End Sub
Private Sub TextBox66_Change(): LimitTextBoxValue Me.TextBox66: UpdateTextBox_60: End Sub
Private Sub CheckBox6_Click(): UpdateTextBox_60: End Sub
Private Sub CheckBox12_Click(): UpdateTextBox_60: End Sub
Private Sub CommandButton1_Click()
' 檢查 ComboBox19 和 ComboBox20 是否為空
If Me.ComboBox19.value = "" Then
MsgBox "兵種請勿空白(我方)", vbExclamation, "輸入錯誤"
Exit Sub
End If
If Me.ComboBox20.value = "" Then
MsgBox "兵種請勿空白(敵方)", vbExclamation, "輸入錯誤"
Exit Sub
End If
' 初始化我方和敵方武將的集合
Dim allUnits As Collection
Set allUnits = New Collection
' 初始化武將及其屬性,並從 TextBox 獲取兵力
AddUnit allUnits, "主將", ComboBox1, 13, TextBox7, TextBox49
AddUnit allUnits, "副將", ComboBox2, 19, TextBox8, TextBox50
AddUnit allUnits, "副將", ComboBox3, 25, TextBox9, TextBox51
AddUnit allUnits, "主將", ComboBox4, 31, TextBox10, TextBox52
AddUnit allUnits, "副將", ComboBox5, 37, TextBox11, TextBox53
AddUnit allUnits, "副將", ComboBox6, 43, TextBox12, TextBox54
' 開始模擬回合
SimulateRounds allUnits
End Sub
Private Sub CommandButton2_Click()
' 若使用 Label 來顯示而非 ListBox
Me.TextBox109.Text = ""
End Sub
Private Function GetProficiencyFactor(proficiency As String) As Double
Select Case proficiency
Case "S"
GetProficiencyFactor = 1.2
Case "A"
GetProficiencyFactor = 1
Case "B"
GetProficiencyFactor = 0.85
Case "C"
GetProficiencyFactor = 0.7
Case Else
GetProficiencyFactor = 1
End Select
End Function
Private Sub AddUnit(units As Collection, name As String, cmb As MSForms.comboBox, startIndex As Integer, forceTextBox As MSForms.TextBox, proficiencyTextBox As MSForms.TextBox)
If cmb.value <> "" Then
Dim unit As Object
Dim proficiencyFactor As Double
Dim comboIndex As Integer
Set unit = CreateObject("Scripting.Dictionary")
' 設置兵種適性
unit.Add "proficiency", proficiencyTextBox.value
' 預先計算適性調整因子
Select Case proficiencyTextBox.value
Case "S"
proficiencyFactor = 1.2
Case "A"
proficiencyFactor = 1#
Case "B"
proficiencyFactor = 0.85
Case "C"
proficiencyFactor = 0.7
Case Else
proficiencyFactor = 1#
End Select
unit.Add "name", name
unit.Add "startIndex", startIndex ' 添加開始索引以方便屬性查找
unit.Add "speed", Val(Me.Controls("TextBox" & startIndex + 3).value) * proficiencyFactor
unit.Add "force", Val(Me.Controls("TextBox" & startIndex).value) * proficiencyFactor
unit.Add "warriorName", cmb.value
unit.Add "forces", Val(forceTextBox.value)
' 根據 ComboBox 名稱判斷 index
comboIndex = CInt(Mid(cmb.name, Len("ComboBox") + 1))
' 添加兵種
If comboIndex >= 1 And comboIndex <= 3 Then
unit.Add "兵種", Me.Controls("ComboBox19").value
ElseIf comboIndex >= 4 And comboIndex <= 6 Then
unit.Add "兵種", Me.Controls("ComboBox20").value
End If
unit.Add "兵種適性", proficiencyTextBox.value
units.Add unit
End If
End Sub
Private Function SortUnitsBySpeed(units As Collection) As Collection
Dim i As Integer, j As Integer
Dim unit As Object
Dim sortedUnits As New Collection
For i = 1 To units.Count
Set unit = units(i)
If sortedUnits.Count = 0 Then
sortedUnits.Add unit
Else
For j = 1 To sortedUnits.Count
If unit("speed") > sortedUnits(j)("speed") Then
sortedUnits.Add unit, , j
Exit For
End If
Next j
If j > sortedUnits.Count Then sortedUnits.Add unit
End If
Next i
Set SortUnitsBySpeed = sortedUnits
End Function
Private Sub SimulateRounds(units As Collection)
Dim attacker As Object, target As Object
Dim i As Integer, j As Integer
Dim damage As Double
Dim targetIndex As Integer
Dim team1Forces As Double, team2Forces As Double
Dim displayText As String
' 初始化顯示文本並重新安排顯示順序
displayText = "開始模擬" & vbCrLf
displayText = displayText & "準備回合:" & vbCrLf
displayText = displayText & vbCrLf & "雙方屬性數值:" & vbCrLf
For i = 1 To units.Count
Dim unit As Object
Set unit = units(i)
Dim proficiencyFactor As Double
proficiencyFactor = GetProficiencyFactor(unit("proficiency"))
displayText = displayText & unit("name") & " (" & unit("warriorName") & ") 的兵種適性為" & unit("proficiency") & ",調整後屬性:" & _
"武力: " & Format(Val(Me.Controls("TextBox" & unit("startIndex")).value) * proficiencyFactor, "0.0") & ", " & _
"智力: " & Format(Val(Me.Controls("TextBox" & (unit("startIndex") + 1)).value) * proficiencyFactor, "0.0") & ", " & _
"統率: " & Format(Val(Me.Controls("TextBox" & (unit("startIndex") + 2)).value) * proficiencyFactor, "0.0") & ", " & _
"速度: " & Format(Val(Me.Controls("TextBox" & (unit("startIndex") + 3)).value) * proficiencyFactor, "0.0") & ", " & _
"政治: " & Format(Val(Me.Controls("TextBox" & (unit("startIndex") + 4)).value) * proficiencyFactor, "0.0") & ", " & _
"魅力: " & Format(Val(Me.Controls("TextBox" & (unit("startIndex") + 5)).value) * proficiencyFactor, "0.0") & vbCrLf
Next i
' 開始回合模擬
For i = 1 To 8 ' 限制到最多八回合
displayText = displayText & vbCrLf & "第 " & i & " 回合:" & vbCrLf
' 初始化隊伍力量
team1Forces = 0
team2Forces = 0
For j = 1 To units.Count
Set attacker = units(j)
' 忽略已陣亡的角色
If attacker("forces") <= 0 Then
displayText = displayText & attacker("warriorName") & " 已陣亡" & vbCrLf
Else
' 更新隊伍剩餘兵力
If j <= 3 Then
team1Forces = team1Forces + attacker("forces")
Else
team2Forces = team2Forces + attacker("forces")
End If
' 判斷主將陣亡
If (j = 1 Or j = 4) And attacker("forces") <= 0 Then
displayText = displayText & "主將陣亡,隊伍無法繼續戰鬥" & vbCrLf
Exit For
End If
' 試圖選擇一個有效的目標
Do
targetIndex = Application.WorksheetFunction.RandBetween(1, units.Count)
Set target = units(targetIndex)
Loop While (targetIndex = j Or SameTeam(j, targetIndex) Or target("forces") <= 0)
' 計算傷害
damage = CalculateDamage(attacker, target)
' 更新目標的剩餘兵力
Dim remainingForces As Double
remainingForces = target("forces") - damage
If remainingForces < 0 Then
remainingForces = 0
End If
' 更新字典中的兵力
target("forces") = remainingForces
' A 開始行動和攻擊描述
displayText = displayText & attacker("warriorName") & " 對 " & target("warriorName") & " 發動普通攻擊," & _
target("warriorName") & " 損失了兵力 " & damage & ",剩餘 " & remainingForces & " 兵力。" & vbCrLf
End If
Next j
' 添加分隔線
displayText = displayText & String(150, "-") & vbCrLf
' 停止模擬,如果任一主將被擊敗
If units(1)("forces") <= 0 Or units(4)("forces") <= 0 Then
Exit For
End If
Next i
' 確定勝利者
If team1Forces > team2Forces Then
displayText = displayText & "我方取得勝利!" & vbCrLf
ElseIf team1Forces < team2Forces Then
displayText = displayText & "敵方取得勝利!" & vbCrLf
Else
displayText = displayText & "平局!" & vbCrLf
End If
' 在 TextBox109 中顯示累積的文本
Me.TextBox109.Text = displayText
End Sub
Private Function CalculateDamage(attacker As Object, target As Object) As Double
Dim attack As Double
Dim defense As Double
Dim advantage As Double
Dim proficiencyFactor As Double
Dim Z As Double
Dim S As Double
Dim baseDamage As Double
Dim minimumDamage As Double
attack = attacker("force")
defense = target("force")
proficiencyFactor = GetProficiencyFactor(attacker("proficiency"))
advantage = 1
' 驗證兵種剋制
Select Case attacker("兵種")
Case "騎兵"
If target("兵種") = "盾兵" Or target("兵種") = "器械" Then
advantage = 1.15
End If
Case "盾兵"
If target("兵種") = "弓兵" Or target("兵種") = "器械" Then
advantage = 1.15
End If
Case "弓兵"
If target("兵種") = "槍兵" Or target("兵種") = "器械" Then
advantage = 1.15
End If
Case "槍兵"
If target("兵種") = "騎兵" Or target("兵種") = "器械" Then
advantage = 1.15
End If
End Select
' 計算 Z 和 S
Z = attack * proficiencyFactor * advantage
S = ((Z - defense) / 150 + 1) * (attacker("forces") / 20)
' 判斷是否需要使用傷害公式 2
If S <= 0 Then
baseDamage = GetBaseDamage(attacker("forces"))
minimumDamage = GetMinimumDamage(attacker("forces"))
S = baseDamage + (attack * 1.6 - defense * 1.6) * 0.9 + minimumDamage
If S < 0 Then S = 0 ' 防止負值
End If
' 無條件進位傷害至整數
CalculateDamage = Application.WorksheetFunction.RoundUp(S, 0)
End Function
Private Function GetBaseDamage(forces As Double) As Double
Select Case forces
Case Is <= 1000: GetBaseDamage = 88
Case Is <= 2000: GetBaseDamage = 176
Case Is <= 3000: GetBaseDamage = 232
Case Is <= 4000: GetBaseDamage = 276
Case Is <= 5000: GetBaseDamage = 314
Case Is <= 6000: GetBaseDamage = 334
Case Is <= 7000: GetBaseDamage = 350
Case Is <= 8000: GetBaseDamage = 364
Case Is <= 9000: GetBaseDamage = 376
Case Else: GetBaseDamage = 387
End Select
End Function
Private Function GetMinimumDamage(forces As Double) As Double
Select Case forces
Case Is <= 1000: GetMinimumDamage = 20
Case Is <= 2000: GetMinimumDamage = 40
Case Is <= 3000: GetMinimumDamage = 53
Case Is <= 4000: GetMinimumDamage = 64
Case Is <= 5000: GetMinimumDamage = 73
Case Is <= 6000: GetMinimumDamage = 77
Case Is <= 7000: GetMinimumDamage = 81
Case Is <= 8000: GetMinimumDamage = 84
Case Is <= 9000: GetMinimumDamage = 87
Case Else: GetMinimumDamage = 90
End Select
End Function
Private Function SameTeam(index1 As Integer, index2 As Integer) As Boolean
' 判斷兩個索引是否在相同隊伍
SameTeam = (index1 <= 3 And index2 <= 3) Or (index1 > 3 And index2 > 3)
End Function
' 第一組 CommandButton 和 TextBox 的事件處理
Private Sub CommandButton3_Click()
UpdateValues Me.TextBox55, Me.TextBox13, Me.TextBox73
End Sub
Private Sub CommandButton4_Click()
UpdateValues Me.TextBox55, Me.TextBox14, Me.TextBox74
End Sub
Private Sub CommandButton5_Click()
UpdateValues Me.TextBox55, Me.TextBox15, Me.TextBox75
End Sub
Private Sub CommandButton6_Click()
UpdateValues Me.TextBox55, Me.TextBox16, Me.TextBox76
End Sub
Private Sub CommandButton7_Click()
UpdateValues Me.TextBox55, Me.TextBox17, Me.TextBox77
End Sub
Private Sub CommandButton8_Click()
UpdateValues Me.TextBox55, Me.TextBox18, Me.TextBox78
End Sub
' 第二組 CommandButton 和 TextBox 的事件
Private Sub CommandButton9_Click()
UpdateValues Me.TextBox56, Me.TextBox19, Me.TextBox79
End Sub
Private Sub CommandButton10_Click()
UpdateValues Me.TextBox56, Me.TextBox20, Me.TextBox80
End Sub
Private Sub CommandButton11_Click()
UpdateValues Me.TextBox56, Me.TextBox21, Me.TextBox81
End Sub
Private Sub CommandButton12_Click()
UpdateValues Me.TextBox56, Me.TextBox22, Me.TextBox82
End Sub
Private Sub CommandButton13_Click()
UpdateValues Me.TextBox56, Me.TextBox23, Me.TextBox83
End Sub
Private Sub CommandButton14_Click()
UpdateValues Me.TextBox56, Me.TextBox24, Me.TextBox84
End Sub
' 第三組 CommandButton 和 TextBox 的事件
Private Sub CommandButton15_Click()
UpdateValues Me.TextBox57, Me.TextBox25, Me.TextBox85
End Sub
Private Sub CommandButton16_Click()
UpdateValues Me.TextBox57, Me.TextBox26, Me.TextBox86
End Sub
Private Sub CommandButton17_Click()
UpdateValues Me.TextBox57, Me.TextBox27, Me.TextBox87
End Sub
Private Sub CommandButton18_Click()
UpdateValues Me.TextBox57, Me.TextBox28, Me.TextBox88
End Sub
Private Sub CommandButton19_Click()
UpdateValues Me.TextBox57, Me.TextBox29, Me.TextBox89
End Sub
Private Sub CommandButton20_Click()
UpdateValues Me.TextBox57, Me.TextBox30, Me.TextBox90
End Sub
' 第四組 CommandButton 和 TextBox 的事件
Private Sub CommandButton21_Click()
UpdateValues Me.TextBox58, Me.TextBox31, Me.TextBox91
End Sub
Private Sub CommandButton22_Click()
UpdateValues Me.TextBox58, Me.TextBox32, Me.TextBox92
End Sub
Private Sub CommandButton23_Click()
UpdateValues Me.TextBox58, Me.TextBox33, Me.TextBox93
End Sub
Private Sub CommandButton24_Click()
UpdateValues Me.TextBox58, Me.TextBox34, Me.TextBox94
End Sub
Private Sub CommandButton25_Click()
UpdateValues Me.TextBox58, Me.TextBox35, Me.TextBox95
End Sub
Private Sub CommandButton26_Click()
UpdateValues Me.TextBox58, Me.TextBox36, Me.TextBox96
End Sub
' 第五組 CommandButton 和 TextBox 的事件
Private Sub CommandButton27_Click()
UpdateValues Me.TextBox59, Me.TextBox37, Me.TextBox97
End Sub
Private Sub CommandButton28_Click()
UpdateValues Me.TextBox59, Me.TextBox38, Me.TextBox98
End Sub
Private Sub CommandButton29_Click()
UpdateValues Me.TextBox59, Me.TextBox39, Me.TextBox99
End Sub
Private Sub CommandButton30_Click()
UpdateValues Me.TextBox59, Me.TextBox40, Me.TextBox100
End Sub
Private Sub CommandButton31_Click()
UpdateValues Me.TextBox59, Me.TextBox41, Me.TextBox101
End Sub
Private Sub CommandButton32_Click()
UpdateValues Me.TextBox59, Me.TextBox42, Me.TextBox102
End Sub
' 第六組 CommandButton 和 TextBox 的事件
Private Sub CommandButton33_Click()
UpdateValues Me.TextBox60, Me.TextBox43, Me.TextBox103
End Sub
Private Sub CommandButton34_Click()
UpdateValues Me.TextBox60, Me.TextBox44, Me.TextBox104
End Sub
Private Sub CommandButton35_Click()
UpdateValues Me.TextBox60, Me.TextBox45, Me.TextBox105
End Sub
Private Sub CommandButton36_Click()
UpdateValues Me.TextBox60, Me.TextBox46, Me.TextBox106
End Sub
Private Sub CommandButton37_Click()
UpdateValues Me.TextBox60, Me.TextBox47, Me.TextBox107
End Sub
Private Sub CommandButton38_Click()
UpdateValues Me.TextBox60, Me.TextBox48, Me.TextBox108
End Sub
' 更新邏輯的核心子程序
Private Sub UpdateValues(sourceTextBox As MSForms.TextBox, incrementTextBox As MSForms.TextBox, counterTextBox As MSForms.TextBox)
Dim currentValue As Integer
Dim currentIncrement As Double
Dim currentCount As Integer
' 確認數值有效
currentValue = SafeVal(sourceTextBox)
currentIncrement = SafeValDouble(incrementTextBox)
currentCount = SafeVal(counterTextBox)
' 確保 Textbox 值大於零才能操作
If currentValue > 0 Then
' 減少來源 TextBox 的值
sourceTextBox.value = currentValue - 1
' 增加到相應 TextBox 並保留一位小數
currentIncrement = currentIncrement + 1
incrementTextBox.value = Format(currentIncrement, "0.0")
' 增至計數 TextBox
counterTextBox.value = currentCount + 1
End If
End Sub
Private Function SafeVal(txt As MSForms.TextBox) As Integer
On Error Resume Next
SafeVal = Val(txt.value)
On Error GoTo 0
End Function
Private Function SafeValDouble(txt As MSForms.TextBox) As Double
On Error Resume Next
SafeValDouble = CDbl(txt.value)
On Error GoTo 0
End Function
我的意思是
把下面這個
Private Sub UserForm_Initialize()
Dim rng As Range
Set rng = ThisWorkbook.Sheets("武將表").Range("F12:F121")
' 初始化 ComboBox1 到 ComboBox6
InitComboBox Me.ComboBox1, rng
InitComboBox Me.ComboBox2, rng
InitComboBox Me.ComboBox3, rng
InitComboBox Me.ComboBox4, rng
InitComboBox Me.ComboBox5, rng
InitComboBox Me.ComboBox6, rng
' 初始化 ComboBox7 到 ComboBox18,使用不同範圍
Set rng = ThisWorkbook.Sheets("武將表").Range("AM122:AM234")
InitComboBox Me.ComboBox7, rng
InitComboBox Me.ComboBox8, rng
InitComboBox Me.ComboBox9, rng
InitComboBox Me.ComboBox10, rng
InitComboBox Me.ComboBox11, rng
InitComboBox Me.ComboBox12, rng
InitComboBox Me.ComboBox13, rng
InitComboBox Me.ComboBox14, rng
InitComboBox Me.ComboBox15, rng
InitComboBox Me.ComboBox16, rng
InitComboBox Me.ComboBox17, rng
InitComboBox Me.ComboBox18, rng
' 初始化 TextBox 的值為空白
ClearTextBox 7, 12
End Sub
Private Sub InitComboBox(cmb As MSForms.ComboBox, rng As Range)
Dim cell As Range
cmb.Clear
For Each cell In rng
If Not IsEmpty(cell.Value) Then
cmb.AddItem cell.Value
End If
Next cell
End Sub
Private Sub ClearTextBox(startIndex As Integer, endIndex As Integer)
Dim i As Integer
For i = startIndex To endIndex
Me.Controls("TextBox" & i).Value = ""
Next i
End Sub
Private Sub ComboBox7_Change()
UpdateComboBoxesOnChange Array(Me.ComboBox7, Me.ComboBox8, Me.ComboBox9, Me.ComboBox13, Me.ComboBox14, Me.ComboBox15), Array(Me.TextBox1, Me.TextBox2, Me.TextBox3)
End Sub
Private Sub ComboBox8_Change()
UpdateComboBoxesOnChange Array(Me.ComboBox7, Me.ComboBox8, Me.ComboBox9, Me.ComboBox13, Me.ComboBox14, Me.ComboBox15), Array(Me.TextBox1, Me.TextBox2, Me.TextBox3)
End Sub
Private Sub ComboBox9_Change()
UpdateComboBoxesOnChange Array(Me.ComboBox7, Me.ComboBox8, Me.ComboBox9, Me.ComboBox13, Me.ComboBox14, Me.ComboBox15), Array(Me.TextBox1, Me.TextBox2, Me.TextBox3)
End Sub
Private Sub ComboBox10_Change()
UpdateComboBoxesOnChange Array(Me.ComboBox10, Me.ComboBox11, Me.ComboBox12, Me.ComboBox16, Me.ComboBox17, Me.ComboBox18), Array(Me.TextBox4, Me.TextBox5, Me.TextBox6)
End Sub
Private Sub ComboBox11_Change()
UpdateComboBoxesOnChange Array(Me.ComboBox10, Me.ComboBox11, Me.ComboBox12, Me.ComboBox16, Me.ComboBox17, Me.ComboBox18), Array(Me.TextBox4, Me.TextBox5, Me.TextBox6)
End Sub
Private Sub ComboBox12_Change()
UpdateComboBoxesOnChange Array(Me.ComboBox10, Me.ComboBox11, Me.ComboBox12, Me.ComboBox16, Me.ComboBox17, Me.ComboBox18), Array(Me.TextBox4, Me.TextBox5, Me.TextBox6)
End Sub
Private Sub ComboBox13_Change()
UpdateComboBoxesOnChange Array(Me.ComboBox7, Me.ComboBox8, Me.ComboBox9, Me.ComboBox13, Me.ComboBox14, Me.ComboBox15), Array(Me.TextBox1, Me.TextBox2, Me.TextBox3)
End Sub
Private Sub ComboBox14_Change()
UpdateComboBoxesOnChange Array(Me.ComboBox7, Me.ComboBox8, Me.ComboBox9, Me.ComboBox13, Me.ComboBox14, Me.ComboBox15), Array(Me.TextBox1, Me.TextBox2, Me.TextBox3)
End Sub
Private Sub ComboBox15_Change()
UpdateComboBoxesOnChange Array(Me.ComboBox7, Me.ComboBox8, Me.ComboBox9, Me.ComboBox13, Me.ComboBox14, Me.ComboBox15), Array(Me.TextBox1, Me.TextBox2, Me.TextBox3)
End Sub
Private Sub ComboBox16_Change()
UpdateComboBoxesOnChange Array(Me.ComboBox10, Me.ComboBox11, Me.ComboBox12, Me.ComboBox16, Me.ComboBox17, Me.ComboBox18), Array(Me.TextBox4, Me.TextBox5, Me.TextBox6)
End Sub
Private Sub ComboBox17_Change()
UpdateComboBoxesOnChange Array(Me.ComboBox10, Me.ComboBox11, Me.ComboBox12, Me.ComboBox16, Me.ComboBox17, Me.ComboBox18), Array(Me.TextBox4, Me.TextBox5, Me.TextBox6)
End Sub
Private Sub ComboBox18_Change()
UpdateComboBoxesOnChange Array(Me.ComboBox10, Me.ComboBox11, Me.ComboBox12, Me.ComboBox16, Me.ComboBox17, Me.ComboBox18), Array(Me.TextBox4, Me.TextBox5, Me.TextBox6)
End Sub
Private Sub UpdateComboBoxesOnChange(comboGroup As Variant, textBoxGroup As Variant)
Dim allValues As Collection
Set allValues = New Collection
Dim rng As Range
Set rng = ThisWorkbook.Sheets("武將表").Range("AM122:AM234")
' 收集所有已選擇的值
Dim i As Integer
On Error Resume Next
For i = LBound(comboGroup) To UBound(comboGroup)
If TypeName(comboGroup(i)) = "ComboBox" And comboGroup(i).Value <> "" Then allValues.Add comboGroup(i).Value, CStr(comboGroup(i).Value)
Next i
For i = LBound(textBoxGroup) To UBound(textBoxGroup)
If TypeName(textBoxGroup(i)) = "TextBox" And textBoxGroup(i).Value <> "" Then allValues.Add textBoxGroup(i).Value, CStr(textBoxGroup(i).Value)
Next i
On Error GoTo 0
' 更新 ComboBox 列表
Dim cmb As MSForms.ComboBox
For i = LBound(comboGroup) To UBound(comboGroup)
Set cmb = comboGroup(i)
Dim currentValue As String
currentValue = cmb.Value
InitComboBox cmb, rng
' 移除集合中的選擇值
Dim ctrl As Variant
For Each ctrl In allValues
Dim listIndex As Integer
For listIndex = cmb.ListCount - 1 To 0 Step -1
If cmb.List(listIndex) = ctrl Then
cmb.RemoveItem listIndex
End If
Next listIndex
Next ctrl
' 檢查并保留有效的當前值
Dim isValidValue As Boolean
isValidValue = False
For listIndex = 0 To cmb.ListCount - 1
If cmb.List(listIndex) = currentValue Then
isValidValue = True
Exit For
End If
Next listIndex
If isValidValue Then
cmb.Value = currentValue
Else
cmb.Value = ""
End If
Next i
End Sub
加上去