我正在一本有两页的工作簿中(一页称为角色表,另一页称为战斗)。我有一个下拉菜单,从字符表中提取信息并将值放入单元格 E2 中。单元格 E2 已从 E2:G2 合并并居中。我正在尝试运行一个脚本,该脚本将看到下拉菜单已更改并在其下方粘贴一堆信息。
这是我试图用来调用 sub 的脚本
Private Sub Worksheet_Change(ByVal Target as Range)
Dim selectedCell As Range
' Store the currently selected cell
Set selectedCell = Selection
Application.EnableEvents = False
Call Weapons
Application.EnableEvents = True
' Reselect the previously selected cell
selectedCell.Select
End Sub
我呼叫的潜艇是
Sub Weapons()
' Define worksheets so thing can get cross referential
Dim CS As Worksheet
Set CS = ThisWorkbook.Sheets("Character Sheet")
Dim combat As Worksheet
Set combat = ThisWorkbook.Sheets("Combat")
' Make a variable for to hit
Dim TH As Integer
' Greatsword
If combat.Range("E2") = "Greatsword" Then
TH = CS.Range("E1") + CS.Range("C8")
With combat
.Range("E4").value = "2d6"
.Range("F4").value = CS.Range("E1")
.Range("G4").value = "Slashing"
.Range("F3").value = TH
.Range("G3").value = "To hit"
End With
' Eldritch Blast
ElseIf combat.Range("E2") = "Eldritch Blast" Then
TH = CS.Range("C8") + CS.Range("E6")
With combat
.Range("E4").value = "2d10"
.Range("F4").value = CS.Range("E6")
.Range("G4").value = "Force"
.Range("F3").value = TH
.Range("G3").value = "To hit"
End With
End If
End Sub
现在我不认为它是武器子项,因为当我尝试用一个只有“呼叫武器”的按钮来调用它时,它工作得很好。我没想到这会如此困难,哈哈。
我尝试了几个不同版本的更改宏,但没有效果。 我尝试将其放入每个页面中,包括新模块和此工作簿,甚至无法显示它和调试消息
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Me.Range("E2")) Is Nothing Then
Debug.Print "Change in E2 detected"
Dim selectedCell As Range
Set selectedCell = Selection
Application.EnableEvents = False
Debug.Print "Calling Weapons"
Call Weapons
Application.EnableEvents = True
selectedCell.Select
End If
End Sub
我用这个做了同样的事情,得到了同样的结果
Private Sub Worksheet_Calculate()
Debug.Print "Calculate event triggered"
Dim selectedCell As Range
Set selectedCell = Selection
Application.EnableEvents = False
Debug.Print "Calling Weapons"
Call Weapons
Application.EnableEvents = True
selectedCell.Select
End Sub
我也做了同样的事情
Private Sub Worksheet_Calculate()
Static oldVal As Variant
Dim newVal As Variant
newVal = Me.Range("E2").Value
If newVal <> oldVal Then
Debug.Print "Change in E2 detected"
Dim selectedCell As Range
Set selectedCell = Selection
Application.EnableEvents = False
Debug.Print "Calling Weapons"
Call Weapons
Application.EnableEvents = True
selectedCell.Select
End If
oldVal = newVal
End Sub
这些都是我在谷歌搜索时发现的潜在解决方案。但遗憾的是似乎没有任何效果。
工作表模块,例如
Sheet1(Combat)
Private Sub Worksheet_Change(ByVal Target As Range)
Weaponize Target
End Sub
标准模块,例如
Module1
、modEvents
...(或在同一工作表模块中)
Sub Weaponize(ByVal Target As Range)
On Error GoTo ClearError
Const SHOW_MESSAGE_IF_NOT_FOUND As Boolean = True
Dim trg1 As Range, trg2 As Range, tArr1() As Variant, tArr2() As Variant
Dim IsNotFound As Boolean
With Target.Worksheet ' Target Sheet ('Combat')
Dim Weapon As String
With .Range("E2")
If Intersect(.Cells, Target) Is Nothing Then Exit Sub
Weapon = CStr(.Value)
End With
Set trg1 = .Range("F3:G3")
Set trg2 = .Range("E4:G4")
With .Parent.Sheets("Character Sheet") ' Source Sheet
Select Case Weapon
Case "Greatsword"
tArr1 = Array(.Range("E1").Value + .Range("C8").Value, "To hit")
tArr2 = Array("2d6", .Range("E1").Value, "Slashing")
Case "Eldritch Blast"
tArr1 = Array(.Range("E6").Value + .Range("C8").Value, "To hit")
tArr2 = Array("2d10", .Range("E6").Value, "Force")
' more cases here!
Case Else
IsNotFound = True
End Select
End With
End With
If IsNotFound Then
If SHOW_MESSAGE_IF_NOT_FOUND Then
MsgBox "The weapon """ & Weapon & """ was not found!", vbExclamation
End If
Exit Sub
End If
Application.EnableEvents = False
trg1.Value = tArr1
trg2.Value = tArr2
ProcExit:
Application.EnableEvents = True
Exit Sub
ClearError:
MsgBox "Run-time error '" & Err.Number & "':" & vbLf & vbLf _
& Err.Description, vbCritical
Resume ProcExit
End Sub