VBA excel目标。地址=单元格范围
我有代码做两件事:首先,它将数据验证下拉列表中的项目排序,该列表位于表2中,带有“,”和表1中所需的单元格范围。此外,如果用户选择了相同的项,则会将其从所选单元格中删除 代码的另一个选项是当用户选择下拉列表(位于VBA excel目标。地址=单元格范围,excel,vba,Excel,Vba,我有代码做两件事:首先,它将数据验证下拉列表中的项目排序,该列表位于表2中,带有“,”和表1中所需的单元格范围。此外,如果用户选择了相同的项,则会将其从所选单元格中删除 代码的另一个选项是当用户选择下拉列表(位于D2:F325中)的单元格时,它应该放大100%以查看列表上的项目(因为其字体太小,无法查看) 在下面的代码中,它几乎可以完美地工作。因为,它仅在我从所需范围中选择单个单元格时才会缩放: Option Explicit Private Sub Worksheet_SelectionCh
D2:F325
中)的单元格时,它应该放大100%以查看列表上的项目(因为其字体太小,无法查看)
在下面的代码中,它几乎可以完美地工作。因为,它仅在我从所需范围中选择单个单元格时才会缩放:
Option Explicit
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Target.Count > 1 Then GoTo exitHandler
If Target.Address = Range("XYZ").Address Then
ActiveWindow.Zoom = 100
[A5000] = "zoomed"
ElseIf [A5000] = "zoomed" Then
'Otherwise set the zoom to original
ActiveWindow.Zoom = 70
[A5000].ClearContents
End If
exitHandler:
Application.EnableEvents = True
End Sub
Private Sub Worksheet_Change(ByVal Target As Range)
Dim rngDV As Range
Dim oldVal As String
Dim newVal As String
Dim strVal As String
Dim i As Long
Dim lCount As Long
Dim Ar As Variant
On Error Resume Next
Dim lType As Long
If Target.Count > 1 Then GoTo exitHandler
lType = Target.Validation.Type
If lType = 3 Then
Application.EnableEvents = False
newVal = Target.Value
Application.Undo
oldVal = Target.Value
Target.Value = newVal
If oldVal = "" Then
'do nothing
Else
If newVal = "" Then
'do nothing
Else
On Error Resume Next
Ar = Split(oldVal, ", ")
strVal = ""
For i = LBound(Ar) To UBound(Ar)
Debug.Print strVal
Debug.Print CStr(Ar(i))
If newVal = CStr(Ar(i)) Then
'do not include this item
strVal = strVal
lCount = 1
Else
strVal = strVal & CStr(Ar(i)) & ", "
End If
Next i
If lCount > 0 Then
Target.Value = Left(strVal, Len(strVal) - 2)
Else
Target.Value = strVal & newVal
End If
End If
End If
End If
exitHandler:
Application.EnableEvents = True
End Sub
XYZ
是单元格D2
的名称,因为我试图用此函数命名要选择的范围,但它不起作用
最后,如何Target.Address
选择整个范围D2:F325
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Target.Count > 1 Then GoTo exitHandler
If Not Application.Intersect(Target, Range("D2:F325")) Is Nothing Then
ActiveWindow.Zoom = 100
[A5000] = "zoomed"
ElseIf [A5000] = "zoomed" Then
'Otherwise set the zoom to original
ActiveWindow.Zoom = 70
[A5000].ClearContents
End If
exitHandler:
Application.EnableEvents = True
End Sub
它工作得很好。如果Target.Count>1,则代码开头有这一行,然后转到exitHandler,如果您选择了多个单元格,则退出您的
子单元
-如果答案对您有效,您能接受您的答案吗?(“社区”将它撞到了行的前面,因为它似乎未解决。)