Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ionic-framework/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel VBA从工作表更改中的目标获取列号_Excel_Vba_Worksheet Function - Fatal编程技术网

Excel VBA从工作表更改中的目标获取列号

Excel VBA从工作表更改中的目标获取列号,excel,vba,worksheet-function,Excel,Vba,Worksheet Function,我有一个需要通过工作表更改功能填写的值表。 我想做的是根据目标所在的位置更改B-G列中的单元格 Private Sub Worksheet_Change(ByVal Target As Range) If (Not Intersect(Target, Range(Cells(12, 2), Cells(14, 7))) Is Nothing) Then Cells(16,Application.WorksheetFunction.Column(Target))="Hello" End I

我有一个需要通过工作表更改功能填写的值表。 我想做的是根据目标所在的位置更改B-G列中的单元格

Private Sub Worksheet_Change(ByVal Target As Range)
If (Not Intersect(Target, Range(Cells(12, 2), Cells(14, 7))) Is Nothing) Then
    Cells(16,Application.WorksheetFunction.Column(Target))="Hello"
End If
End Sub

我在同一工作表\u change sub中有类似的代码位,当我使用
Target.Offset(1,0)
时,这些代码位可以正常工作,但由于我可能的目标范围在1行以上,我不知道如何使它始终是第16行和目标的同一列……

您需要处理目标不止是一个单元格的情况,并禁用事件处理,以便在更改工作表上的值时,工作表不会试图在其上运行

这将把“hello”放在B:G中任何发生变化的单元格右侧的单元格中;基本上,您将向Target中每个单元格的关联行上的C:H列添加“hello”

Private Sub Worksheet_Change(ByVal Target As Range)

    if not intersect(target, Range(Cells(12, "B"), Cells(14, "G"))) is nothing then
        on error goto safe_exit
        application.enableevents = false
        dim t as range 
        for each t in intersect(target, Range(Cells(12, "B"), Cells(14, "G")))
            t.Offset(1,0) = "hello"
        next t
    End If

safe_exit:
    application.enableevents = true
End Sub

Target.Column
为您提供列号(用于目标中的第一个单元格)。不过,你应该考虑@Jeeped下面的建议。像往常一样注意。我认为你应该从你的IPad上获得额外的积分???实际上是一个surface pro,但我(老实说)只用一只手打字和鼠标。