仅对某一特定列使用宏代码

发布于 2024-12-18 11:52:45 字数 899 浏览 1 评论 0原文

我对 Excel 编程还是个新手,对代码进行细微更改时遇到了麻烦。我的代码使用户能够选择一个单元格,然后使用向上和向下箭头来选择定义的值。查看下面的代码,我在工作表中调用函数“UpOn​​e”和“DownOne”,然后将保存的字符串值保存在模块中。

工作表代码

Private Sub Worksheet_Open()
    Application.OnKey "{UP}", "UpOne"
    Application.OnKey "{DOWN}", "DownOne"
End Sub

模块代码

Sub DownOne()
    Select Case ActiveCell.Value
    Case ""
        ActiveCell.Value = "PASS"
    Case "PASS"
        ActiveCell.Value = "FAIL"
    Case "FAIL"
        ActiveCell.Value = "Unknown"
 End Sub
 Sub UpOne()
    Select Case ActiveCell.Value
    Case "Unknown"
        ActiveCell.Value = "FAIL"
    Case "FAIL"
        ActiveCell.Value = "PASS"
    Case "PASS"
        ActiveCell.Value = ""
 End Sub

我的问题是如何使该代码仅适用于某一特定列中的所有单元格?当用户选择不同的列时,我将如何获得不同的值。因此,如果用户选择“J”列中的空单元格,他/她可以使用箭头键浏览“A”、“B”、“C”等值,但是当他/她选择“N”列中的空单元格时”,他/她可以浏览一组不同的值,例如“E”、“F”、“G”等。 感谢您的帮助!

I'm still new to programming in excel and I am having trouble making slight changes to my code.My code enables the user to select a cell and then use the up and down arrows to choose a defined value. Looking at the code below, I call the functions "UpOne" and "DownOne" in my worksheet and then I have my saved strings values in a module.

Worksheet Code

Private Sub Worksheet_Open()
    Application.OnKey "{UP}", "UpOne"
    Application.OnKey "{DOWN}", "DownOne"
End Sub

Module Code

Sub DownOne()
    Select Case ActiveCell.Value
    Case ""
        ActiveCell.Value = "PASS"
    Case "PASS"
        ActiveCell.Value = "FAIL"
    Case "FAIL"
        ActiveCell.Value = "Unknown"
 End Sub
 Sub UpOne()
    Select Case ActiveCell.Value
    Case "Unknown"
        ActiveCell.Value = "FAIL"
    Case "FAIL"
        ActiveCell.Value = "PASS"
    Case "PASS"
        ActiveCell.Value = ""
 End Sub

My question is how would I make this code only work for all cells in one specific column? And how would I have different values when the user chooses a different column. So if user selects an empty cell in column "J", he/she can navigates with the arrows keys through values such as "A","B","C" but when he/she selects an empty cell in Column "N", he/she can navigate through a different set of values such as "E", "F", "G", etc.
thank you for any help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

玉环 2024-12-25 11:52:45

与每次选择单元格时禁用/启用代码(另外,您需要满足多单元格选择的要求)相比,使用数据验证会更简单。您可以设置适用于特定单元格的列表。Debra

Dalgleish 在 Contextures在此处输入图像描述

Rather than disabling/enabling code every time you selected a cell (plus you would need to cater for a multi-cell selection) it would be simpler for you to use Data Validation. You could set up lists that could apply to specific cells

Debra Dalgleish has an excellent discussion on Data Validation at Contexturesenter image description here

羁拥 2024-12-25 11:52:45

基于您原始方法的粗略代码:

Sub DownOne()
   CycleValue Selection, -1
End Sub
Sub UpOne()
   CycleValue Selection, 1
End Sub

Sub CycleValue(rng As Range, GoDir As Integer)

    Dim arrA, arrB
    Dim arrVals, val
    Dim c As Range, m, indx As Integer
    Dim lb As Integer, ub As Integer
    Dim ProcessThis As Boolean

    arrA = Array("Fail", "Pass", "")
    arrB = Array("Eggs", "Bacon", "Toast", "Beans", "")

    For Each c In rng.Cells
        ProcessThis = True

        'what values are we cycling through?
        'based on column position
        If c.Column = 1 Then
            arrVals = arrA
        ElseIf c.Column = 5 Then
            arrVals = arrB
        Else
            ProcessThis = False 'not checking this column
        End If

        If ProcessThis Then
            lb = LBound(arrVals)
            ub = UBound(arrVals)
            val = Trim(c.Value)
            m = Application.Match(val, arrVals, 0)

            If IsError(m) Then
                indx = lb
            Else
                m = m - 1 '1-based
                indx = m + GoDir
                If indx < lb Then indx = ub
                If indx > ub Then indx = lb
            End If
            c.Value = arrVals(indx)
         End If
    Next c
End Sub

Rough code based on your original approach:

Sub DownOne()
   CycleValue Selection, -1
End Sub
Sub UpOne()
   CycleValue Selection, 1
End Sub

Sub CycleValue(rng As Range, GoDir As Integer)

    Dim arrA, arrB
    Dim arrVals, val
    Dim c As Range, m, indx As Integer
    Dim lb As Integer, ub As Integer
    Dim ProcessThis As Boolean

    arrA = Array("Fail", "Pass", "")
    arrB = Array("Eggs", "Bacon", "Toast", "Beans", "")

    For Each c In rng.Cells
        ProcessThis = True

        'what values are we cycling through?
        'based on column position
        If c.Column = 1 Then
            arrVals = arrA
        ElseIf c.Column = 5 Then
            arrVals = arrB
        Else
            ProcessThis = False 'not checking this column
        End If

        If ProcessThis Then
            lb = LBound(arrVals)
            ub = UBound(arrVals)
            val = Trim(c.Value)
            m = Application.Match(val, arrVals, 0)

            If IsError(m) Then
                indx = lb
            Else
                m = m - 1 '1-based
                indx = m + GoDir
                If indx < lb Then indx = ub
                If indx > ub Then indx = lb
            End If
            c.Value = arrVals(indx)
         End If
    Next c
End Sub
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文