vba 在单元格中输入数据后偏移到不同的列
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28370744/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Offset to different column after entering data in cell
提问by Doug
I have two tables. Both tables are the same size, located in columns I:X and AB:AQ (offset by 19 columns start-to-start).
我有两张桌子。两个表的大小相同,位于 I:X 和 AB:AQ 列(从开始到开始偏移 19 列)。
I want when I enter a number in a cell within the left table, to go 19 columns to the right after pressing enter. Then, once entering text into the active cell in the right table, return 19 cells to the left table.
我想当我在左表的单元格中输入一个数字时,按 Enter 键后向右移动 19 列。然后,在右表的活动单元格中输入文本后,将 19 个单元格返回到左表。
I found this question/answer, but I wasn't able to apply it.
我找到了这个问题/答案,但我无法应用它。
Private Sub Worksheet_Change(ByVal Target As Range)
On Error GoTo Whoa
Application.EnableEvents = False
If Not Target.Cells.CountLarge > 1 Then
If Not Intersect(Target, Columns(9)) Is Nothing Then
Target.Offset(, 19).Select
ElseIf Not Intersect(Target, Columns(10)) Is Nothing Then
Target.Offset(, 19).Select
ElseIf Not Intersect(Target, Columns(11)) Is Nothing Then
Target.Offset(, 19).Select
ElseIf Not Intersect(Target, Columns(12)) Is Nothing Then
Target.Offset(, 19).Select
'etc...
ElseIf Not Intersect(Target, Columns(42)) Is Nothing Then
Target.Offset(, -19).Select
ElseIf Not Intersect(Target, Columns(43)) Is Nothing Then
Target.Offset(, -19).Select
End If
End If
Letscontinue:
Application.EnableEvents = True
Exit Sub
Whoa:
MsgBox Err.Description
Resume Letscontinue
End Sub
As an alternative, if it can recognize that the cell entered is within columns I:X and just offset 19 columns after pressing enter (and then the reverse), this would be fine.
作为替代方案,如果它可以识别输入的单元格在 I:X 列内,并且在按下 Enter 键后仅偏移 19 列(然后反之),那就没问题了。
A second alternative, if it is easier, changing the function of the Page Upand Page Downkeys, I would not need to check which column I am in.
第二种选择,如果更容易,更改Page Up和Page Down键的功能,我不需要检查我在哪一列。
Lastly, this should only work on a particular sheet Tracking.
最后,这应该只适用于特定的工作表Tracking。
回答by tigeravatar
Something like this should work for you. Make sure the code is in the 'Tracking' worksheet code module:
像这样的事情应该适合你。确保代码在“跟踪”工作表代码模块中:
And here is the code you would use:
这是您将使用的代码:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim rngChange1 As Range
Dim rngChange2 As Range
Set rngChange1 = Range("I:X")
Set rngChange2 = Range("AB:AQ")
Application.EnableEvents = False
If Not Intersect(rngChange1, Target) Is Nothing Then
Target.Offset(, 19).Value = Target.Value
End If
If Not Intersect(rngChange2, Target) Is Nothing Then
Target.Offset(, -19).Value = Target.Value
End If
Application.EnableEvents = True
End Sub