Excel VBA-合并多个工作表选择更改

时间:2019-04-01 00:02:26

标签: excel vba

我是vba的新手,试图让我了解正在处理的电子表格。

我有两个分别工作的代码,但是很难将它们组合在一起。知道将它们组合在一起的最佳方法是什么吗?

第一个代码是当我单击第18列时打开一个对话框。

第二个代码是从下拉验证列表中选择多个选项。来自此链接:https://trumpexcel.com/select-multiple-items-drop-down-list-excel/

我也有兴趣将第二个代码应用于多个列,是否可以通过某种方式实现?

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    If Target.Column = 17 Then
        ActiveSheet.Shapes("Rectangle 3").Visible = True
        ActiveSheet.Shapes("Rectangle 2").Visible = True
        ActiveSheet.Shapes("Rectangle 2").TextFrame.Characters.Text = Range("A" & (ActiveCell.Row)) & " - " & Range("B" & (ActiveCell.Row))
        With ActiveSheet.Shapes("Rectangle 3")
            .Left = Target.Offset(, 1).Left
            .Top = Target.Offset(0).Top
        End With
        With ActiveSheet.Shapes("Rectangle 2")
            .Left = Target.Offset(0, 2).Left
            .Top = Target.Offset(0, 0).Top
        End With
    Else
        ActiveSheet.Shapes("Rectangle 3").Visible = False
        ActiveSheet.Shapes("Rectangle 2").Visible = False
    End If
End Sub
Private Sub Worksheet_Change(ByVal Target As Range)
Dim Oldvalue As String
Dim Newvalue As String
Application.EnableEvents = True
On Error GoTo Exitsub
If Target.Column = 12 Then
  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
      Else
        If InStr(1, Oldvalue, Newvalue) = 0 Then
            Target.Value = Oldvalue & ";" & Newvalue
      Else:
        Target.Value = Oldvalue
      End If
    End If
  End If
End If
Application.EnableEvents = True
Exitsub:
Application.EnableEvents = True
End Sub

在此先感谢您的帮助!

0 个答案:

没有答案