仅对某一特定列使用宏代码
我对 Excel 编程还是个新手,对代码进行细微更改时遇到了麻烦。我的代码使用户能够选择一个单元格,然后使用向上和向下箭头来选择定义的值。查看下面的代码,我在工作表中调用函数“UpOne”和“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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
与每次选择单元格时禁用/启用代码(另外,您需要满足多单元格选择的要求)相比,使用数据验证会更简单。您可以设置适用于特定单元格的列表。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 Contextures
基于您原始方法的粗略代码:
Rough code based on your original approach: