請使用繁體中文
- User
請使用繁體中文
- Assistant
好的!有什麼我可以幫助您的嗎?
- User
Private isUpdating As Boolean
Private Sub UserForm_Initialize() ' 初始化隨機數生成器 Randomize
Dim rng As Range ' 初始化 ComboBox1 到 ComboBox6 的資料 Set rng = ThisWorkbook.Sheets("武將表").Range("F12:F121") 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 ' 初始化兵種ComboBox InitTypeComboBox Me.ComboBox19, ThisWorkbook.Sheets("武將表").Range("M11:Q11") InitTypeComboBox Me.ComboBox20, ThisWorkbook.Sheets("武將表").Range("M11:Q11") ' 初始化特定範圍 TextBox 的值為空白 ClearTextBox 7, 12 ' 更新按鈕狀態 UpdateButtonStatesEnd 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 ComboBox7_Change() UpdateComboBoxesForSubset Array(Me.ComboBox7, Me.ComboBox8, Me.ComboBox9, Me.ComboBox13, Me.ComboBox14, Me.ComboBox15) End Sub
Private Sub ComboBox8_Change() UpdateComboBoxesForSubset Array(Me.ComboBox7, Me.ComboBox8, Me.ComboBox9, Me.ComboBox13, Me.ComboBox14, Me.ComboBox15) End Sub
Private Sub ComboBox9_Change() UpdateComboBoxesForSubset Array(Me.ComboBox7, Me.ComboBox8, Me.ComboBox9, Me.ComboBox13, Me.ComboBox14, Me.ComboBox15) End Sub
Private Sub ComboBox10_Change() UpdateComboBoxesForSubset Array(Me.ComboBox10, Me.ComboBox11, Me.ComboBox12, Me.ComboBox16, Me.ComboBox17, Me.ComboBox18) End Sub
Private Sub ComboBox11_Change() UpdateComboBoxesForSubset Array(Me.ComboBox10, Me.ComboBox11, Me.ComboBox12, Me.ComboBox16, Me.ComboBox17, Me.ComboBox18) End Sub
Private Sub ComboBox12_Change() UpdateComboBoxesForSubset Array(Me.ComboBox10, Me.ComboBox11, Me.ComboBox12, Me.ComboBox16, Me.ComboBox17, Me.ComboBox18) End Sub
Private Sub ComboBox13_Change() UpdateComboBoxesForSubset Array(Me.ComboBox7, Me.ComboBox8, Me.ComboBox9, Me.ComboBox13, Me.ComboBox14, Me.ComboBox15) End Sub
Private Sub ComboBox14_Change() UpdateComboBoxesForSubset Array(Me.ComboBox7, Me.ComboBox8, Me.ComboBox9, Me.ComboBox13, Me.ComboBox14, Me.ComboBox15) End Sub
Private Sub ComboBox15_Change() UpdateComboBoxesForSubset Array(Me.ComboBox7, Me.ComboBox8, Me.ComboBox9, Me.ComboBox13, Me.ComboBox14, Me.ComboBox15) End Sub
Private Sub ComboBox16_Change() UpdateComboBoxesForSubset Array(Me.ComboBox10, Me.ComboBox11, Me.ComboBox12, Me.ComboBox16, Me.ComboBox17, Me.ComboBox18) End Sub
Private Sub ComboBox17_Change() UpdateComboBoxesForSubset Array(Me.ComboBox10, Me.ComboBox11, Me.ComboBox12, Me.ComboBox16, Me.ComboBox17, Me.ComboBox18) End Sub
Private Sub ComboBox18_Change() UpdateComboBoxesForSubset Array(Me.ComboBox10, Me.ComboBox11, Me.ComboBox12, Me.ComboBox16, Me.ComboBox17, Me.ComboBox18) End Sub
Private Sub UpdateComboBoxesForSubset(comboBoxes As Variant) If isUpdating Then Exit Sub isUpdating = True
Dim i As Integer, j As Integer Dim cmb As MSForms.comboBox Dim currentValue As String Dim selectedValues As New Collection ' 收集所有被選擇的值 On Error Resume Next For i = LBound(comboBoxes) To UBound(comboBoxes) Set cmb = comboBoxes(i) If cmb.value <> "" Then selectedValues.Add cmb.value, CStr(cmb.value) End If Next i On Error GoTo 0 ' 更新每個 ComboBox 列表, 移除已選擇的值 For i = LBound(comboBoxes) To UBound(comboBoxes) Set cmb = comboBoxes(i) currentValue = cmb.value InitComboBox cmb, ThisWorkbook.Sheets("武將表").Range("AM122:AM234") ' 移除已選擇的值 For j = cmb.ListCount - 1 To 0 Step -1 If IsValueInCollection(selectedValues, cmb.list(j)) And cmb.list(j) <> currentValue Then cmb.RemoveItem j End If Next j cmb.value = currentValue Next i isUpdating = FalseEnd Sub
Private Function IsValueInCollection(coll As Collection, value As String) As Boolean Dim item As Variant IsValueInCollection = False On Error Resume Next For Each item In coll If item = value Then IsValueInCollection = True Exit Function End If Next item On Error GoTo 0 End Function
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 = FalseEnd 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 IfEnd 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 IfEnd 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 IfEnd 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("武將表") ' 使用 Application.Match 來避免錯誤 1004 Dim matchResult As Variant matchResult = Application.Match(typeCombo.value, ws.Range("M11:Q11"), 0) If IsError(matchResult) Then MsgBox "不能找到匹配的值。請檢查 ComboBox 的輸入值是否正確。", vbExclamation colOffset = 0 ' 設置默認值或相應處理 Else colOffset = matchResult - 1 End If UpdateTextBoxForWarrior ws, warriorCombo1, colOffset, txt1 UpdateTextBoxForWarrior ws, warriorCombo2, colOffset, txt2 UpdateTextBoxForWarrior ws, warriorCombo3, colOffset, txt3End 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 IfEnd Sub
Private Sub LimitTextBoxValue(txt As MSForms.TextBox) Dim inputValue As Integer inputValue = Val(txt.value)
If inputValue > 5 Then txt.value = "5" End IfEnd 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 ' 清空 ListBox1 Me.ListBox1.Clear ' 初始化武將及其屬性 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 ' 計算初始兵力總和 Dim initialTeam1Forces As Double Dim initialTeam2Forces As Double initialTeam1Forces = Val(TextBox7.value) + Val(TextBox8.value) + Val(TextBox9.value) initialTeam2Forces = Val(TextBox10.value) + Val(TextBox11.value) + Val(TextBox12.value) ' 開始模擬回合 SimulateRounds allUnits, initialTeam1Forces, initialTeam2Forces ' 更新 ListBox1 顯示 TextBox109 的內容 UpdateListBoxFromTextBoxEnd Sub
Private Sub UpdateListBoxFromTextBox() Dim content As String Dim lines() As String Dim i As Integer Dim paragraphEmpty As Boolean
' 取得 TextBox109 的所有內容 content = Me.TextBox109.Text ' 按行分隔內容 lines = Split(content, vbCrLf) ' 清除 ListBox1 的內容 Me.ListBox1.Clear ' 初始段落空行為 True paragraphEmpty = True ' 逐行添加到 ListBox1 中 For i = LBound(lines) To UBound(lines) Dim trimmedLine As String trimmedLine = Trim(lines(i)) If trimmedLine = "" Then ' 只在非段落開頭和非空段落後插入空白行 If Not paragraphEmpty Then Me.ListBox1.AddItem "" paragraphEmpty = True End If Else Me.ListBox1.AddItem trimmedLine paragraphEmpty = False End If Next iEnd Sub
Private Sub CommandButton2_Click() ' 清除 TextBox109 的內容 Me.TextBox109.Text = ""
' 清除 Label26 和 Label27 的內容 Me.Label26.Caption = "" Me.Label27.Caption = "" ' 清除 ListBox1 的內容 Me.ListBox1.ClearEnd 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 ' 添加戰法信息 Dim selfSkillName As String selfSkillName = Me.Controls("TextBox" & comboIndex).Text unit.Add "selfSkillName", selfSkillName unit.Add "selfSkillProbability", GetWarSkillProbability(selfSkillName) unit.Add "selfSkillType", GetWarSkillType(selfSkillName) ' 添加額外戰法信息 Dim additionalSkillName As String additionalSkillName = Me.Controls("ComboBox" & (comboIndex + 6)).value unit.Add "additionalSkillName", additionalSkillName unit.Add "additionalSkillProbability", GetWarSkillProbability(additionalSkillName) unit.Add "additionalSkillType", GetWarSkillType(additionalSkillName) ' 為第三組 ComboBox 初始化額外額外戰法信息 Dim extraSkillName As String extraSkillName = Me.Controls("ComboBox" & (comboIndex + 12)).value unit.Add "extraSkillName", extraSkillName unit.Add "extraSkillProbability", GetWarSkillProbability(extraSkillName) unit.Add "extraSkillType", GetWarSkillType(extraSkillName) ' 添加單位到集合 units.Add unit End IfEnd 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 = sortedUnitsEnd Function
Private Sub SimulateRounds(units As Collection, initialTeam1Forces As Double, initialTeam2Forces As Double) Dim attacker As Object, target As Object Dim speedSortedUnits As Collection Dim i As Integer Dim targetIndex As Integer Dim team1Forces As Double, team2Forces As Double Dim displayText As String
' 初始化顯示文本並重新排列顯示順序 displayText = "準備回合" & vbCrLf & vbCrLf & "雙方屬性數值:" & vbCrLf For i = 1 To units.Count Dim unit As Object Set unit = units(i) Dim proficiencyFactor As Double proficiencyFactor = GetProficiencyFactor(unit("proficiency")) Dim teamName As String ' 判斷是我方還是敵方 If unit("startIndex") <= 25 Then teamName = "我方" Else teamName = "敵方" End If displayText = displayText & "(" & teamName & ") " & 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 & vbCrLf Next i ' 準備回合:觸發指揮、兵種、陣營類型的戰法 For Each attacker In units If attacker("forces") > 0 Then TriggerPreparationSkills attacker, displayText End If Next attacker ' 開始模擬 For i = 1 To 8 ' 限制到最多八回合 displayText = displayText & vbCrLf & "第 " & i & " 回合:" & vbCrLf ' 按速度排序單元 Set speedSortedUnits = SortUnitsBySpeed(units) ' 初始化隊伍力量 team1Forces = 0 team2Forces = 0 For Each attacker In speedSortedUnits ' 忽略已陣亡的角色 If attacker("forces") > 0 Then Dim attackerTeam As String If attacker("startIndex") <= 25 Then attackerTeam = "我方" team1Forces = team1Forces + attacker("forces") Else attackerTeam = "敵方" team2Forces = team2Forces + attacker("forces") End If displayText = displayText & "(" & attackerTeam & ") " & attacker("warriorName") & " 開始行動" & vbCrLf ' 試圖選擇一個有效的目標 Do targetIndex = Application.WorksheetFunction.RandBetween(1, speedSortedUnits.Count) Set target = speedSortedUnits(targetIndex) Loop While (SameTeam(attacker("startIndex"), target("startIndex")) Or target("forces") <= 0) ' 依次發動 主動 → 普通攻擊 → 突擊 → 被動 TryCombatSkills attacker, attackerTeam, target, displayText ' 為簡易閱讀增加換行段落 displayText = displayText & vbCrLf End If Next attacker ' 添加分隔線 displayText = displayText & String(143, "-") & vbCrLf ' 停止模擬,如果任一主將被擊敗 If units(1)("forces") <= 0 Or units(4)("forces") <= 0 Then Exit For End If Next i ' 更新 Label26 和 Label27 來顯示剩餘/初始兵力 Me.Label26.Caption = team1Forces & "/" & initialTeam1Forces Me.Label27.Caption = team2Forces & "/" & initialTeam2Forces Me.TextBox109.Text = displayText ' 確定勝利者並設置字體顏色 If team1Forces > team2Forces Then Me.Label28.Caption = "勝" Me.Label28.ForeColor = RGB(255, 255, 0) ' 黃色 Me.Label29.Caption = "敗" Me.Label29.ForeColor = RGB(255, 0, 0) ' 紅色 ElseIf team1Forces < team2Forces Then Me.Label28.Caption = "敗" Me.Label28.ForeColor = RGB(255, 0, 0) ' 紅色 Me.Label29.Caption = "勝" Me.Label29.ForeColor = RGB(255, 255, 0) ' 黃色 Else Me.Label28.Caption = "平" Me.Label28.ForeColor = RGB(255, 255, 255) ' 白色 Me.Label29.Caption = "平" Me.Label29.ForeColor = RGB(255, 255, 255) ' 白色 End IfEnd Sub
Private Sub TryCombatSkills(attacker As Object, team As String, target As Object, ByRef displayText As String) Dim attackOrder As Variant attackOrder = Array("主動", "普通", "突擊", "被動")
Dim allSkills As Collection Set allSkills = New Collection allSkills.Add Array(attacker("selfSkillName"), attacker("selfSkillProbability"), attacker("selfSkillType")) allSkills.Add Array(attacker("additionalSkillName"), attacker("additionalSkillProbability"), attacker("additionalSkillType")) allSkills.Add Array(attacker("extraSkillName"), attacker("extraSkillProbability"), attacker("extraSkillType")) Dim skillInfo As Variant Dim rng As Double Dim isTriggered As Boolean ' 設定隊伍名稱 Dim teamName As String If attacker("startIndex") <= 25 Then teamName = "我方" Else teamName = "敵方" End If ' 確認技能順序和普通攻擊順序 For Each Order In attackOrder For Each skillInfo In allSkills If skillInfo(0) <> "" And skillInfo(2) = Order Then rng = Rnd() isTriggered = rng <= skillInfo(1) displayText = displayText & "(" & teamName & ") " & attacker("warriorName") & " 嘗試發動 " & skillInfo(0) & " (" & _ Format(skillInfo(1) * 100, "0.0") & "%) - " & IIf(isTriggered, "成功", "失敗") & "。" & vbCrLf End If Next skillInfo If Order = "普通" Then Dim damage As Double damage = CalculateDamage(attacker, target) ' 更新目標的兵力 target("forces") = target("forces") - damage ' 調整displayText以包含損失與剩餘信息 displayText = displayText & "(" & teamName & ") " & attacker("warriorName") & " 對 " & target("warriorName") & " 發動普通攻擊,造成損失 " & damage & " 兵力。" & _ " (剩餘 " & target("forces") & " 兵力)" & vbCrLfEnd If Next Order End Sub Private Sub TriggerPreparationSkills(attacker As Object, ByRef displayText As String) Dim skillTypes As Variant skillTypes = Array("指揮", "兵種", "陣法")
TriggerSkillsByType attacker, skillTypes, displayText, "準備階段"End Sub
Private Sub TriggerSkillsByType(attacker As Object, types As Variant, ByRef displayText As String, phase As String) Dim skillNames As Variant Dim SkillType As String Dim rng As Double Dim isTriggered As Boolean Dim teamName As String
' Determine if the unit is from team 1 or team 2 If attacker("startIndex") <= 25 Then teamName = "我方" Else teamName = "敵方" End If skillNames = Array(attacker("selfSkillName"), attacker("additionalSkillName"), attacker("extraSkillName")) ' Check skills and trigger them if applicable at the current phase For Each SkillName In skillNames If Not IsNull(SkillName) And SkillName <> "" Then ' Ensure skill names are not empty or null SkillType = GetWarSkillType(CStr(SkillName)) If IsInList(SkillType, types) Then rng = Rnd() isTriggered = rng <= GetWarSkillProbability(CStr(SkillName)) If isTriggered Then displayText = displayText & "(" & teamName & ") " & attacker("warriorName") & " 發動 " & SkillName & " (" _ & Format(GetWarSkillProbability(CStr(SkillName)) * 100, "0.0") & "%) - 成功" & vbCrLf Else displayText = displayText & "(" & teamName & ") " & attacker("warriorName") & " 嘗試發動 " & SkillName & " (" _ & Format(GetWarSkillProbability(CStr(SkillName)) * 100, "0.0") & "%) - 失敗" & vbCrLf End If End If End If Next SkillNameEnd Sub
Private Function IsInList(value As String, list As Variant) As Boolean Dim i As Integer For i = LBound(list) To UBound(list) If list(i) = value Then IsInList = True Exit Function End If Next i IsInList = False End Function
Private Function GetWarSkillProbability(SkillName As String) As Double Dim ws As Worksheet Dim r As Range Dim found As Range
Set ws = ThisWorkbook.Sheets("武將表") Set r = ws.Range("AM12:AM234") ' 假設技能名稱信息位於這個範圍 Set found = r.Find(What:=Trim(SkillName), LookIn:=xlValues, LookAt:=xlWhole) If Not found Is Nothing Then GetWarSkillProbability = ws.Cells(found.Row, "AO").value Else GetWarSkillProbability = 0 End IfEnd Function
Private Function GetWarSkillType(SkillName As String) As String Dim ws As Worksheet Dim r As Range Dim found As Range
Set ws = ThisWorkbook.Sheets("武將表") Set r = ws.Range("AM12:AM234") ' 假設技能名稱信息位於這個範圍 Set found = r.Find(What:=Trim(SkillName), LookIn:=xlValues, LookAt:=xlWhole) If Not found Is Nothing Then GetWarSkillType = ws.Cells(found.Row, "AN").value Else GetWarSkillType = "" End IfEnd Function
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 <= 25 And index2 <= 25) Or (index1 > 25 And index2 > 25) 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 ' 更新按鈕狀態 UpdateButtonStatesEnd Sub
Private Sub CommandButton39_Click() ShowRoundContent "準備回合" End Sub
Private Sub CommandButton40_Click() ShowRoundContent "第 1 回合" End Sub
Private Sub CommandButton41_Click() ShowRoundContent "第 2 回合" End Sub
Private Sub CommandButton42_Click() ShowRoundContent "第 3 回合" End Sub
Private Sub CommandButton43_Click() ShowRoundContent "第 4 回合" End Sub
Private Sub CommandButton44_Click() ShowRoundContent "第 5 回合" End Sub
Private Sub CommandButton45_Click() ShowRoundContent "第 6 回合" End Sub
Private Sub CommandButton46_Click() ShowRoundContent "第 7 回合" End Sub
Private Sub CommandButton47_Click() ShowRoundContent "第 8 回合" End Sub
Private Sub UpdateButtonStates() ' 檢查 TextBox55 到 TextBox60 的值,並相應地啟用或禁用按鈕 Me.CommandButton3.Enabled = (Val(Me.TextBox55.value) > 0) Me.CommandButton4.Enabled = (Val(Me.TextBox55.value) > 0) Me.CommandButton5.Enabled = (Val(Me.TextBox55.value) > 0) Me.CommandButton6.Enabled = (Val(Me.TextBox55.value) > 0) Me.CommandButton7.Enabled = (Val(Me.TextBox55.value) > 0) Me.CommandButton8.Enabled = (Val(Me.TextBox55.value) > 0)
Me.CommandButton9.Enabled = (Val(Me.TextBox56.value) > 0) Me.CommandButton10.Enabled = (Val(Me.TextBox56.value) > 0) Me.CommandButton11.Enabled = (Val(Me.TextBox56.value) > 0) Me.CommandButton12.Enabled = (Val(Me.TextBox56.value) > 0) Me.CommandButton13.Enabled = (Val(Me.TextBox56.value) > 0) Me.CommandButton14.Enabled = (Val(Me.TextBox56.value) > 0) Me.CommandButton15.Enabled = (Val(Me.TextBox57.value) > 0) Me.CommandButton16.Enabled = (Val(Me.TextBox57.value) > 0) Me.CommandButton17.Enabled = (Val(Me.TextBox57.value) > 0) Me.CommandButton18.Enabled = (Val(Me.TextBox57.value) > 0) Me.CommandButton19.Enabled = (Val(Me.TextBox57.value) > 0) Me.CommandButton20.Enabled = (Val(Me.TextBox57.value) > 0) Me.CommandButton21.Enabled = (Val(Me.TextBox58.value) > 0) Me.CommandButton22.Enabled = (Val(Me.TextBox58.value) > 0) Me.CommandButton23.Enabled = (Val(Me.TextBox58.value) > 0) Me.CommandButton24.Enabled = (Val(Me.TextBox58.value) > 0) Me.CommandButton25.Enabled = (Val(Me.TextBox58.value) > 0) Me.CommandButton26.Enabled = (Val(Me.TextBox58.value) > 0) Me.CommandButton27.Enabled = (Val(Me.TextBox59.value) > 0) Me.CommandButton28.Enabled = (Val(Me.TextBox59.value) > 0) Me.CommandButton29.Enabled = (Val(Me.TextBox59.value) > 0) Me.CommandButton30.Enabled = (Val(Me.TextBox59.value) > 0) Me.CommandButton31.Enabled = (Val(Me.TextBox59.value) > 0) Me.CommandButton32.Enabled = (Val(Me.TextBox59.value) > 0) Me.CommandButton33.Enabled = (Val(Me.TextBox60.value) > 0) Me.CommandButton34.Enabled = (Val(Me.TextBox60.value) > 0) Me.CommandButton35.Enabled = (Val(Me.TextBox60.value) > 0) Me.CommandButton36.Enabled = (Val(Me.TextBox60.value) > 0) Me.CommandButton37.Enabled = (Val(Me.TextBox60.value) > 0) Me.CommandButton38.Enabled = (Val(Me.TextBox60.value) > 0)End Sub
Private Sub TextBox55_Change() If Val(Me.TextBox55.value) < 0 Then Me.TextBox55.value = 0 UpdateButtonStates End Sub
Private Sub TextBox56_Change() If Val(Me.TextBox56.value) < 0 Then Me.TextBox56.value = 0 UpdateButtonStates End Sub
Private Sub TextBox57_Change() If Val(Me.TextBox57.value) < 0 Then Me.TextBox57.value = 0 UpdateButtonStates End Sub
Private Sub TextBox58_Change() If Val(Me.TextBox58.value) < 0 Then Me.TextBox58.value = 0 UpdateButtonStates End Sub
Private Sub TextBox59_Change() If Val(Me.TextBox59.value) < 0 Then Me.TextBox59.value = 0 UpdateButtonStates End Sub
Private Sub TextBox60_Change() If Val(Me.TextBox60.value) < 0 Then Me.TextBox60.value = 0 UpdateButtonStates End Sub
Private Function SafeVal(txtBox As MSForms.TextBox) As Integer ' 如果TextBox有合法數字,返回其值,否則返回0 On Error Resume Next SafeVal = Val(txtBox.value) If Err.number <> 0 Then SafeVal = 0 End If On Error GoTo 0 End Function
Private Function SafeValDouble(txtBox As MSForms.TextBox) As Double ' 如果TextBox有合法數字,返回其值,否則返回0.0 On Error Resume Next SafeValDouble = CDbl(txtBox.value) If Err.number <> 0 Then SafeValDouble = 0# End If On Error GoTo 0 End Function
Private Sub ShowRoundContent(roundText As String) Dim position As Long Dim content As String Dim lines() As String Dim i As Integer Dim roundContent As String Dim endOfRound As Boolean endOfRound = False
' 取得TextBox的全部內容 content = Me.TextBox109.Text ' 以行符拆分內容 lines = Split(content, vbCrLf) ' 找到所需回合文本的開始行 For i = LBound(lines) To UBound(lines) If InStr(lines(i), roundText) > 0 Then ' 開始行始,取得之后所有內容 roundContent = lines(i) & vbCrLf i = i + 1 Exit For End If Next i ' 繼續取得?容直到下一?回合或文件的末尾 Do While i <= UBound(lines) And Not endOfRound If Trim(lines(i)) <> "" Then If InStr(lines(i), "回合") > 0 Then endOfRound = True Else roundContent = roundContent & lines(i) & vbCrLf ' 在每個武將動作之間添加一個空行 roundContent = roundContent & vbCrLf End If End If i = i + 1 Loop If roundContent <> "" Then ' 設置ListBox的內容 Me.ListBox1.Clear lines = Split(roundContent, vbCrLf) ' 確保添加空行分隔段落 Dim previousLineWasEmpty As Boolean previousLineWasEmpty = True For i = LBound(lines) To UBound(lines) If Trim(lines(i)) <> "" Then If Not previousLineWasEmpty Then Me.ListBox1.AddItem "" ' 增加一行空白分隔 End If Me.ListBox1.AddItem lines(i) previousLineWasEmpty = False Else previousLineWasEmpty = True End If Next i Else MsgBox roundText & " 未找到", vbExclamation End IfEnd Sub
這使我的程式 我要修改一些功能
- Assistant
這段程式碼是一個用於 VBA(Visual Basic for Applications)的用戶表單程式,主要涉及到 Excel 中的範圍和組件操作,模擬了一個回合制戰鬥系統。看起來這是一個比較複雜的程式,滿包含戰鬥模擬的邏輯。
要對此程式進行修改,您需要對 VBA 和 Excel 中的對象模型有一定理解。這裡是一些常見的修改建議,您可以根據需要進行應用:
-
新增或修改 ComboBox 的選項: 如果要修改或新增 ComboBox 的選項,可以更新
InitComboBox函數中用到的 Excel 範圍。 -
更改戰鬥計算邏輯: 如果要修改戰鬥計算邏輯,關鍵函數是
CalculateDamage和TryCombatSkills。可以根據新的規則調整裡面的計算邏輯。 -
增加新的戰法或技能特性: 需要在
AddUnit方法中增加新的屬性,並確保在處理戰鬥時適當引用這些屬性。 -
改進 UI 元件: 可以透過 VBA 編輯器中的表單設計視圖來視覺化地添加或調整 UI 元件,比如按鈕或標籤。
-
增加訊息框提示: 可以在關鍵事件或錯誤捕獲時使用
MsgBox來提醒用戶輸入不當或有新的通知。
如果你有具體的修改需求,例如需要增加某個特定功能或者修正某些邏輯錯誤,請詳細說明該需求,這樣可以給出更具體的指導。也可以考慮具體的程式邏輯部分,我可以幫助更詳細地解釋。
-
- User
我要把TEXTBOX109的功能改成WebBrowser1 可以嗎 Private Sub UserForm_Initialize() Dim sDefHTML As String sDefHTML = "" sDefHTML = sDefHTML & "" sDefHTML = sDefHTML & "" sDefHTML = sDefHTML & "body {overflow-x:hidden;overflow-y:auto;margin:0;padding:2 5;font-family:'tahoma';font-size:8pt;}" sDefHTML = sDefHTML & "p {margin:0;padding:0;}" sDefHTML = sDefHTML & "" sDefHTML = sDefHTML & "" sDefHTML = sDefHTML & "" sDefHTML = sDefHTML & ""
With WebBrowser1 .Offline = True .Silent = True .RegisterAsBrowser = False .RegisterAsDropTarget = False .MenuBar = False .Toolbar = 0
.Navigate "about:blank" .Document.Write sDefHTML End With 'WebBrowser1
WebBrowser1.Document.Body.innerHTML = "<font color=""red"">Hello <font color=""blue"">world !!!" End Sub
- Assistant
要將
TextBox109的功能改為WebBrowser1,您需要確保所有引用TextBox109的地方都改為使用WebBrowser1,並且按照 HTML 內容的格式進行更新。首先,您已經在
UserForm_Initialize中設定了WebBrowser1的默認 HTML。接下來,您需要確保其他操作也改用WebBrowser1。下面是您需要修改的一些地方:
-
用
WebBrowser1替代TextBox109的地方:假如有任何地方有
Me.TextBox109.Text的用法,您需要將這些地方替換為WebBrowser1.Document.Body.innerHTML。 -
更新
WebBrowser1的內容:當要更改顯示的內容時,把
TextBox109.Text的改動替代如下:WebBrowser1.Document.Body.innerHTML = "您的新的 HTML 內容" -
修改您的
SimulateRounds例子:替換改原來更新
TextBox109.Text的部分:Dim displayText As String displayText = "<p>準備回合</p><p>雙方屬性數值:</p>" ' 在循環或其他地方形成 displayText 的 HTML ' 然後用 WebBrowser1 替換 TextBox109 WebBrowser1.Document.Body.innerHTML = displayText -
搜尋整個 VBA 腳本:
使用 VBA 編輯器的搜尋功能,查找所有
TextBox109(例如TextBox109.Text)的應用,逐一將其使用WebBrowser1的innerHTML屬性進行取代。
這樣,
WebBrowser1會顯示 HTML 格式的內容,並且您可以使用 HTML 和 CSS 來格式化內容,提高顯示效果。-
特殊格式或轉化:
如果之前您用
TextBox109顯示普通文本,則需要將文本格式轉化為 HTML。如果不確定如何轉化,請具體介紹文本格式,我可以更詳細地指導。
-
- User
給我完整的
- Assistant
好的,要替換
TextBox109為WebBrowser1的完整實現方案涉及到將程式中所有與TextBox109相關的代碼替換為WebBrowser1的操作,並確保內容按 HTML 格式顯示。以下是您需要完成的步驟和代碼範例:
-
初始化 WebBrowser: 在
UserForm_Initialize事件中,確保WebBrowser1正確初始化,並設定初始顯示。Private Sub UserForm_Initialize() Dim sDefHTML As String sDefHTML = "<html>" sDefHTML = sDefHTML & "<head>" sDefHTML = sDefHTML & "<style>" sDefHTML = sDefHTML & "body {overflow-x:hidden;overflow-y:auto;margin:0;padding:2 5;font-family:'tahoma';font-size:8pt;}" sDefHTML = sDefHTML & "p {margin:0;padding:0;}" sDefHTML = sDefHTML & "</style>" sDefHTML = sDefHTML & "<head></head>" sDefHTML = sDefHTML & "<body></body>" sDefHTML = sDefHTML & "</html>" With WebBrowser1 .Offline = True .Silent = True .RegisterAsBrowser = False .RegisterAsDropTarget = False .MenuBar = False .Toolbar = 0 .Navigate "about:blank" .Document.Write sDefHTML End With ' WebBrowser1 WebBrowser1.Document.Body.innerHTML = "<b><font color=""red"">Hello</font> <font color=""blue"">world</font> !!!</b>" End Sub -
更新內容的地方:在需要更新內容的位置,例如您的
SimulateRounds方法,使用innerHTML來更新 WebBrowser 的顯示。Private Sub SimulateRounds(units As Collection, initialTeam1Forces As Double, initialTeam2Forces As Double) Dim attacker As Object, target As Object Dim i As Integer Dim targetIndex As Integer Dim team1Forces As Double, team2Forces As Double Dim displayText As String ' 構建 HTML 格式內容 displayText = "<p>準備回合</p><p>雙方屬性數值:</p>" ' 繼續模擬邏輯,並將結果追加到 displayText 中作為 HTML ' For example: For i = 1 To units.Count Dim unit As Object Set unit = units(i) Dim proficiencyFactor As Double proficiencyFactor = GetProficiencyFactor(unit("proficiency")) Dim teamName As String If unit("startIndex") <= 25 Then teamName = "我方" Else teamName = "敵方" End If displayText = displayText & "<p>(" & teamName & ") " & 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") & "</p>" Next i ' 更新 WebBrowser 的內容 WebBrowser1.Document.Body.innerHTML = displayText End Sub -
其他替換:
- 搜尋整段代碼中所有涉及
TextBox109的操作,將其替換成WebBrowser1操作。 - 確保所有的文本內容被轉化成適合的 HTML,如
<p>標籤封裝文本段落,使用innerHTML更新內容。
- 搜尋整段代碼中所有涉及
通過這樣的更改,您可以將所有從文本框的輸出轉換為使用瀏覽器元件來處理,以獲得更豐富的格式化效果。如果 HTML 格式不適用,也可以考慮純文本顯示,請調整 HTML 標籤。根据需要,您可以进一步修改或添加样式。
-
- User
Private Sub UserForm_Initialize() ' 初始化隨機數生成器 Randomize
Dim rng As Range ' 初始化 ComboBox1 到 ComboBox6 的資料 Set rng = ThisWorkbook.Sheets("武將表").Range("F12:F121") 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 ' 初始化兵種ComboBox InitTypeComboBox Me.ComboBox19, ThisWorkbook.Sheets("武將表").Range("M11:Q11") InitTypeComboBox Me.ComboBox20, ThisWorkbook.Sheets("武將表").Range("M11:Q11") ' 初始化特定範圍 TextBox 的值為空白 ClearTextBox 7, 12 ' 更新按鈕狀態 UpdateButtonStatesEnd Sub 我已經有這段程式
- Assistant
看來您已經有初始設置
ComboBox和TextBox的程式碼。我假設您的問題是希望在這段程式中,將一個TextBox轉換成WebBrowser的使用,如果有誤請糾正我。您需要進行以下更改:
-
將
WebBrowser元件加入您的表單中:- 在您的用戶表單中,確保有一個
WebBrowser控件,可通過VBA編輯器的“工具箱”中的“其他控件”來添加。
- 在您的用戶表單中,確保有一個
-
初始化
WebBrowser1:- 您可能已經編寫了一部分代碼來初始化
WebBrowser1。在UserForm_Initialize方法中保持或新增該初始化過程。
- 您可能已經編寫了一部分代碼來初始化
-
將所有涉及
TextBox109的程式碼替換為WebBrowser1:假設
TextBox109是用於顯示一些動態內容的,您可以將相關代碼替換為WebBrowser1的操作。在
UserForm_Initialize中設定初始HTML内容(這部份您似乎已經有一個模版代碼):Private Sub UserForm_Initialize() ' 之前的初始化代碼... ' 初始化 WebBrowser1 Dim sDefHTML As String sDefHTML = "<html><head><style>" sDefHTML = sDefHTML & "body {overflow-x:hidden;overflow-y:auto;margin:0;padding:2 5;font-family:'tahoma';font-size:8pt;}" sDefHTML = sDefHTML & "p {margin:0;padding:0;}" sDefHTML = sDefHTML & "</style></head><body></body></html>" With WebBrowser1 .Offline = True .Silent = True .RegisterAsBrowser = False .RegisterAsDropTarget = False .MenuBar = False .Toolbar = 0 .Navigate "about:blank" .Document.Write sDefHTML End With ' 設定初始顯示 WebBrowser1.Document.Body.innerHTML = "<p>初始狀態...</p>" ' 之前的初始化代碼... End Sub -
更新或顯示內容:
- 在之後的程式碼中,如
SimulateRounds之類的函數,當您需要更新文本框內容的時候,使用WebBrowser1.Document.Body.innerHTML來更新顯示內容。
假設您有一個地方需要展示計算結果,模板代碼如下:
Private Sub DisplayCombatResults(results As String) ' 使用HTML更新WebBrowser1顯示結果 WebBrowser1.Document.Body.innerHTML = results End Sub僅的例子,展示如何準備HTML內容:
Dim htmlContent As String htmlContent = "<p>這是一些結果...</p><p>細節如下:</p>" ' Append more HTML-formatted text as needed DisplayCombatResults(htmlContent) - 在之後的程式碼中,如
這樣做可以將之前通過
TextBox109.Text顯示的內容更換成WebBrowser1,利用 HTML 的方式讓顯示的內容更加豐富和靈活。如果您需要格式化或更改 HTML,直接在字串中修改即可。這樣,您順利地將一個文本顯示控件變成了一個 Web 顯示控件。 -
- User
給我完整的
- Assistant
好的,為了將
TextBox109的功能轉換為WebBrowser1,以下是完整的步驟和代碼:-
確保表單中已經添加
WebBrowser控件確保在您的 VBA 用戶表單中添加了一個
WebBrowser控件,通常可以在VBA編輯器的“工具箱”中的“其他控件”找到。 -
調整
UserForm_Initialize來初始化WebBrowser1在初始化中設定
WebBrowser1,以下是完整的初始化代碼:Private Sub UserForm_Initialize() ' 初始化隨機數生成器 Randomize Dim rng As Range ' 初始化 ComboBox1 到 ComboBox6 的資料 Set rng = ThisWorkbook.Sheets("武將表").Range("F12:F121") 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 ' 初始化兵種ComboBox InitTypeComboBox Me.ComboBox19, ThisWorkbook.Sheets("武將表").Range("M11:Q11") InitTypeComboBox Me.ComboBox20, ThisWorkbook.Sheets("武將表").Range("M11:Q11") ' 初始化特定範圍 TextBox 的值為空白 ClearTextBox 7, 12 ' 初始化 WebBrowser1 Dim sDefHTML As String sDefHTML = "<html><head><style>" sDefHTML = sDefHTML & "body {overflow-x:hidden;overflow-y:auto;margin:0;padding:2 5;font-family:'tahoma';font-size:8pt;}" sDefHTML = sDefHTML & "p {margin:0;padding:0;}" sDefHTML = sDefHTML & "</style></head><body></body></html>" With WebBrowser1 .Offline = True .Silent = True .RegisterAsBrowser = False .RegisterAsDropTarget = False .MenuBar = False .Toolbar = 0 .Navigate "about:blank" .Document.Write sDefHTML End With ' 初始顯示 WebBrowser1.Document.Body.innerHTML = "<p>初始狀態...</p>" ' 更新按鈕狀態 UpdateButtonStates End Sub -
使用
WebBrowser1顯示內容在您之前使用
TextBox109做輸出顯示的地方,現在改成使用WebBrowser1。假設您有一個函數要輸出結果,在內部使用類似以下方法:Private Sub DisplayCombatResults(resultHtml As String) ' 更新 WebBrowser1 的內容 WebBrowser1.Document.Body.innerHTML = resultHtml End Sub當您需要更新顯示,如在
SimulateRounds函數中:Private Sub SimulateRounds() ' 一些計算後形成HTML內容 Dim displayText As String displayText = "<p>這是模擬的結果:</p>" displayText = displayText & "<p>具體的數據和狀態在這裡顯示...</p>" ' 呼叫 DisplayCombatResults 顯示計算結果 DisplayCombatResults(displayText) End Sub
請根據需要在程式中其他需要顯示的地方使用此顯示方法來確保所有需要的內容都能通過
WebBrowser1正確展示。而WebBrowser1中可以直接使用 HTML 的標記來進行高度的文本格式化,以改進顯示效果。 -
- User
Private Sub SimulateRounds(units As Collection, initialTeam1Forces As Double, initialTeam2Forces As Double)
給我完整的
- Assistant
要提供
SimulateRounds函數的完整代碼,這個函數的目標是模擬多輪的戰鬥過程,同時將結果展示在WebBrowser1控制項中。以下是一個範例實現,包含了一些常見的邏輯和顯示更新。Private Sub SimulateRounds(units As Collection, initialTeam1Forces As Double, initialTeam2Forces As Double) Dim attacker As Object, target As Object Dim speedSortedUnits As Collection Dim i As Integer, roundNumber As Integer Dim targetIndex As Integer Dim team1Forces As Double, team2Forces As Double Dim displayText As String Dim rng As Double Dim htmlContent As String ' 初始化顯示文本 htmlContent = "<h2>模擬戰鬥回合</h2>" ' 計算準備階段的消息顯示 htmlContent = htmlContent & "<p>準備階段:</p><ul>" ' 初始化計算每個單元的屬性 For i = 1 To units.Count Set attacker = units(i) htmlContent = htmlContent & "<li>" & attacker("warriorName") & " 參戰,兵力: " & attacker("forces") & "</li>" Next i htmlContent = htmlContent & "</ul>" ' 設定最大回合數 Dim maxRounds As Integer maxRounds = 8 ' 開始模擬回合 For roundNumber = 1 To maxRounds htmlContent = htmlContent & "<h3>第 " & roundNumber & " 回合</h3>" ' 检查部队是否仍然存在 team1Forces = 0 team2Forces = 0 For Each attacker In units If attacker("forces") > 0 Then If attacker("startIndex") <= 25 Then team1Forces = team1Forces + attacker("forces") Else team2Forces = team2Forces + attacker("forces") End If End If Next attacker ' 如果一方已經全部陣亡,結束 If team1Forces = 0 Or team2Forces = 0 Then Exit For End If ' 按速度排序單元 Set speedSortedUnits = SortUnitsBySpeed(units) ' 回合內每個角色行動 For Each attacker In speedSortedUnits If attacker("forces") > 0 Then ' 隨機選擇一個有效目標(不是同隊的,且仍有兵力) Do targetIndex = Application.WorksheetFunction.RandBetween(1, speedSortedUnits.Count) Set target = speedSortedUnits(targetIndex) Loop While (SameTeam(attacker("startIndex"), target("startIndex")) Or target("forces") <= 0) ' 計算傷害和更新目標狀態 Dim damage As Double damage = CalculateDamage(attacker, target) target("forces") = target("forces") - damage ' 結果顯示 htmlContent = htmlContent & "<p>" & attacker("warriorName") & " 攻擊 " & target("warriorName") & _ ",造成 " & damage & " 點傷害 (剩餘兵力: " & target("forces") & ")。</p>" End If Next attacker ' 使後續的回合有一個分隔 htmlContent = htmlContent & "<hr>" Next roundNumber ' 確定最終結果 htmlContent = htmlContent & "<h3>戰鬥結束</h3>" If team1Forces > team2Forces Then htmlContent = htmlContent & "<p><b>我方勝利!</b></p>" ElseIf team1Forces < team2Forces Then htmlContent = htmlContent & "<p><b>敵方勝利!</b></p>" Else htmlContent = htmlContent & "<p><b>雙方平手!</b></p>" End If ' 使用 WebBrowser1 顯示結果 WebBrowser1.Document.Body.innerHTML = htmlContent End Sub代碼說明:
- 初始化:首先設置顯示內容的模版和初始化一些變量。
- 回合循環:模擬設定的回合數,如果某一方完全陣亡則提前結束。
- 單元行動:每個單元選擇敵對目標並計算傷害,同時更新顯示文本 HTML。
- 顯示結果:最終的戰鬥結果顯示在
WebBrowser1中,以 HTML 格式呈現。
請確保所有涉及的方法,如
SortUnitsBySpeed、CalculateDamage、SameTeam等,都在相應位置正確實現和使用。如果有進一步需求或需要修改的邏輯,請告知。 - User
然後ListBox1功能也要幫我改
- Assistant
當您希望將
TextBox109和ListBox1的功能移植到WebBrowser1,實現更豐富的格式化顯示時,需要替換掉原來使用ListBox1的地方,將其內容顯示在WebBrowser1中。以下是一些步驟和建議:主要思路
-
初始化
WebBrowser1: 在UserForm_Initialize中已經完成初始化,確保了WebBrowser1可以顯示 HTML 頁面。 -
替換
ListBox1:將所有向ListBox1添加內容的操作替換為對WebBrowser1的 HTML 操作。 -
格式化顯示:運用
HTML標記來格式化需要顯示的信息,使之更加美觀且易讀。
更新與顯示數據
這裡提供一個範例,這裡假設之前是用
ListBox1來顯示戰鬥過程或結果的各種細節:更新與顯示框架:
Private Sub UpdateWebBrowserContent(content As String) ' 將內容作為 HTML 更新到 WebBrowser1 WebBrowser1.Document.Body.innerHTML = content End SubSimulateRounds的更新在
SimulateRounds方法內部,本來是更新TextBox109和ListBox1,我們需要確保這些更新都重定向至WebBrowser1:Private Sub SimulateRounds(units As Collection, initialTeam1Forces As Double, initialTeam2Forces As Double) Dim attacker As Object, target As Object Dim speedSortedUnits As Collection Dim i As Integer, roundNumber As Integer Dim targetIndex As Integer Dim team1Forces As Double, team2Forces As Double Dim htmlContent As String ' 初始化顯示文本 htmlContent = "<h2>模擬戰鬥回合</h2>" ' 倒計時顯示 htmlContent = htmlContent & "<p>準備回合:</p><ul>" For i = 1 To units.Count Set attacker = units(i) htmlContent = htmlContent & "<li>" & attacker("warriorName") & " 參戰,兵力: " & attacker("forces") & "</li>" Next i htmlContent = htmlContent & "</ul>" Dim maxRounds As Integer maxRounds = 8 For roundNumber = 1 To maxRounds htmlContent = htmlContent & "<h3>第 " & roundNumber & " 回合</h3>" team1Forces = 0 team2Forces = 0 For Each attacker In units If attacker("forces") > 0 Then If attacker("startIndex") <= 25 Then team1Forces = team1Forces + attacker("forces") Else team2Forces = team2Forces + attacker("forces") End If End If Next attacker If team1Forces = 0 Or team2Forces = 0 Then Exit For End If Set speedSortedUnits = SortUnitsBySpeed(units) For Each attacker In speedSortedUnits If attacker("forces") > 0 Then Do targetIndex = Application.WorksheetFunction.RandBetween(1, speedSortedUnits.Count) Set target = speedSortedUnits(targetIndex) Loop While (SameTeam(attacker("startIndex"), target("startIndex")) Or target("forces") <= 0) Dim damage As Double damage = CalculateDamage(attacker, target) target("forces") = target("forces") - damage ' 添加戰鬥步驟到 htmlContent htmlContent = htmlContent & "<p>" & attacker("warriorName") & " 攻擊 " & target("warriorName") & _ ",造成 " & damage & " 點傷害 (剩餘兵力: " & target("forces") & ")。</p>" End If Next attacker htmlContent = htmlContent & "<hr>" Next roundNumber htmlContent = htmlContent & "<h3>戰鬥結束</h3>" If team1Forces > team2Forces Then htmlContent = htmlContent & "<p><b>我方勝利!</b></p>" ElseIf team1Forces < team2Forces Then htmlContent = htmlContent & "<p><b>敵方勝利!</b></p>" Else htmlContent = htmlContent & "<p><b>雙方平手!</b></p>" End If ' 最後更新到 WebBrowser1 UpdateWebBrowserContent(htmlContent) End Sub步驟總結
-
架構改變:從之前基於
ListBox的顯示轉向使用 HTML 將原本的文字格式化,在WebBrowser1控件中顯示。 -
內容更新:確保所有需要顯示的信息都已整合進
htmlContent,然後通過WebBrowser1.Document.Body.innerHTML這種方式渲染輸出。
如果有進一步改進或其他具體功能整合的需求,請隨時告知以便調整。
-
- User
重新來LISTBOX改成WebBrowser2
- Assistant
好的,當您希望將
ListBox1的功能轉換為WebBrowser2時,您需要確保所有原本添加到ListBox1的內容都改為使用WebBrowser2顯示。以下是將
ListBox1視作顯示列表內容的部分替換成WebBrowser2方法的完整實現:WebBrowser2 初始化
首先,在
UserForm_Initialize方法中,請確保正確初始化WebBrowser2,以便它可以顯示 HTML 內容:Private Sub UserForm_Initialize() ' 初始化隨機數生成器 Randomize Dim rng As Range ' 初始化 ComboBox1 到 ComboBox6 的資料 Set rng = ThisWorkbook.Sheets("武將表").Range("F12:F121") 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 ' 初始化兵種 ComboBox InitTypeComboBox Me.ComboBox19, ThisWorkbook.Sheets("武將表").Range("M11:Q11") InitTypeComboBox Me.ComboBox20, ThisWorkbook.Sheets("武將表").Range("M11:Q11") ' 初始化特定範圍 TextBox 的值為空白 ClearTextBox 7, 12 ' 初始化 WebBrowser1 Dim sDefHTML1 As String sDefHTML1 = "<html><head><style>" & _ "body {overflow-x:hidden;overflow-y:auto;margin:0;padding:2 5;font-family:'tahoma';font-size:8pt;}" & _ "p {margin:0;padding:0;}" & _ "</style></head><body></body></html>" With WebBrowser1 .Offline = True .Silent = True .Navigate "about:blank" .Document.Write sDefHTML1 End With WebBrowser1.Document.Body.innerHTML = "<p>初始狀態...</p>" ' 初始化 WebBrowser2 Dim sDefHTML2 As String sDefHTML2 = "<html><head><style>" & _ "body {overflow-x:hidden;overflow-y:auto;margin:0;padding:2 5;font-family:'tahoma';font-size:8pt;}" & _ "p {margin:0;padding:0;}" & _ "</style></head><body></body></html>" With WebBrowser2 .Offline = True .Silent = True .Navigate "about:blank" .Document.Write sDefHTML2 End With ' 更新按鈕狀態 UpdateButtonStates End Sub使用 WebBrowser2 來顯示列表內容
假設您之前是用
ListBox1來顯示一些文本行,您可以用以下方法來替換為WebBrowser2顯示:Private Sub UpdateWebBrowser2Content(content As String) ' 將 HTML 內容寫入 WebBrowser2 以顯示 WebBrowser2.Document.Body.innerHTML = content End Sub當需要展示某些信息的時候,原本添加到
ListBox1的邏輯替換為:Private Sub DisplaySomething() ' 構建要顯示的HTML內容 Dim htmlContent As String htmlContent = "<ul>" ' 假設從某個資料來源獲得一些項目 Dim i As Integer For i = 1 To 10 htmlContent = htmlContent & "<li>項目 " & i & "</li>" Next i htmlContent = htmlContent & "</ul>" ' 更新 WebBrowser2 內容 UpdateWebBrowser2Content(htmlContent) End Sub主要邏輯
- 轉換邏輯: 將需顯示的列表內容由位於
ListBox1的AddItem替換為用 HTML 格式化好插入WebBrowser2。
網站效果讓內容更易讀,也讓格式更美觀。這樣您可以更靈活地使用 HTML 及 CSS,來控制顯示樣式。此流程會將
ListBox1的所有功能用 HTML 給移植到WebBrowser2來顯示。 - 轉換邏輯: 將需顯示的列表內容由位於
- User
Private isUpdating As Boolean
Private Sub UserForm_Initialize() ' 初始化隨機數生成器 Randomize
Dim rng As Range ' 初始化 ComboBox1 到 ComboBox6 的資料 Set rng = ThisWorkbook.Sheets("武將表").Range("F12:F121") 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 ' 初始化兵種 ComboBox InitTypeComboBox Me.ComboBox19, ThisWorkbook.Sheets("武將表").Range("M11:Q11") InitTypeComboBox Me.ComboBox20, ThisWorkbook.Sheets("武將表").Range("M11:Q11") ' 初始化特定範圍 TextBox 的值為空白 ClearTextBox 7, 12 ' 初始化 WebBrowser1 Dim sDefHTML1 As String sDefHTML1 = "<html><head><style>" & _ "body {overflow-x:hidden;overflow-y:auto;margin:0;padding:2 5;font-family:'tahoma';font-size:8pt;}" & _ "p {margin:0;padding:0;}" & _ "</style></head><body></body></html>" With WebBrowser1 .Offline = True .Silent = True .Navigate "about:blank" .Document.Write sDefHTML1 End With WebBrowser1.Document.Body.innerHTML = "<p>初始狀態...</p>" ' 初始化 WebBrowser2 Dim sDefHTML2 As String sDefHTML2 = "<html><head><style>" & _ "body {overflow-x:hidden;overflow-y:auto;margin:0;padding:2 5;font-family:'tahoma';font-size:8pt;}" & _ "p {margin:0;padding:0;}" & _ "</style></head><body></body></html>" With WebBrowser2 .Offline = True .Silent = True .Navigate "about:blank" .Document.Write sDefHTML2 End With ' 更新按鈕狀態 UpdateButtonStatesEnd Sub
Private Sub UpdateWebBrowser2Content(content As String) ' 將 HTML 內容寫入 WebBrowser2 以顯示 WebBrowser2.Document.Body.innerHTML = content End Sub
Private Sub DisplayCombatResults(resultHtml As String) ' 更新 WebBrowser1 的內容 WebBrowser1.Document.Body.innerHTML = resultHtml End Sub
Private Sub DisplaySomething() ' 構建要顯示的HTML內容 Dim htmlContent As String htmlContent = ""
' 假設從某個資料來源獲得一些項目 Dim i As Integer For i = 1 To 10 htmlContent = htmlContent & "<li>項目 " & i & "</li>" Next i htmlContent = htmlContent & "</ul>" ' 更新 WebBrowser2 內容 UpdateWebBrowser2Content (htmlContent)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 ComboBox7_Change() UpdateComboBoxesForSubset Array(Me.ComboBox7, Me.ComboBox8, Me.ComboBox9, Me.ComboBox13, Me.ComboBox14, Me.ComboBox15) End Sub
Private Sub ComboBox8_Change() UpdateComboBoxesForSubset Array(Me.ComboBox7, Me.ComboBox8, Me.ComboBox9, Me.ComboBox13, Me.ComboBox14, Me.ComboBox15) End Sub
Private Sub ComboBox9_Change() UpdateComboBoxesForSubset Array(Me.ComboBox7, Me.ComboBox8, Me.ComboBox9, Me.ComboBox13, Me.ComboBox14, Me.ComboBox15) End Sub
Private Sub ComboBox10_Change() UpdateComboBoxesForSubset Array(Me.ComboBox10, Me.ComboBox11, Me.ComboBox12, Me.ComboBox16, Me.ComboBox17, Me.ComboBox18) End Sub
Private Sub ComboBox11_Change() UpdateComboBoxesForSubset Array(Me.ComboBox10, Me.ComboBox11, Me.ComboBox12, Me.ComboBox16, Me.ComboBox17, Me.ComboBox18) End Sub
Private Sub ComboBox12_Change() UpdateComboBoxesForSubset Array(Me.ComboBox10, Me.ComboBox11, Me.ComboBox12, Me.ComboBox16, Me.ComboBox17, Me.ComboBox18) End Sub
Private Sub ComboBox13_Change() UpdateComboBoxesForSubset Array(Me.ComboBox7, Me.ComboBox8, Me.ComboBox9, Me.ComboBox13, Me.ComboBox14, Me.ComboBox15) End Sub
Private Sub ComboBox14_Change() UpdateComboBoxesForSubset Array(Me.ComboBox7, Me.ComboBox8, Me.ComboBox9, Me.ComboBox13, Me.ComboBox14, Me.ComboBox15) End Sub
Private Sub ComboBox15_Change() UpdateComboBoxesForSubset Array(Me.ComboBox7, Me.ComboBox8, Me.ComboBox9, Me.ComboBox13, Me.ComboBox14, Me.ComboBox15) End Sub
Private Sub ComboBox16_Change() UpdateComboBoxesForSubset Array(Me.ComboBox10, Me.ComboBox11, Me.ComboBox12, Me.ComboBox16, Me.ComboBox17, Me.ComboBox18) End Sub
Private Sub ComboBox17_Change() UpdateComboBoxesForSubset Array(Me.ComboBox10, Me.ComboBox11, Me.ComboBox12, Me.ComboBox16, Me.ComboBox17, Me.ComboBox18) End Sub
Private Sub ComboBox18_Change() UpdateComboBoxesForSubset Array(Me.ComboBox10, Me.ComboBox11, Me.ComboBox12, Me.ComboBox16, Me.ComboBox17, Me.ComboBox18) End Sub
Private Sub UpdateComboBoxesForSubset(comboBoxes As Variant) If isUpdating Then Exit Sub isUpdating = True
Dim i As Integer, j As Integer Dim cmb As MSForms.comboBox Dim currentValue As String Dim selectedValues As New Collection ' 收集所有被選擇的值 On Error Resume Next For i = LBound(comboBoxes) To UBound(comboBoxes) Set cmb = comboBoxes(i) If cmb.value <> "" Then selectedValues.Add cmb.value, CStr(cmb.value) End If Next i On Error GoTo 0 ' 更新每個 ComboBox 列表, 移除已選擇的值 For i = LBound(comboBoxes) To UBound(comboBoxes) Set cmb = comboBoxes(i) currentValue = cmb.value InitComboBox cmb, ThisWorkbook.Sheets("武將表").Range("AM122:AM234") ' 移除已選擇的值 For j = cmb.ListCount - 1 To 0 Step -1 If IsValueInCollection(selectedValues, cmb.list(j)) And cmb.list(j) <> currentValue Then cmb.RemoveItem j End If Next j cmb.value = currentValue Next i isUpdating = FalseEnd Sub
Private Function IsValueInCollection(coll As Collection, value As String) As Boolean Dim item As Variant IsValueInCollection = False On Error Resume Next For Each item In coll If item = value Then IsValueInCollection = True Exit Function End If Next item On Error GoTo 0 End Function
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 = FalseEnd 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 IfEnd 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 IfEnd 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 IfEnd 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("武將表") ' 使用 Application.Match 來避免錯誤 1004 Dim matchResult As Variant matchResult = Application.Match(typeCombo.value, ws.Range("M11:Q11"), 0) If IsError(matchResult) Then MsgBox "不能找到匹配的值。請檢查 ComboBox 的輸入值是否正確。", vbExclamation colOffset = 0 ' 設置默認值或相應處理 Else colOffset = matchResult - 1 End If UpdateTextBoxForWarrior ws, warriorCombo1, colOffset, txt1 UpdateTextBoxForWarrior ws, warriorCombo2, colOffset, txt2 UpdateTextBoxForWarrior ws, warriorCombo3, colOffset, txt3End 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 IfEnd Sub
Private Sub LimitTextBoxValue(txt As MSForms.TextBox) Dim inputValue As Integer inputValue = Val(txt.value)
If inputValue > 5 Then txt.value = "5" End IfEnd 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 ' 清空 ListBox1 Me.ListBox1.Clear ' 初始化武將及其屬性 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 ' 計算初始兵力總和 Dim initialTeam1Forces As Double Dim initialTeam2Forces As Double initialTeam1Forces = Val(TextBox7.value) + Val(TextBox8.value) + Val(TextBox9.value) initialTeam2Forces = Val(TextBox10.value) + Val(TextBox11.value) + Val(TextBox12.value) ' 開始模擬回合 SimulateRounds allUnits, initialTeam1Forces, initialTeam2Forces ' 更新 ListBox1 顯示 TextBox109 的內容 UpdateListBoxFromTextBoxEnd Sub
Private Sub UpdateListBoxFromTextBox() Dim content As String Dim lines() As String Dim i As Integer Dim paragraphEmpty As Boolean
' 取得 TextBox109 的所有內容 content = Me.TextBox109.Text ' 按行分隔內容 lines = Split(content, vbCrLf) ' 清除 ListBox1 的內容 Me.ListBox1.Clear ' 初始段落空行為 True paragraphEmpty = True ' 逐行添加到 ListBox1 中 For i = LBound(lines) To UBound(lines) Dim trimmedLine As String trimmedLine = Trim(lines(i)) If trimmedLine = "" Then ' 只在非段落開頭和非空段落後插入空白行 If Not paragraphEmpty Then Me.ListBox1.AddItem "" paragraphEmpty = True End If Else Me.ListBox1.AddItem trimmedLine paragraphEmpty = False End If Next iEnd Sub
Private Sub CommandButton2_Click() ' 清除 TextBox109 的內容 Me.TextBox109.Text = ""
' 清除 Label26 和 Label27 的內容 Me.Label26.Caption = "" Me.Label27.Caption = "" ' 清除 ListBox1 的內容 Me.ListBox1.ClearEnd 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 ' 添加戰法信息 Dim selfSkillName As String selfSkillName = Me.Controls("TextBox" & comboIndex).Text unit.Add "selfSkillName", selfSkillName unit.Add "selfSkillProbability", GetWarSkillProbability(selfSkillName) unit.Add "selfSkillType", GetWarSkillType(selfSkillName) ' 添加額外戰法信息 Dim additionalSkillName As String additionalSkillName = Me.Controls("ComboBox" & (comboIndex + 6)).value unit.Add "additionalSkillName", additionalSkillName unit.Add "additionalSkillProbability", GetWarSkillProbability(additionalSkillName) unit.Add "additionalSkillType", GetWarSkillType(additionalSkillName) ' 為第三組 ComboBox 初始化額外額外戰法信息 Dim extraSkillName As String extraSkillName = Me.Controls("ComboBox" & (comboIndex + 12)).value unit.Add "extraSkillName", extraSkillName unit.Add "extraSkillProbability", GetWarSkillProbability(extraSkillName) unit.Add "extraSkillType", GetWarSkillType(extraSkillName) ' 添加單位到集合 units.Add unit End IfEnd 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 = sortedUnitsEnd Function
Private Sub SimulateRounds(units As Collection, initialTeam1Forces As Double, initialTeam2Forces As Double) Dim attacker As Object, target As Object Dim speedSortedUnits As Collection Dim i As Integer, roundNumber As Integer Dim targetIndex As Integer Dim team1Forces As Double, team2Forces As Double Dim displayText As String Dim rng As Double Dim htmlContent As String
' 初始化顯示文本 htmlContent = "<h2>模擬戰鬥回合</h2>" ' 計算準備階段的消息顯示 htmlContent = htmlContent & "<p>準備階段:</p><ul>" ' 初始化計算每個單元的屬性 For i = 1 To units.Count Set attacker = units(i) htmlContent = htmlContent & "<li>" & attacker("warriorName") & " 參戰,兵力: " & attacker("forces") & "</li>" Next i htmlContent = htmlContent & "</ul>" ' 設定最大回合數 Dim maxRounds As Integer maxRounds = 8 ' 開始模擬回合 For roundNumber = 1 To maxRounds htmlContent = htmlContent & "<h3>第 " & roundNumber & " 回合</h3>" ' ?查部?是否仍然存在 team1Forces = 0 team2Forces = 0 For Each attacker In units If attacker("forces") > 0 Then If attacker("startIndex") <= 25 Then team1Forces = team1Forces + attacker("forces") Else team2Forces = team2Forces + attacker("forces") End If End If Next attacker ' 如果一方已經全部陣亡,結束 If team1Forces = 0 Or team2Forces = 0 Then Exit For End If ' 按速度排序單元 Set speedSortedUnits = SortUnitsBySpeed(units) ' 回合內每個角色行動 For Each attacker In speedSortedUnits If attacker("forces") > 0 Then ' 隨機選擇一個有效目標(不是同隊的,且仍有兵力) Do targetIndex = Application.WorksheetFunction.RandBetween(1, speedSortedUnits.Count) Set target = speedSortedUnits(targetIndex) Loop While (SameTeam(attacker("startIndex"), target("startIndex")) Or target("forces") <= 0) ' 計算傷害和更新目標狀態 Dim damage As Double damage = CalculateDamage(attacker, target) target("forces") = target("forces") - damage ' 結果顯示 htmlContent = htmlContent & "<p>" & attacker("warriorName") & " 攻擊 " & target("warriorName") & _ ",造成 " & damage & " 點傷害 (剩餘兵力: " & target("forces") & ")。</p>" End If Next attacker ' 使後續的回合有一個分隔 htmlContent = htmlContent & "<hr>" Next roundNumber ' 確定最終結果 htmlContent = htmlContent & "<h3>戰鬥結束</h3>" If team1Forces > team2Forces Then htmlContent = htmlContent & "<p><b>我方勝利!</b></p>" ElseIf team1Forces < team2Forces Then htmlContent = htmlContent & "<p><b>敵方勝利!</b></p>" Else htmlContent = htmlContent & "<p><b>雙方平手!</b></p>" End If ' 使用 WebBrowser1 顯示結果 WebBrowser1.Document.Body.innerHTML = htmlContentEnd Sub
Private Sub TryCombatSkills(attacker As Object, team As String, target As Object, ByRef displayText As String) Dim attackOrder As Variant attackOrder = Array("主動", "普通", "突擊", "被動")
Dim allSkills As Collection Set allSkills = New Collection allSkills.Add Array(attacker("selfSkillName"), attacker("selfSkillProbability"), attacker("selfSkillType")) allSkills.Add Array(attacker("additionalSkillName"), attacker("additionalSkillProbability"), attacker("additionalSkillType")) allSkills.Add Array(attacker("extraSkillName"), attacker("extraSkillProbability"), attacker("extraSkillType")) Dim skillInfo As Variant Dim rng As Double Dim isTriggered As Boolean ' 設定隊伍名稱 Dim teamName As String If attacker("startIndex") <= 25 Then teamName = "我方" Else teamName = "敵方" End If ' 確認技能順序和普通攻擊順序 For Each Order In attackOrder For Each skillInfo In allSkills If skillInfo(0) <> "" And skillInfo(2) = Order Then rng = Rnd() isTriggered = rng <= skillInfo(1) displayText = displayText & "(" & teamName & ") " & attacker("warriorName") & " 嘗試發動 " & skillInfo(0) & " (" & _ Format(skillInfo(1) * 100, "0.0") & "%) - " & IIf(isTriggered, "成功", "失敗") & "。" & vbCrLf End If Next skillInfo If Order = "普通" Then Dim damage As Double damage = CalculateDamage(attacker, target) ' 更新目標的兵力 target("forces") = target("forces") - damage ' 調整displayText以包含損失與剩餘信息 displayText = displayText & "(" & teamName & ") " & attacker("warriorName") & " 對 " & target("warriorName") & " 發動普通攻擊,造成損失 " & damage & " 兵力。" & _ " (剩餘 " & target("forces") & " 兵力)" & vbCrLfEnd If Next Order End Sub Private Sub TriggerPreparationSkills(attacker As Object, ByRef displayText As String) Dim skillTypes As Variant skillTypes = Array("指揮", "兵種", "陣法")
TriggerSkillsByType attacker, skillTypes, displayText, "準備階段"End Sub
Private Sub TriggerSkillsByType(attacker As Object, types As Variant, ByRef displayText As String, phase As String) Dim skillNames As Variant Dim SkillType As String Dim rng As Double Dim isTriggered As Boolean Dim teamName As String
' Determine if the unit is from team 1 or team 2 If attacker("startIndex") <= 25 Then teamName = "我方" Else teamName = "敵方" End If skillNames = Array(attacker("selfSkillName"), attacker("additionalSkillName"), attacker("extraSkillName")) ' Check skills and trigger them if applicable at the current phase For Each SkillName In skillNames If Not IsNull(SkillName) And SkillName <> "" Then ' Ensure skill names are not empty or null SkillType = GetWarSkillType(CStr(SkillName)) If IsInList(SkillType, types) Then rng = Rnd() isTriggered = rng <= GetWarSkillProbability(CStr(SkillName)) If isTriggered Then displayText = displayText & "(" & teamName & ") " & attacker("warriorName") & " 發動 " & SkillName & " (" _ & Format(GetWarSkillProbability(CStr(SkillName)) * 100, "0.0") & "%) - 成功" & vbCrLf Else displayText = displayText & "(" & teamName & ") " & attacker("warriorName") & " 嘗試發動 " & SkillName & " (" _ & Format(GetWarSkillProbability(CStr(SkillName)) * 100, "0.0") & "%) - 失敗" & vbCrLf End If End If End If Next SkillNameEnd Sub
Private Function IsInList(value As String, list As Variant) As Boolean Dim i As Integer For i = LBound(list) To UBound(list) If list(i) = value Then IsInList = True Exit Function End If Next i IsInList = False End Function
Private Function GetWarSkillProbability(SkillName As String) As Double Dim ws As Worksheet Dim r As Range Dim found As Range
Set ws = ThisWorkbook.Sheets("武將表") Set r = ws.Range("AM12:AM234") ' 假設技能名稱信息位於這個範圍 Set found = r.Find(What:=Trim(SkillName), LookIn:=xlValues, LookAt:=xlWhole) If Not found Is Nothing Then GetWarSkillProbability = ws.Cells(found.Row, "AO").value Else GetWarSkillProbability = 0 End IfEnd Function
Private Function GetWarSkillType(SkillName As String) As String Dim ws As Worksheet Dim r As Range Dim found As Range
Set ws = ThisWorkbook.Sheets("武將表") Set r = ws.Range("AM12:AM234") ' 假設技能名稱信息位於這個範圍 Set found = r.Find(What:=Trim(SkillName), LookIn:=xlValues, LookAt:=xlWhole) If Not found Is Nothing Then GetWarSkillType = ws.Cells(found.Row, "AN").value Else GetWarSkillType = "" End IfEnd Function
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 <= 25 And index2 <= 25) Or (index1 > 25 And index2 > 25) 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 ' 更新按鈕狀態 UpdateButtonStatesEnd Sub
Private Sub CommandButton39_Click() ShowRoundContent "準備回合" End Sub
Private Sub CommandButton40_Click() ShowRoundContent "第 1 回合" End Sub
Private Sub CommandButton41_Click() ShowRoundContent "第 2 回合" End Sub
Private Sub CommandButton42_Click() ShowRoundContent "第 3 回合" End Sub
Private Sub CommandButton43_Click() ShowRoundContent "第 4 回合" End Sub
Private Sub CommandButton44_Click() ShowRoundContent "第 5 回合" End Sub
Private Sub CommandButton45_Click() ShowRoundContent "第 6 回合" End Sub
Private Sub CommandButton46_Click() ShowRoundContent "第 7 回合" End Sub
Private Sub CommandButton47_Click() ShowRoundContent "第 8 回合" End Sub
Private Sub UpdateButtonStates() ' 檢查 TextBox55 到 TextBox60 的值,並相應地啟用或禁用按鈕 Me.CommandButton3.Enabled = (Val(Me.TextBox55.value) > 0) Me.CommandButton4.Enabled = (Val(Me.TextBox55.value) > 0) Me.CommandButton5.Enabled = (Val(Me.TextBox55.value) > 0) Me.CommandButton6.Enabled = (Val(Me.TextBox55.value) > 0) Me.CommandButton7.Enabled = (Val(Me.TextBox55.value) > 0) Me.CommandButton8.Enabled = (Val(Me.TextBox55.value) > 0)
Me.CommandButton9.Enabled = (Val(Me.TextBox56.value) > 0) Me.CommandButton10.Enabled = (Val(Me.TextBox56.value) > 0) Me.CommandButton11.Enabled = (Val(Me.TextBox56.value) > 0) Me.CommandButton12.Enabled = (Val(Me.TextBox56.value) > 0) Me.CommandButton13.Enabled = (Val(Me.TextBox56.value) > 0) Me.CommandButton14.Enabled = (Val(Me.TextBox56.value) > 0) Me.CommandButton15.Enabled = (Val(Me.TextBox57.value) > 0) Me.CommandButton16.Enabled = (Val(Me.TextBox57.value) > 0) Me.CommandButton17.Enabled = (Val(Me.TextBox57.value) > 0) Me.CommandButton18.Enabled = (Val(Me.TextBox57.value) > 0) Me.CommandButton19.Enabled = (Val(Me.TextBox57.value) > 0) Me.CommandButton20.Enabled = (Val(Me.TextBox57.value) > 0) Me.CommandButton21.Enabled = (Val(Me.TextBox58.value) > 0) Me.CommandButton22.Enabled = (Val(Me.TextBox58.value) > 0) Me.CommandButton23.Enabled = (Val(Me.TextBox58.value) > 0) Me.CommandButton24.Enabled = (Val(Me.TextBox58.value) > 0) Me.CommandButton25.Enabled = (Val(Me.TextBox58.value) > 0) Me.CommandButton26.Enabled = (Val(Me.TextBox58.value) > 0) Me.CommandButton27.Enabled = (Val(Me.TextBox59.value) > 0) Me.CommandButton28.Enabled = (Val(Me.TextBox59.value) > 0) Me.CommandButton29.Enabled = (Val(Me.TextBox59.value) > 0) Me.CommandButton30.Enabled = (Val(Me.TextBox59.value) > 0) Me.CommandButton31.Enabled = (Val(Me.TextBox59.value) > 0) Me.CommandButton32.Enabled = (Val(Me.TextBox59.value) > 0) Me.CommandButton33.Enabled = (Val(Me.TextBox60.value) > 0) Me.CommandButton34.Enabled = (Val(Me.TextBox60.value) > 0) Me.CommandButton35.Enabled = (Val(Me.TextBox60.value) > 0) Me.CommandButton36.Enabled = (Val(Me.TextBox60.value) > 0) Me.CommandButton37.Enabled = (Val(Me.TextBox60.value) > 0) Me.CommandButton38.Enabled = (Val(Me.TextBox60.value) > 0)End Sub
Private Sub TextBox55_Change() If Val(Me.TextBox55.value) < 0 Then Me.TextBox55.value = 0 UpdateButtonStates End Sub
Private Sub TextBox56_Change() If Val(Me.TextBox56.value) < 0 Then Me.TextBox56.value = 0 UpdateButtonStates End Sub
Private Sub TextBox57_Change() If Val(Me.TextBox57.value) < 0 Then Me.TextBox57.value = 0 UpdateButtonStates End Sub
Private Sub TextBox58_Change() If Val(Me.TextBox58.value) < 0 Then Me.TextBox58.value = 0 UpdateButtonStates End Sub
Private Sub TextBox59_Change() If Val(Me.TextBox59.value) < 0 Then Me.TextBox59.value = 0 UpdateButtonStates End Sub
Private Sub TextBox60_Change() If Val(Me.TextBox60.value) < 0 Then Me.TextBox60.value = 0 UpdateButtonStates End Sub
Private Function SafeVal(txtBox As MSForms.TextBox) As Integer ' 如果TextBox有合法數字,返回其值,否則返回0 On Error Resume Next SafeVal = Val(txtBox.value) If Err.number <> 0 Then SafeVal = 0 End If On Error GoTo 0 End Function
Private Function SafeValDouble(txtBox As MSForms.TextBox) As Double ' 如果TextBox有合法數字,返回其值,否則返回0.0 On Error Resume Next SafeValDouble = CDbl(txtBox.value) If Err.number <> 0 Then SafeValDouble = 0# End If On Error GoTo 0 End Function
Private Sub ShowRoundContent(roundText As String) Dim position As Long Dim content As String Dim lines() As String Dim i As Integer Dim roundContent As String Dim endOfRound As Boolean endOfRound = False
' 取得TextBox的全部內容 content = Me.TextBox109.Text ' 以行符拆分內容 lines = Split(content, vbCrLf) ' 找到所需回合文本的開始行 For i = LBound(lines) To UBound(lines) If InStr(lines(i), roundText) > 0 Then ' 開始行始,取得之后所有內容 roundContent = lines(i) & vbCrLf i = i + 1 Exit For End If Next i ' 繼續取得?容直到下一?回合或文件的末尾 Do While i <= UBound(lines) And Not endOfRound If Trim(lines(i)) <> "" Then If InStr(lines(i), "回合") > 0 Then endOfRound = True Else roundContent = roundContent & lines(i) & vbCrLf ' 在每個武將動作之間添加一個空行 roundContent = roundContent & vbCrLf End If End If i = i + 1 Loop If roundContent <> "" Then ' 設置ListBox的內容 Me.ListBox1.Clear lines = Split(roundContent, vbCrLf) ' 確保添加空行分隔段落 Dim previousLineWasEmpty As Boolean previousLineWasEmpty = True For i = LBound(lines) To UBound(lines) If Trim(lines(i)) <> "" Then If Not previousLineWasEmpty Then Me.ListBox1.AddItem "" ' 增加一行空白分隔 End If Me.ListBox1.AddItem lines(i) previousLineWasEmpty = False Else previousLineWasEmpty = True End If Next i Else MsgBox roundText & " 未找到", vbExclamation End IfEnd Sub
幫我檢查,跟LISTBOX1及TEXTBOX109有關的都改成WebBrowser1 WebBrowser2
- Assistant
好的,讓我們一起將
ListBox1和TextBox109中的相關功能轉換為WebBrowser1和WebBrowser2。我們將所有涉及ListBox1和TextBox109的部分轉換為使用這兩個WebBrowser控件來顯示內容。大致思路
- 替換
ListBox1和TextBox109的引用:- 將所有的
ListBox1和TextBox109的使用轉換為WebBrowser1和WebBrowser2。 - 使用 HTML 設計結果顯示內容,並通過
WebBrowser的Document.Body.innerHTML屬性來更新內容顯示。
- 將所有的
更新後的部分程式碼
下面是應用部分更改後的程式碼,將原來的文本框和列表框的顯示邏輯替換為
WebBrowser的顯示方式:初始化程式碼
Private Sub UserForm_Initialize() ' 初始化隨機數生成器 Randomize Dim rng As Range ' 初始化 ComboBox1 到 ComboBox6 的資料 Set rng = ThisWorkbook.Sheets("武將表").Range("F12:F121") 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 ' 初始化兵種 ComboBox InitTypeComboBox Me.ComboBox19, ThisWorkbook.Sheets("武將表").Range("M11:Q11") InitTypeComboBox Me.ComboBox20, ThisWorkbook.Sheets("武將表").Range("M11:Q11") ' 初始化特定範圍 TextBox 的值為空白 ClearTextBox 7, 12 ' 初始化 WebBrowser1 Dim sDefHTML1 As String sDefHTML1 = "<html><head><style>" & _ "body {overflow-x:hidden;overflow-y:auto;margin:0;padding:2 5;font-family:'tahoma';font-size:8pt;}" & _ "p {margin:0;padding:0;}" & _ "</style></head><body></body></html>" With WebBrowser1 .Offline = True .Silent = True .Navigate "about:blank" .Document.Write sDefHTML1 End With WebBrowser1.Document.Body.innerHTML = "<p>初始狀態...</p>" ' 初始化 WebBrowser2 同樣的方式 Dim sDefHTML2 As String sDefHTML2 = "<html><head><style>" & _ "body {overflow-x:hidden;overflow-y:auto;margin:0;padding:2 5;font-family:'tahoma';font-size:8pt;}" & _ "p {margin:0;padding:0;}" & _ "</style></head><body></body></html>" With WebBrowser2 .Offline = True .Silent = True .Navigate "about:blank" .Document.Write sDefHTML2 End With ' 更新按鈕狀態 UpdateButtonStates End Sub顯示戰鬥結果
將顯示邏輯用到的部分改成:
Private Sub DisplayCombatResults(resultHtml As String) ' 更新 WebBrowser1 的內容顯示戰鬥結果 WebBrowser1.Document.Body.innerHTML = resultHtml End Sub顯示回合內容
將顯示回合邏輯替換並改寫為:
Private Sub ShowRoundContent(roundText As String) Dim position As Long Dim htmlContent As String Dim content As String Dim lines() As String Dim i As Integer Dim roundContent As String Dim endOfRound As Boolean endOfRound = False ' 假定 content 是全部 TextBox109 的等效內容 content = WebBrowser1.Document.Body.innerText ' 以行符拆分內容 lines = Split(content, vbCrLf) ' 找到所需回合文本的開始行 For i = LBound(lines) To UBound(lines) If InStr(lines(i), roundText) > 0 Then ' 開始行始,取得之后所有內容 roundContent = lines(i) & vbCrLf i = i + 1 Exit For End If Next i ' 繼續取得內容直到下一回合或文件的末尾 Do While i <= UBound(lines) And Not endOfRound If Trim(lines(i)) <> "" Then If InStr(lines(i), "回合") > 0 Then endOfRound = True Else roundContent = roundContent & lines(i) & vbCrLf ' 在每個武將動作之間添加一個空行 roundContent = roundContent & vbCrLf End If End If i = i + 1 Loop If roundContent <> "" Then ' 使用 WebBrowser2 來顯示回合內容 htmlContent = "<html><body>" & Replace(roundContent, vbCrLf, "<br>") & "</body></html>" UpdateWebBrowser2Content(htmlContent) Else MsgBox roundText & " 未找到", vbExclamation End If End Sub Private Sub UpdateWebBrowser2Content(content As String) ' 將 HTML 內容寫入 WebBrowser2 以顯示 WebBrowser2.Document.Body.innerHTML = content End Sub總結
這些變更將
ListBox1和TextBox109的功能轉為更靈活的 HTML 顯示。注意您需要在適當的地方填充和使用htmlContent,尤其是內容所需的基本格式、邏輯更改。這樣就能利用WebBrowser1和WebBrowser2來完成更加豐富的內容展示。 - 替換
- User
Private isUpdating As Boolean
Private Sub UserForm_Initialize() ' 初始化隨機數生成器 Randomize
Dim rng As Range ' 初始化 ComboBox1 到 ComboBox6 的資料 Set rng = ThisWorkbook.Sheets("武將表").Range("F12:F121") 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 ' 初始化兵種 ComboBox InitTypeComboBox Me.ComboBox19, ThisWorkbook.Sheets("武將表").Range("M11:Q11") InitTypeComboBox Me.ComboBox20, ThisWorkbook.Sheets("武將表").Range("M11:Q11") ' 初始化特定範圍 TextBox 的值為空白 ClearTextBox 7, 12 ' 初始化 WebBrowser1 Dim sDefHTML1 As String sDefHTML1 = "<html><head><style>" & _ "body {overflow-x:hidden;overflow-y:auto;margin:0;padding:2 5;font-family:'tahoma';font-size:8pt;}" & _ "p {margin:0;padding:0;}" & _ "</style></head><body></body></html>" With WebBrowser1 .Offline = True .Silent = True .Navigate "about:blank" .Document.Write sDefHTML1 End With WebBrowser1.Document.Body.innerHTML = "<p>初始狀態...</p>" ' 初始化 WebBrowser2 同樣的方式 Dim sDefHTML2 As String sDefHTML2 = "<html><head><style>" & _ "body {overflow-x:hidden;overflow-y:auto;margin:0;padding:2 5;font-family:'tahoma';font-size:8pt;}" & _ "p {margin:0;padding:0;}" & _ "</style></head><body></body></html>" With WebBrowser2 .Offline = True .Silent = True .Navigate "about:blank" .Document.Write sDefHTML2 End With ' 更新按鈕狀態 UpdateButtonStatesEnd Sub
Private Sub UpdateWebBrowser2Content(content As String) ' 將 HTML 內容寫入 WebBrowser2 以顯示 WebBrowser2.Document.Body.innerHTML = content End Sub
Private Sub DisplayCombatResults(resultHtml As String) ' 更新 WebBrowser1 的內容顯示戰鬥結果 WebBrowser1.Document.Body.innerHTML = resultHtml End Sub
Private Sub DisplaySomething() ' 構建要顯示的HTML內容 Dim htmlContent As String htmlContent = ""
' 假設從某個資料來源獲得一些項目 Dim i As Integer For i = 1 To 10 htmlContent = htmlContent & "<li>項目 " & i & "</li>" Next i htmlContent = htmlContent & "</ul>" ' 更新 WebBrowser2 內容 UpdateWebBrowser2Content (htmlContent)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 ComboBox7_Change() UpdateComboBoxesForSubset Array(Me.ComboBox7, Me.ComboBox8, Me.ComboBox9, Me.ComboBox13, Me.ComboBox14, Me.ComboBox15) End Sub
Private Sub ComboBox8_Change() UpdateComboBoxesForSubset Array(Me.ComboBox7, Me.ComboBox8, Me.ComboBox9, Me.ComboBox13, Me.ComboBox14, Me.ComboBox15) End Sub
Private Sub ComboBox9_Change() UpdateComboBoxesForSubset Array(Me.ComboBox7, Me.ComboBox8, Me.ComboBox9, Me.ComboBox13, Me.ComboBox14, Me.ComboBox15) End Sub
Private Sub ComboBox10_Change() UpdateComboBoxesForSubset Array(Me.ComboBox10, Me.ComboBox11, Me.ComboBox12, Me.ComboBox16, Me.ComboBox17, Me.ComboBox18) End Sub
Private Sub ComboBox11_Change() UpdateComboBoxesForSubset Array(Me.ComboBox10, Me.ComboBox11, Me.ComboBox12, Me.ComboBox16, Me.ComboBox17, Me.ComboBox18) End Sub
Private Sub ComboBox12_Change() UpdateComboBoxesForSubset Array(Me.ComboBox10, Me.ComboBox11, Me.ComboBox12, Me.ComboBox16, Me.ComboBox17, Me.ComboBox18) End Sub
Private Sub ComboBox13_Change() UpdateComboBoxesForSubset Array(Me.ComboBox7, Me.ComboBox8, Me.ComboBox9, Me.ComboBox13, Me.ComboBox14, Me.ComboBox15) End Sub
Private Sub ComboBox14_Change() UpdateComboBoxesForSubset Array(Me.ComboBox7, Me.ComboBox8, Me.ComboBox9, Me.ComboBox13, Me.ComboBox14, Me.ComboBox15) End Sub
Private Sub ComboBox15_Change() UpdateComboBoxesForSubset Array(Me.ComboBox7, Me.ComboBox8, Me.ComboBox9, Me.ComboBox13, Me.ComboBox14, Me.ComboBox15) End Sub
Private Sub ComboBox16_Change() UpdateComboBoxesForSubset Array(Me.ComboBox10, Me.ComboBox11, Me.ComboBox12, Me.ComboBox16, Me.ComboBox17, Me.ComboBox18) End Sub
Private Sub ComboBox17_Change() UpdateComboBoxesForSubset Array(Me.ComboBox10, Me.ComboBox11, Me.ComboBox12, Me.ComboBox16, Me.ComboBox17, Me.ComboBox18) End Sub
Private Sub ComboBox18_Change() UpdateComboBoxesForSubset Array(Me.ComboBox10, Me.ComboBox11, Me.ComboBox12, Me.ComboBox16, Me.ComboBox17, Me.ComboBox18) End Sub
Private Sub UpdateComboBoxesForSubset(comboBoxes As Variant) If isUpdating Then Exit Sub isUpdating = True
Dim i As Integer, j As Integer Dim cmb As MSForms.comboBox Dim currentValue As String Dim selectedValues As New Collection ' 收集所有被選擇的值 On Error Resume Next For i = LBound(comboBoxes) To UBound(comboBoxes) Set cmb = comboBoxes(i) If cmb.value <> "" Then selectedValues.Add cmb.value, CStr(cmb.value) End If Next i On Error GoTo 0 ' 更新每個 ComboBox 列表, 移除已選擇的值 For i = LBound(comboBoxes) To UBound(comboBoxes) Set cmb = comboBoxes(i) currentValue = cmb.value InitComboBox cmb, ThisWorkbook.Sheets("武將表").Range("AM122:AM234") ' 移除已選擇的值 For j = cmb.ListCount - 1 To 0 Step -1 If IsValueInCollection(selectedValues, cmb.list(j)) And cmb.list(j) <> currentValue Then cmb.RemoveItem j End If Next j cmb.value = currentValue Next i isUpdating = FalseEnd Sub
Private Function IsValueInCollection(coll As Collection, value As String) As Boolean Dim item As Variant IsValueInCollection = False On Error Resume Next For Each item In coll If item = value Then IsValueInCollection = True Exit Function End If Next item On Error GoTo 0 End Function
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 = FalseEnd 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 IfEnd 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 IfEnd 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 IfEnd 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("武將表") ' 使用 Application.Match 來避免錯誤 1004 Dim matchResult As Variant matchResult = Application.Match(typeCombo.value, ws.Range("M11:Q11"), 0) If IsError(matchResult) Then MsgBox "不能找到匹配的值。請檢查 ComboBox 的輸入值是否正確。", vbExclamation colOffset = 0 ' 設置默認值或相應處理 Else colOffset = matchResult - 1 End If UpdateTextBoxForWarrior ws, warriorCombo1, colOffset, txt1 UpdateTextBoxForWarrior ws, warriorCombo2, colOffset, txt2 UpdateTextBoxForWarrior ws, warriorCombo3, colOffset, txt3End 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 IfEnd Sub
Private Sub LimitTextBoxValue(txt As MSForms.TextBox) Dim inputValue As Integer inputValue = Val(txt.value)
If inputValue > 5 Then txt.value = "5" End IfEnd 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 ' 清空 ListBox1 Me.ListBox1.Clear ' 初始化武將及其屬性 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 ' 計算初始兵力總和 Dim initialTeam1Forces As Double Dim initialTeam2Forces As Double initialTeam1Forces = Val(TextBox7.value) + Val(TextBox8.value) + Val(TextBox9.value) initialTeam2Forces = Val(TextBox10.value) + Val(TextBox11.value) + Val(TextBox12.value) ' 開始模擬回合 SimulateRounds allUnits, initialTeam1Forces, initialTeam2Forces ' 更新 ListBox1 顯示 TextBox109 的內容 UpdateListBoxFromTextBoxEnd Sub
Private Sub UpdateListBoxFromTextBox() Dim content As String Dim lines() As String Dim i As Integer Dim paragraphEmpty As Boolean
' 取得 TextBox109 的所有內容 content = Me.TextBox109.Text ' 按行分隔內容 lines = Split(content, vbCrLf) ' 清除 ListBox1 的內容 Me.ListBox1.Clear ' 初始段落空行為 True paragraphEmpty = True ' 逐行添加到 ListBox1 中 For i = LBound(lines) To UBound(lines) Dim trimmedLine As String trimmedLine = Trim(lines(i)) If trimmedLine = "" Then ' 只在非段落開頭和非空段落後插入空白行 If Not paragraphEmpty Then Me.ListBox1.AddItem "" paragraphEmpty = True End If Else Me.ListBox1.AddItem trimmedLine paragraphEmpty = False End If Next iEnd Sub
Private Sub CommandButton2_Click() ' 清除 TextBox109 的內容 Me.TextBox109.Text = ""
' 清除 Label26 和 Label27 的內容 Me.Label26.Caption = "" Me.Label27.Caption = "" ' 清除 ListBox1 的內容 Me.ListBox1.ClearEnd 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 ' 添加戰法信息 Dim selfSkillName As String selfSkillName = Me.Controls("TextBox" & comboIndex).Text unit.Add "selfSkillName", selfSkillName unit.Add "selfSkillProbability", GetWarSkillProbability(selfSkillName) unit.Add "selfSkillType", GetWarSkillType(selfSkillName) ' 添加額外戰法信息 Dim additionalSkillName As String additionalSkillName = Me.Controls("ComboBox" & (comboIndex + 6)).value unit.Add "additionalSkillName", additionalSkillName unit.Add "additionalSkillProbability", GetWarSkillProbability(additionalSkillName) unit.Add "additionalSkillType", GetWarSkillType(additionalSkillName) ' 為第三組 ComboBox 初始化額外額外戰法信息 Dim extraSkillName As String extraSkillName = Me.Controls("ComboBox" & (comboIndex + 12)).value unit.Add "extraSkillName", extraSkillName unit.Add "extraSkillProbability", GetWarSkillProbability(extraSkillName) unit.Add "extraSkillType", GetWarSkillType(extraSkillName) ' 添加單位到集合 units.Add unit End IfEnd 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 = sortedUnitsEnd Function
Private Sub SimulateRounds(units As Collection, initialTeam1Forces As Double, initialTeam2Forces As Double) Dim attacker As Object, target As Object Dim speedSortedUnits As Collection Dim i As Integer, roundNumber As Integer Dim targetIndex As Integer Dim team1Forces As Double, team2Forces As Double Dim displayText As String Dim rng As Double Dim htmlContent As String
' 初始化顯示文本 htmlContent = "<h2>模擬戰鬥回合</h2>" ' 計算準備階段的消息顯示 htmlContent = htmlContent & "<p>準備階段:</p><ul>" ' 初始化計算每個單元的屬性 For i = 1 To units.Count Set attacker = units(i) htmlContent = htmlContent & "<li>" & attacker("warriorName") & " 參戰,兵力: " & attacker("forces") & "</li>" Next i htmlContent = htmlContent & "</ul>" ' 設定最大回合數 Dim maxRounds As Integer maxRounds = 8 ' 開始模擬回合 For roundNumber = 1 To maxRounds htmlContent = htmlContent & "<h3>第 " & roundNumber & " 回合</h3>" ' ?查部?是否仍然存在 team1Forces = 0 team2Forces = 0 For Each attacker In units If attacker("forces") > 0 Then If attacker("startIndex") <= 25 Then team1Forces = team1Forces + attacker("forces") Else team2Forces = team2Forces + attacker("forces") End If End If Next attacker ' 如果一方已經全部陣亡,結束 If team1Forces = 0 Or team2Forces = 0 Then Exit For End If ' 按速度排序單元 Set speedSortedUnits = SortUnitsBySpeed(units) ' 回合內每個角色行動 For Each attacker In speedSortedUnits If attacker("forces") > 0 Then ' 隨機選擇一個有效目標(不是同隊的,且仍有兵力) Do targetIndex = Application.WorksheetFunction.RandBetween(1, speedSortedUnits.Count) Set target = speedSortedUnits(targetIndex) Loop While (SameTeam(attacker("startIndex"), target("startIndex")) Or target("forces") <= 0) ' 計算傷害和更新目標狀態 Dim damage As Double damage = CalculateDamage(attacker, target) target("forces") = target("forces") - damage ' 結果顯示 htmlContent = htmlContent & "<p>" & attacker("warriorName") & " 攻擊 " & target("warriorName") & _ ",造成 " & damage & " 點傷害 (剩餘兵力: " & target("forces") & ")。</p>" End If Next attacker ' 使後續的回合有一個分隔 htmlContent = htmlContent & "<hr>" Next roundNumber ' 確定最終結果 htmlContent = htmlContent & "<h3>戰鬥結束</h3>" If team1Forces > team2Forces Then htmlContent = htmlContent & "<p><b>我方勝利!</b></p>" ElseIf team1Forces < team2Forces Then htmlContent = htmlContent & "<p><b>敵方勝利!</b></p>" Else htmlContent = htmlContent & "<p><b>雙方平手!</b></p>" End If ' 使用 WebBrowser1 顯示結果 WebBrowser1.Document.Body.innerHTML = htmlContentEnd Sub
Private Sub TryCombatSkills(attacker As Object, team As String, target As Object, ByRef displayText As String) Dim attackOrder As Variant attackOrder = Array("主動", "普通", "突擊", "被動")
Dim allSkills As Collection Set allSkills = New Collection allSkills.Add Array(attacker("selfSkillName"), attacker("selfSkillProbability"), attacker("selfSkillType")) allSkills.Add Array(attacker("additionalSkillName"), attacker("additionalSkillProbability"), attacker("additionalSkillType")) allSkills.Add Array(attacker("extraSkillName"), attacker("extraSkillProbability"), attacker("extraSkillType")) Dim skillInfo As Variant Dim rng As Double Dim isTriggered As Boolean ' 設定隊伍名稱 Dim teamName As String If attacker("startIndex") <= 25 Then teamName = "我方" Else teamName = "敵方" End If ' 確認技能順序和普通攻擊順序 For Each Order In attackOrder For Each skillInfo In allSkills If skillInfo(0) <> "" And skillInfo(2) = Order Then rng = Rnd() isTriggered = rng <= skillInfo(1) displayText = displayText & "(" & teamName & ") " & attacker("warriorName") & " 嘗試發動 " & skillInfo(0) & " (" & _ Format(skillInfo(1) * 100, "0.0") & "%) - " & IIf(isTriggered, "成功", "失敗") & "。" & vbCrLf End If Next skillInfo If Order = "普通" Then Dim damage As Double damage = CalculateDamage(attacker, target) ' 更新目標的兵力 target("forces") = target("forces") - damage ' 調整displayText以包含損失與剩餘信息 displayText = displayText & "(" & teamName & ") " & attacker("warriorName") & " 對 " & target("warriorName") & " 發動普通攻擊,造成損失 " & damage & " 兵力。" & _ " (剩餘 " & target("forces") & " 兵力)" & vbCrLfEnd If Next Order End Sub Private Sub TriggerPreparationSkills(attacker As Object, ByRef displayText As String) Dim skillTypes As Variant skillTypes = Array("指揮", "兵種", "陣法")
TriggerSkillsByType attacker, skillTypes, displayText, "準備階段"End Sub
Private Sub TriggerSkillsByType(attacker As Object, types As Variant, ByRef displayText As String, phase As String) Dim skillNames As Variant Dim SkillType As String Dim rng As Double Dim isTriggered As Boolean Dim teamName As String
' Determine if the unit is from team 1 or team 2 If attacker("startIndex") <= 25 Then teamName = "我方" Else teamName = "敵方" End If skillNames = Array(attacker("selfSkillName"), attacker("additionalSkillName"), attacker("extraSkillName")) ' Check skills and trigger them if applicable at the current phase For Each SkillName In skillNames If Not IsNull(SkillName) And SkillName <> "" Then ' Ensure skill names are not empty or null SkillType = GetWarSkillType(CStr(SkillName)) If IsInList(SkillType, types) Then rng = Rnd() isTriggered = rng <= GetWarSkillProbability(CStr(SkillName)) If isTriggered Then displayText = displayText & "(" & teamName & ") " & attacker("warriorName") & " 發動 " & SkillName & " (" _ & Format(GetWarSkillProbability(CStr(SkillName)) * 100, "0.0") & "%) - 成功" & vbCrLf Else displayText = displayText & "(" & teamName & ") " & attacker("warriorName") & " 嘗試發動 " & SkillName & " (" _ & Format(GetWarSkillProbability(CStr(SkillName)) * 100, "0.0") & "%) - 失敗" & vbCrLf End If End If End If Next SkillNameEnd Sub
Private Function IsInList(value As String, list As Variant) As Boolean Dim i As Integer For i = LBound(list) To UBound(list) If list(i) = value Then IsInList = True Exit Function End If Next i IsInList = False End Function
Private Function GetWarSkillProbability(SkillName As String) As Double Dim ws As Worksheet Dim r As Range Dim found As Range
Set ws = ThisWorkbook.Sheets("武將表") Set r = ws.Range("AM12:AM234") ' 假設技能名稱信息位於這個範圍 Set found = r.Find(What:=Trim(SkillName), LookIn:=xlValues, LookAt:=xlWhole) If Not found Is Nothing Then GetWarSkillProbability = ws.Cells(found.Row, "AO").value Else GetWarSkillProbability = 0 End IfEnd Function
Private Function GetWarSkillType(SkillName As String) As String Dim ws As Worksheet Dim r As Range Dim found As Range
Set ws = ThisWorkbook.Sheets("武將表") Set r = ws.Range("AM12:AM234") ' 假設技能名稱信息位於這個範圍 Set found = r.Find(What:=Trim(SkillName), LookIn:=xlValues, LookAt:=xlWhole) If Not found Is Nothing Then GetWarSkillType = ws.Cells(found.Row, "AN").value Else GetWarSkillType = "" End IfEnd Function
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 <= 25 And index2 <= 25) Or (index1 > 25 And index2 > 25) 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 ' 更新按鈕狀態 UpdateButtonStatesEnd Sub
Private Sub CommandButton39_Click() ShowRoundContent "準備回合" End Sub
Private Sub CommandButton40_Click() ShowRoundContent "第 1 回合" End Sub
Private Sub CommandButton41_Click() ShowRoundContent "第 2 回合" End Sub
Private Sub CommandButton42_Click() ShowRoundContent "第 3 回合" End Sub
Private Sub CommandButton43_Click() ShowRoundContent "第 4 回合" End Sub
Private Sub CommandButton44_Click() ShowRoundContent "第 5 回合" End Sub
Private Sub CommandButton45_Click() ShowRoundContent "第 6 回合" End Sub
Private Sub CommandButton46_Click() ShowRoundContent "第 7 回合" End Sub
Private Sub CommandButton47_Click() ShowRoundContent "第 8 回合" End Sub
Private Sub UpdateButtonStates() ' 檢查 TextBox55 到 TextBox60 的值,並相應地啟用或禁用按鈕 Me.CommandButton3.Enabled = (Val(Me.TextBox55.value) > 0) Me.CommandButton4.Enabled = (Val(Me.TextBox55.value) > 0) Me.CommandButton5.Enabled = (Val(Me.TextBox55.value) > 0) Me.CommandButton6.Enabled = (Val(Me.TextBox55.value) > 0) Me.CommandButton7.Enabled = (Val(Me.TextBox55.value) > 0) Me.CommandButton8.Enabled = (Val(Me.TextBox55.value) > 0)
Me.CommandButton9.Enabled = (Val(Me.TextBox56.value) > 0) Me.CommandButton10.Enabled = (Val(Me.TextBox56.value) > 0) Me.CommandButton11.Enabled = (Val(Me.TextBox56.value) > 0) Me.CommandButton12.Enabled = (Val(Me.TextBox56.value) > 0) Me.CommandButton13.Enabled = (Val(Me.TextBox56.value) > 0) Me.CommandButton14.Enabled = (Val(Me.TextBox56.value) > 0) Me.CommandButton15.Enabled = (Val(Me.TextBox57.value) > 0) Me.CommandButton16.Enabled = (Val(Me.TextBox57.value) > 0) Me.CommandButton17.Enabled = (Val(Me.TextBox57.value) > 0) Me.CommandButton18.Enabled = (Val(Me.TextBox57.value) > 0) Me.CommandButton19.Enabled = (Val(Me.TextBox57.value) > 0) Me.CommandButton20.Enabled = (Val(Me.TextBox57.value) > 0) Me.CommandButton21.Enabled = (Val(Me.TextBox58.value) > 0) Me.CommandButton22.Enabled = (Val(Me.TextBox58.value) > 0) Me.CommandButton23.Enabled = (Val(Me.TextBox58.value) > 0) Me.CommandButton24.Enabled = (Val(Me.TextBox58.value) > 0) Me.CommandButton25.Enabled = (Val(Me.TextBox58.value) > 0) Me.CommandButton26.Enabled = (Val(Me.TextBox58.value) > 0) Me.CommandButton27.Enabled = (Val(Me.TextBox59.value) > 0) Me.CommandButton28.Enabled = (Val(Me.TextBox59.value) > 0) Me.CommandButton29.Enabled = (Val(Me.TextBox59.value) > 0) Me.CommandButton30.Enabled = (Val(Me.TextBox59.value) > 0) Me.CommandButton31.Enabled = (Val(Me.TextBox59.value) > 0) Me.CommandButton32.Enabled = (Val(Me.TextBox59.value) > 0) Me.CommandButton33.Enabled = (Val(Me.TextBox60.value) > 0) Me.CommandButton34.Enabled = (Val(Me.TextBox60.value) > 0) Me.CommandButton35.Enabled = (Val(Me.TextBox60.value) > 0) Me.CommandButton36.Enabled = (Val(Me.TextBox60.value) > 0) Me.CommandButton37.Enabled = (Val(Me.TextBox60.value) > 0) Me.CommandButton38.Enabled = (Val(Me.TextBox60.value) > 0)End Sub
Private Sub TextBox55_Change() If Val(Me.TextBox55.value) < 0 Then Me.TextBox55.value = 0 UpdateButtonStates End Sub
Private Sub TextBox56_Change() If Val(Me.TextBox56.value) < 0 Then Me.TextBox56.value = 0 UpdateButtonStates End Sub
Private Sub TextBox57_Change() If Val(Me.TextBox57.value) < 0 Then Me.TextBox57.value = 0 UpdateButtonStates End Sub
Private Sub TextBox58_Change() If Val(Me.TextBox58.value) < 0 Then Me.TextBox58.value = 0 UpdateButtonStates End Sub
Private Sub TextBox59_Change() If Val(Me.TextBox59.value) < 0 Then Me.TextBox59.value = 0 UpdateButtonStates End Sub
Private Sub TextBox60_Change() If Val(Me.TextBox60.value) < 0 Then Me.TextBox60.value = 0 UpdateButtonStates End Sub
Private Function SafeVal(txtBox As MSForms.TextBox) As Integer ' 如果TextBox有合法數字,返回其值,否則返回0 On Error Resume Next SafeVal = Val(txtBox.value) If Err.number <> 0 Then SafeVal = 0 End If On Error GoTo 0 End Function
Private Function SafeValDouble(txtBox As MSForms.TextBox) As Double ' 如果TextBox有合法數字,返回其值,否則返回0.0 On Error Resume Next SafeValDouble = CDbl(txtBox.value) If Err.number <> 0 Then SafeValDouble = 0# End If On Error GoTo 0 End Function
Private Sub ShowRoundContent(roundText As String) Dim position As Long Dim htmlContent As String Dim content As String Dim lines() As String Dim i As Integer Dim roundContent As String Dim endOfRound As Boolean endOfRound = False
' 假定 content 是全部 TextBox109 的等效內容 content = WebBrowser1.Document.Body.innerText ' 以行符拆分內容 lines = Split(content, vbCrLf) ' 找到所需回合文本的開始行 For i = LBound(lines) To UBound(lines) If InStr(lines(i), roundText) > 0 Then ' 開始行始,取得之后所有內容 roundContent = lines(i) & vbCrLf i = i + 1 Exit For End If Next i ' 繼續取得內容直到下一回合或文件的末尾 Do While i <= UBound(lines) And Not endOfRound If Trim(lines(i)) <> "" Then If InStr(lines(i), "回合") > 0 Then endOfRound = True Else roundContent = roundContent & lines(i) & vbCrLf ' 在每個武將動作之間添加一個空行 roundContent = roundContent & vbCrLf End If End If i = i + 1 Loop If roundContent <> "" Then ' 使用 WebBrowser2 來顯示回合內容 htmlContent = "<html><body>" & Replace(roundContent, vbCrLf, "<br>") & "</body></html>" UpdateWebBrowser2Content (htmlContent) Else MsgBox roundText & " 未找到", vbExclamation End IfEnd Sub
我已經沒有TEXTBOX109跟LISTBOX1 請把跟這兩個有關的程式進行修正或移除
- Assistant
好的,將程式碼中所有涉及
TextBox109和ListBox1的部分移除或修正,讓我們只保留使用WebBrowser1和WebBrowser2來顯示內容的邏輯。修正後的程式碼:
以下是更新後的程式碼,完全移除了
TextBox109和ListBox1的引用與功能,並轉而使用WebBrowser1和WebBrowser2來進行顯示:Private isUpdating As Boolean Private Sub UserForm_Initialize() ' 初始化隨機數生成器 Randomize Dim rng As Range ' 初始化 ComboBox1 到 ComboBox6 的資料 Set rng = ThisWorkbook.Sheets("武將表").Range("F12:F121") 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 ' 初始化兵種 ComboBox InitTypeComboBox Me.ComboBox19, ThisWorkbook.Sheets("武將表").Range("M11:Q11") InitTypeComboBox Me.ComboBox20, ThisWorkbook.Sheets("武將表").Range("M11:Q11") ' 初始化特定範圍 TextBox 的值為空白 ClearTextBox 7, 12 ' 初始化 WebBrowser1 Dim sDefHTML1 As String sDefHTML1 = "<html><head><style>" & _ "body {overflow-x:hidden;overflow-y:auto;margin:0;padding:2 5;font-family:'tahoma';font-size:8pt;}" & _ "p {margin:0;padding:0;}" & _ "</style></head><body></body></html>" With WebBrowser1 .Offline = True .Silent = True .Navigate "about:blank" .Document.Write sDefHTML1 End With WebBrowser1.Document.Body.innerHTML = "<p>初始狀態...</p>" ' 初始化 WebBrowser2 同樣的方式 Dim sDefHTML2 As String sDefHTML2 = "<html><head><style>" & _ "body {overflow-x:hidden;overflow-y:auto;margin:0;padding:2 5;font-family:'tahoma';font-size:8pt;}" & _ "p {margin:0;padding:0;}" & _ "</style></head><body></body></html>" With WebBrowser2 .Offline = True .Silent = True .Navigate "about:blank" .Document.Write sDefHTML2 End With ' 更新按鈕狀態 UpdateButtonStates End Sub Private Sub UpdateWebBrowser2Content(content As String) ' 將 HTML 內容寫入 WebBrowser2 以顯示 WebBrowser2.Document.Body.innerHTML = content End Sub Private Sub DisplayCombatResults(resultHtml As String) ' 更新 WebBrowser1 的內容顯示戰鬥結果 WebBrowser1.Document.Body.innerHTML = resultHtml 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 ComboBox7_Change() UpdateComboBoxesForSubset Array(Me.ComboBox7, Me.ComboBox8, Me.ComboBox9, Me.ComboBox13, Me.ComboBox14, Me.ComboBox15) End Sub Private Sub ComboBox8_Change() UpdateComboBoxesForSubset Array(Me.ComboBox7, Me.ComboBox8, Me.ComboBox9, Me.ComboBox13, Me.ComboBox14, Me.ComboBox15) End Sub Private Sub ComboBox9_Change() UpdateComboBoxesForSubset Array(Me.ComboBox7, Me.ComboBox8, Me.ComboBox9, Me.ComboBox13, Me.ComboBox14, Me.ComboBox15) End Sub Private Sub ComboBox10_Change() UpdateComboBoxesForSubset Array(Me.ComboBox10, Me.ComboBox11, Me.ComboBox12, Me.ComboBox16, Me.ComboBox17, Me.ComboBox18) End Sub Private Sub ComboBox11_Change() UpdateComboBoxesForSubset Array(Me.ComboBox10, Me.ComboBox11, Me.ComboBox12, Me.ComboBox16, Me.ComboBox17, Me.ComboBox18) End Sub Private Sub ComboBox12_Change() UpdateComboBoxesForSubset Array(Me.ComboBox10, Me.ComboBox11, Me.ComboBox12, Me.ComboBox16, Me.ComboBox17, Me.ComboBox18) End Sub Private Sub ComboBox13_Change() UpdateComboBoxesForSubset Array(Me.ComboBox7, Me.ComboBox8, Me.ComboBox9, Me.ComboBox13, Me.ComboBox14, Me.ComboBox15) End Sub Private Sub ComboBox14_Change() UpdateComboBoxesForSubset Array(Me.ComboBox7, Me.ComboBox8, Me.ComboBox9, Me.ComboBox13, Me.ComboBox14, Me.ComboBox15) End Sub Private Sub ComboBox15_Change() UpdateComboBoxesForSubset Array(Me.ComboBox7, Me.ComboBox8, Me.ComboBox9, Me.ComboBox13, Me.ComboBox14, Me.ComboBox15) End Sub Private Sub ComboBox16_Change() UpdateComboBoxesForSubset Array(Me.ComboBox10, Me.ComboBox11, Me.ComboBox12, Me.ComboBox16, Me.ComboBox17, Me.ComboBox18) End Sub Private Sub ComboBox17_Change() UpdateComboBoxesForSubset Array(Me.ComboBox10, Me.ComboBox11, Me.ComboBox12, Me.ComboBox16, Me.ComboBox17, Me.ComboBox18) End Sub Private Sub ComboBox18_Change() UpdateComboBoxesForSubset Array(Me.ComboBox10, Me.ComboBox11, Me.ComboBox12, Me.ComboBox16, Me.ComboBox17, Me.ComboBox18) End Sub Private Sub UpdateComboBoxesForSubset(comboBoxes As Variant) If isUpdating Then Exit Sub isUpdating = True Dim i As Integer, j As Integer Dim cmb As MSForms.comboBox Dim currentValue As String Dim selectedValues As New Collection ' 收集所有被選擇的值 On Error Resume Next For i = LBound(comboBoxes) To UBound(comboBoxes) Set cmb = comboBoxes(i) If cmb.value <> "" Then selectedValues.Add cmb.value, CStr(cmb.value) End If Next i On Error GoTo 0 ' 更新每個 ComboBox 列表, 移除已選擇的值 For i = LBound(comboBoxes) To UBound(comboBoxes) Set cmb = comboBoxes(i) currentValue = cmb.value InitComboBox cmb, ThisWorkbook.Sheets("武將表").Range("AM122:AM234") ' 移除已選擇的值 For j = cmb.ListCount - 1 To 0 Step -1 If IsValueInCollection(selectedValues, cmb.list(j)) And cmb.list(j) <> currentValue Then cmb.RemoveItem j End If Next j cmb.value = currentValue Next i isUpdating = False End Sub Private Function IsValueInCollection(coll As Collection, value As String) As Boolean Dim item As Variant IsValueInCollection = False On Error Resume Next For Each item In coll If item = value Then IsValueInCollection = True Exit Function End If Next item On Error GoTo 0 End Function 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("武將表") ' 使用 Application.Match 來避免錯誤 1004 Dim matchResult As Variant matchResult = Application.Match(typeCombo.value, ws.Range("M11:Q11"), 0) If IsError(matchResult) Then MsgBox "不能找到匹配的值。請檢查 ComboBox 的輸入值是否正確。", vbExclamation colOffset = 0 ' 設置默認值或相應處理 Else colOffset = matchResult - 1 End If 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 ' 初始化武將及其屬性 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 ' 計算初始兵力總和 Dim initialTeam1Forces As Double Dim initialTeam2Forces As Double initialTeam1Forces = Val(TextBox7.value) + Val(TextBox8.value) + Val(TextBox9.value) initialTeam2Forces = Val(TextBox10.value) + Val(TextBox11.value) + Val(TextBox12.value) ' 開始模擬回合 SimulateRounds allUnits, initialTeam1Forces, initialTeam2Forces End Sub Private Sub CommandButton2_Click() ' 清除 WebBrowser1 和 WebBrowser2 的內容 WebBrowser1.Document.Body.innerHTML = "<p></p>" WebBrowser2.Document.Body.innerHTML = "<p></p>" ' 清除 Label26 和 Label27 的內容 Me.Label26.Caption = "" Me.Label27.Caption = "" 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 ' 添加戰法信息 Dim selfSkillName As String selfSkillName = Me.Controls("TextBox" & comboIndex).Text unit.Add "selfSkillName", selfSkillName unit.Add "selfSkillProbability", GetWarSkillProbability(selfSkillName) unit.Add "selfSkillType", GetWarSkillType(selfSkillName) ' 添加額外戰法信息 Dim additionalSkillName As String additionalSkillName = Me.Controls("ComboBox" & (comboIndex + 6)).value unit.Add "additionalSkillName", additionalSkillName unit.Add "additionalSkillProbability", GetWarSkillProbability(additionalSkillName) unit.Add "additionalSkillType", GetWarSkillType(additionalSkillName) ' 為第三組 ComboBox 初始化額外額外戰法信息 Dim extraSkillName As String extraSkillName = Me.Controls("ComboBox" & (comboIndex + 12)).value unit.Add "extraSkillName", extraSkillName unit.Add "extraSkillProbability", GetWarSkillProbability(extraSkillName) unit.Add "extraSkillType", GetWarSkillType(extraSkillName) ' 添加單位到集合 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, initialTeam1Forces As Double, initialTeam2Forces As Double) Dim attacker As Object, target As Object Dim speedSortedUnits As Collection Dim i As Integer, roundNumber As Integer Dim targetIndex As Integer Dim team1Forces As Double, team2Forces As Double Dim htmlContent As String ' 初始化顯示文本 htmlContent = "<h2>模擬戰鬥回合</h2>" ' 準備階段消息顯示 htmlContent = htmlContent & "<p>準備階段:</p><ul>" ' 初始化計算每個單元的屬性 For i = 1 To units.Count Set attacker = units(i) htmlContent = htmlContent & "<li>" & attacker("warriorName") & " 參戰,兵力: " & attacker("forces") & "</li>" Next i htmlContent = htmlContent & "</ul>" ' 設定最大回合數 Dim maxRounds As Integer maxRounds = 8 ' 開始模擬回合 For roundNumber = 1 To maxRounds htmlContent = htmlContent & "<h3>第 " & roundNumber & " 回合</h3>" ' 檢查部隊是否仍然存在 team1Forces = 0 team2Forces = 0 For Each attacker In units If attacker("forces") > 0 Then If attacker("startIndex") <= 25 Then team1Forces = team1Forces + attacker("forces") Else team2Forces = team2Forces + attacker("forces") End If End If Next attacker ' 如果一方已經全部陣亡,結束 If team1Forces = 0 Or team2Forces = 0 Then Exit For End If ' 按速度排序單元 Set speedSortedUnits = SortUnitsBySpeed(units) ' 回合內每個角色行動 For Each attacker In speedSortedUnits If attacker("forces") > 0 Then ' 隨機選擇一個有效目標(不是同隊的,且仍有兵力) Do targetIndex = Application.WorksheetFunction.RandBetween(1, speedSortedUnits.Count) Set target = speedSortedUnits(targetIndex) Loop While (SameTeam(attacker("startIndex"), target("startIndex")) Or target("forces") <= 0) ' 計算傷害和更新目標狀態 Dim damage As Double damage = CalculateDamage(attacker, target) target("forces") = target("forces") - damage ' 結果顯示 htmlContent = htmlContent & "<p>" & attacker("warriorName") & " 攻擊 " & target("warriorName") & _ ",造成 " & damage & " 點傷害 (剩餘兵力: " & target("forces") & ")。</p>" End If Next attacker ' 使後續的回合有一個分隔 htmlContent = htmlContent & "<hr>" Next roundNumber ' 確定最終結果 htmlContent = htmlContent & "<h3>戰鬥結束</h3>" If team1Forces > team2Forces Then htmlContent = htmlContent & "<p><b>我方勝利!</b></p>" ElseIf team1Forces < team2Forces Then htmlContent = htmlContent & "<p><b>敵方勝利!</b></p>" Else htmlContent = htmlContent & "<p><b>雙方平手!</b></p>" End If ' 使用 WebBrowser1 顯示結果 WebBrowser1.Document.Body.innerHTML = htmlContent 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 <= 25 And index2 <= 25) Or (index1 > 25 And index2 > 25) End Function Private Sub UpdateButtonStates() ' 檢查 TextBox55 到 TextBox60 的值,並相應地啟用或禁用按鈕 Me.CommandButton3.Enabled = (Val(Me.TextBox55.value) > 0) Me.CommandButton4.Enabled = (Val(Me.TextBox55.value) > 0) Me.CommandButton5.Enabled = (Val(Me.TextBox55.value) > 0) Me.CommandButton6.Enabled = (Val(Me.TextBox55.value) > 0) Me.CommandButton7.Enabled = (Val(Me.TextBox55.value) > 0) Me.CommandButton8.Enabled = (Val(Me.TextBox55.value) > 0) Me.CommandButton9.Enabled = (Val(Me.TextBox56.value) > 0) Me.CommandButton10.Enabled = (Val(Me.TextBox56.value) > 0) Me.CommandButton11.Enabled = (Val(Me.TextBox56.value) > 0) Me.CommandButton12.Enabled = (Val(Me.TextBox56.value) > 0) Me.CommandButton13.Enabled = (Val(Me.TextBox56.value) > 0) Me.CommandButton14.Enabled = (Val(Me.TextBox56.value) > 0) Me.CommandButton15.Enabled = (Val(Me.TextBox57.value) > 0) Me.CommandButton16.Enabled = (Val(Me.TextBox57.value) > 0) Me.CommandButton17.Enabled = (Val(Me.TextBox57.value) > 0) Me.CommandButton18.Enabled = (Val(Me.TextBox57.value) > 0) Me.CommandButton19.Enabled = (Val(Me.TextBox57.value) > 0) Me.CommandButton20.Enabled = (Val(Me.TextBox57.value) > 0) Me.CommandButton21.Enabled = (Val(Me.TextBox58.value) > 0) Me.CommandButton22.Enabled = (Val(Me.TextBox58.value) > 0) Me.CommandButton23.Enabled = (Val(Me.TextBox58.value) > 0) Me.CommandButton24.Enabled = (Val(Me.TextBox58.value) > 0) Me.CommandButton25.Enabled = (Val(Me.TextBox58.value) > 0) Me.CommandButton26.Enabled = (Val(Me.TextBox58.value) > 0) Me.CommandButton27.Enabled = (Val(Me.TextBox59.value) > 0) Me.CommandButton28.Enabled = (Val(Me.TextBox59.value) > 0) Me.CommandButton29.Enabled = (Val(Me.TextBox59.value) > 0) Me.CommandButton30.Enabled = (Val(Me.TextBox59.value) > 0) Me.CommandButton31.Enabled = (Val(Me.TextBox59.value) > 0) Me.CommandButton32.Enabled = (Val(Me.TextBox59.value) > 0) Me.CommandButton33.Enabled = (Val(Me.TextBox60.value) > 0) Me.CommandButton34.Enabled = (Val(Me.TextBox60.value) > 0) Me.CommandButton35.Enabled = (Val(Me.TextBox60.value) > 0) Me.CommandButton36.Enabled = (Val(Me.TextBox60.value) > 0) Me.CommandButton37.Enabled = (Val(Me.TextBox60.value) > 0) Me.CommandButton38.Enabled = (Val(Me.TextBox60.value) > 0) End Sub說明
TextBox109和ListBox1的引用已被移除,相關功能被WebBrowser1和WebBrowser2替換。- 並未修改不涉及
TextBox109和ListBox1的邏輯部分,仍會保持現有戰鬥模擬和其他計算功能。 - 在
CommandButton1和ShowRoundContent等類似功能中,更新和顯示都透過WebBrowser控件的innerHTML進行。
如果需要進一步調整或確認其他部分的操作實現,可以隨時告知。
- User
Private Sub AddUnit(units As Collection, Name As String, cmb As MSForms.comboBox, startIndex As Integer, forceTextBox As MSForms.TextBox, proficiencyTextBox As MSForms.TextBox) 沒有定義
- Assistant
以上的程式碼提供了一個名為
AddUnit的子程序,用於新增一個單位到單位集合中。這段子程序的主要目的在於根據輸入的參數來初始化一個單位對象,並將其添加到作為參數傳入的單位集合中。關鍵步驟:
-
參數說明:
units:這是一個Collection(集合),用來存放所有單位的資訊。Name:用來指出單位的名稱角色,例如 "主將" 或 "副將"。cmb: 對應的ComboBox控件,提供單位的選擇信息。startIndex:指示用於讀取文本框中相關數據的起始位置。forceTextBox:提供該單位的兵力值。proficiencyTextBox:表示該單位的兵種適應性。
-
實現單位初始化:
- 使用
Scripting.Dictionary創建一個字典作為每個單位的對象。 - 從控件中取得所需的信息並計算單位屬性。
- 根據這些信息設置單位對象的各種屬性。
- 使用
-
修復錯誤: 確保您在使用該
AddUnit子程序之前,已經在表單或模組中定義並初始化好需要的集合和控件。
以下是完整的
AddUnit子程序定義: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 ' 預先計算適性調整因子 proficiencyFactor = GetProficiencyFactor(proficiencyTextBox.value) 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 ' 添加戰法信息 Dim selfSkillName As String selfSkillName = Me.Controls("TextBox" & comboIndex).Text unit.Add "selfSkillName", selfSkillName unit.Add "selfSkillProbability", GetWarSkillProbability(selfSkillName) unit.Add "selfSkillType", GetWarSkillType(selfSkillName) ' 添加額外戰法信息 Dim additionalSkillName As String additionalSkillName = Me.Controls("ComboBox" & (comboIndex + 6)).value unit.Add "additionalSkillName", additionalSkillName unit.Add "additionalSkillProbability", GetWarSkillProbability(additionalSkillName) unit.Add "additionalSkillType", GetWarSkillType(additionalSkillName) ' 為第三組 ComboBox 初始化額外額外戰法信息 Dim extraSkillName As String extraSkillName = Me.Controls("ComboBox" & (comboIndex + 12)).value unit.Add "extraSkillName", extraSkillName unit.Add "extraSkillProbability", GetWarSkillProbability(extraSkillName) unit.Add "extraSkillType", GetWarSkillType(extraSkillName) ' 添加單位到集合 units.Add unit End If End Sub確保功能正常:
- 檢查控件:確保所有引用的控件(例如
ComboBox和TextBox)在您的表單上已經正確地添加和命名。 - 戰法計算函數:確保存在
GetWarSkillProbability和GetWarSkillType這些方法來獲取戰法信息。 - 集合初始化:使用該方法前,應確保
units是已初始化的Collection。
這樣,您應能夠正常使用
AddUnit來添加單位信息至系統中。 -
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.