【问题标题】:VBA: using data on one sheet to target another sheetVBA:使用一张纸上的数据来定位另一张纸
【发布时间】:2018-11-19 12:18:18
【问题描述】:

我正在使用 Excel 创建一个工具,可以帮助我 DM 一个 D&D 活动,并且目前正在研究一个施法系统。我的目标是能够单击一个工作表(“战斗”)上的按钮,这将减少另一个工作表中的拼写槽(数值)(目标工作表的名称源自“战斗”中的文本值工作表),对应于给定的法术等级。这些值由“战斗”表中的单元格引用,这些值将随另一表中单元格的值而变化。 我的代码是:

Private Sub CastSpell_Click()
    SpellAction "CastSpell"
End Sub


Sub SpellAction(ByVal action As String)
    Dim Combat As Worksheet
    Set Combat = ThisWorkbook.Sheets("Combat")
    Dim Player As Worksheet
    Set Player = ThisWorkbook.Sheets(Cells(5, 12).Text)

    Dim foundCell As Range
    Set foundCell = Player.Range("Z31", "Z39").Find(Left(Combat.Cells(18, 12).Text, 3))

    If Not (foundCell Is Nothing) Then
        r = foundCell.Row
    Else
        MsgBox "Couldn't find the current selection."
        Exit Sub
    End If

    Select Case LCase(action)
        Case "CastSpell"
            If Not (Player.Cells(r, 28) = 0) Then
                Cells(r, 28) = Cells(r, 28) - 1
            Else
                MsgBox "No remaining spell slots of this level."
                Exit Sub
            End If
    End Select
End Sub

在哪里 Cells(5, 12) 表示从下拉列表中选择的文本字符串,Range("Z31", "Z39") 表示包含值 (1) 到 (9) 的列,Cells(18, 12) 表示法术名称,(例如:(1) Cure Wounds ),Cells(r, 28) 中引用的数字 28 指的是 AB 列,这是我希望减少其值的单元格的列。

目前,当我单击按钮(名为“CastSpell”并位于“战斗”表中)时,它会运行SpellAction 代码,找到PlayerfoundCell 和@987654329 的正确值@ 没有错误消息,但不执行 "CastSpell" 内的代码。我哪里错了?

【问题讨论】:

    标签: vba excel


    【解决方案1】:

    我将范围声明为“Z31:Z39”。目前您只指 z31 和 z39,而不是介于两者之间的所有单元格。逗号是您如何包含多个不按顺序排列的范围。然而,这可能不是它失败的原因,值得指出的是你错过了大部分选择。看起来你在小写字符串上使用选择大小写,然后检查一个字符串有大写字母,这很可能是您将字符串转换为“xxxxx”但检查“XxxxxXxx”时失败的原因。要么转换它,然后检查字符串的小写版本,要么根本不费心转换。

    这有帮助吗?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-12-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多