如何遍历不同列中的下拉菜单以及如何在单独列中显示结果

时间:2019-04-20 16:45:15

标签: excel vba

我目前有一个代码,可以循环浏览动态下拉菜单,并在单独的列中将不同的选择打印在自己的单元格中。但是,我想修改此代码以与多个下拉菜单一起使用。当前,当用户从相邻的下拉菜单中选择选项时,该值将覆盖先前的选择。我正在尝试创建一个循环,将每个下拉菜单中的选择放在自己的列中。任何帮助将不胜感激,因为我是VBA的新手。

我试图在Target.Row if语句下创建ActiveCell.Column函数,但是收到一个错误,提示需要变量。

Dim Oldvalue As String
Dim Newvalue As String
Application.EnableEvents = True
On Error GoTo Exitsub
If Target.Row = 11 Then
 'The following commented line causes variable error'
 'For ActiveCell.Column = 1 To 8'
  If Target.SpecialCells(xlCellTypeAllValidation) Is Nothing Then
    GoTo Exitsub
  Else: If Target.Value = "" Then GoTo Exitsub Else
    Application.EnableEvents = False
    Newvalue = Target.Value
    Application.Undo
    Oldvalue = Target.Value
      If Oldvalue = "" Then
        Target.Value = Newvalue
        Dim val As String
        val = ActiveCell.Value
        If j = 0 Then
            Cells(11, j + 9).Value = val
        End If
      Else
        If InStr(1, Oldvalue, Newvalue) = 0 Then
            Target.Value = Oldvalue & ", " & Newvalue
            Dim txt As String
            Dim i As Integer
            Dim FullName As Variant
            txt = ActiveCell.Value
            FullName = Split(txt, ",")
            For i = 0 To UBound(FullName)
                Cells(i + 11, 9).Value = FullName(i)
            Next i
      Else:
        Target.Value = Oldvalue

0 个答案:

没有答案