Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/14.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
使用vba根据所选菜单在excel上自动填充特定列_Vba_Excel - Fatal编程技术网

使用vba根据所选菜单在excel上自动填充特定列

使用vba根据所选菜单在excel上自动填充特定列,vba,excel,Vba,Excel,我正在尝试根据excel上第一列的选定下拉选项自动填充下一列 下面是我认为的初始代码示例,但我的方法似乎不正确 Private Sub WorksheetStore_Change(ByVal Target As Range) Dim i As Integer Dim intCol As Integer intCol = shtStoreGroup.Range("A") If Not IsEmpty(Target.value) And intCol > 1

我正在尝试根据excel上第一列的选定下拉选项自动填充下一列

下面是我认为的初始代码示例,但我的方法似乎不正确

Private Sub WorksheetStore_Change(ByVal Target As Range)
    Dim i As Integer
    Dim intCol As Integer

    intCol = shtStoreGroup.Range("A")
    If Not IsEmpty(Target.value) And intCol > 1 And Target.Columns.Count = 1 And Target.Column = intCol And Target.Row > Start_Row Then
        For i = Target.Row To Target.Row + Target.Rows.Count - 1
            If shtStoreGroup.Columns(intCol).Rows(i).value = "Create" Then
                shtStoreGroup.Columns(intCol + 2).Rows(i).value = "N/A"
                shtStoreGroup.Columns(intCol + 3).Rows(i).value = "Test"
        Next i
    End If
End Sub

也许你在追求这个:

Private Sub Worksheet_Change(ByVal target As Range)

    If Not ValidateTarget(target, 2) Then Exit Sub '<-- exit if function validating 'Target' returns 'False'

    On Error GoTo EXITSUB ' <-- be sure to handle possible errors properly
    Application.EnableEvents = False '<--| disable events handling not to run this sub on top of itself
    Select Case UCase(target.Value)
        Case "CREATE"
            target.Offset(, 2).Value = "N/A"
            target.Offset(, 3).Value = "Test"
        Case "DELETE"
            target.Offset(, 2).Value = "???"
            target.Offset(, 3).Value = "Test??"
    End Select

EXITSUB:
    Application.EnableEvents = True '<--| restore events handling
End Sub

Function ValidateTarget(target As Range, Start_Row As Long) As Boolean
    With target
        If .columns.Count > 1 Then Exit Function
        If .Column <> 1 Then Exit Function
        If .Row <= Start_Row Then Exit Function
        If IsEmpty(.Value) Then Exit Function
        ValidateTarget = True
    End With
End Function
Private子工作表\u更改(ByVal目标作为范围)

如果没有ValidateTarget(目标,2),则退出Sub',我认为您不需要工作表_change()函数

使用名为cbTest的组合框,我会像

Option Explicit
Sub fill()

    cbTest.AddItem ("Value1")
    cbTest.AddItem ("Value2")

End Sub

Private Sub cbTest_Change()
    Select Case cbTest.Value
    Case "Value1"
        Cells(1, 1).Value = "Test"
    Case "Value2"
        Cells(1, 2).Value = "Test2"
    End Select
End Sub

我想“WorksheetStore”是您的工作表的名称。但是当您的组合框值发生变化时,您应该做一些事情。e、 g.:
私有子ComboxName_Change()。。。End Sub
或者当它失去焦点时:
私有Sub ComboxName\u LostFocus()。。。End Sub
您可能需要添加更多有关涉及多少张工作表、工作表名称、开始位置以及触发位置的详细信息,…@Tretom实际上工作表名称是shtStoreGroup。我只是想我需要定义子例程“WorksheetStore_Change”,每当菜单下拉菜单上有操作/更改时,它都会执行。@user3598756这是唯一的工作表。目标是,如果在列的下拉菜单上选择“创建”,则将有特定的值填充下一列(例如存储组ID=N/A、组名=MyGroup、描述=Test Group等)。如果我选择删除,情况也是如此