创建偏移代码以使用选择更改运行 [英] creating offset code to run with selection change
问题描述
我有一台扫描仪可以扫描并切换到下一栏。 一旦它到达某个列(即列D),我需要它返回1行,返回到B列。 我认为处理这个的最好方法是
VB中的SelectionChange excel对象。 但是,我只是尝试使用偏移量来确保它能够正常工作,但事实并非如此。 我刚刚输入了简单的代码
I have a scanner that scans and tabs across to next column. Once it gets to a certain column (i.e. column D) I need it to return 1 row down, back to column B. I figured the best way to handle this would be the SelectionChange excel object in VB. However, I just attempted to just use the offset to make sure it would work correctly, and it does not. I just entered simple code of
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
ActiveCell.Offset(1,-1).Activate
ActiveCell.Offset(1,-1).Activate
End Sub
并且它会爆炸。 我尝试将它们都设为正数,它会跳过几百行和大约50列。 有什么指针吗? 我认为这是唯一一个自动运行我的VB而无需点击快捷键的coltrol,因为
他们正在扫描大量的信息而且重复性过高。 如果你也可以帮助识别有用的if语句来识别有用的活动列ID。
and it bombs out. I tried making them both positive number, and it will jump down a couple hundred rows and about 50 columns. Any pointers? I assumed this was the only coltrol to run my VB automatically without hitting a shortcut key, as they are scanning tons of information and that would be too repetitive. If you could also help with the if statement needed to identify active column ID that would help.
推荐答案
得到帮助并想出来。
Got help and figured it out.
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
如果Target.Column> = 4那么
Application.EnableEvents = False
Target.Offset(1,-2)。激活
Application.EnableEvents = True
结束如果
结束子
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Target.Column >= 4 Then
Application.EnableEvents = False
Target.Offset(1, -2).Activate
Application.EnableEvents = True
End If
End Sub
这篇关于创建偏移代码以使用选择更改运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!