gpt4 book ai didi

vba - 将 VBA 宏应用于多个单元格

转载 作者:行者123 更新时间:2023-12-02 16:01:29 25 4
gpt4 key购买 nike

我想说我不是 VBA 专家。我有一个 VBA 代码,可将所选列表中的多个值附加到一个单元格中。该代码有效,但现在我想将该代码应用到多个单元格。

Option Explicit

Private Sub Worksheet_Change(ByVal Target As Range)
'Code by Sumit Bansal from https://trumpexcel.com
' To Select Multiple Items from a Drop Down List in Excel
Dim Oldvalue As String
Dim Newvalue As String
Application.EnableEvents = True
On Error GoTo Exitsub
If Target.Address = "$A$7" 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 & "," & Chr(10) & Newvalue
Else:
Target.Value = Oldvalue
End If
End If
End If
End If
Application.EnableEvents = True
Exitsub:
Application.EnableEvents = True
End Sub

如何扩展我的代码以使用单元格范围

$A$7:$A$18 

而不是“$A$7”,以便我可以将 VBA 代码应用到 Excel 中的多个单元格。

最佳答案

改变

If Target.Address = "$A$7" Then

If Not Intersect(Target, Range("A7:A18")) Is Nothing Then

关于vba - 将 VBA 宏应用于多个单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45960778/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com