2017-02-21 23 views
0

私は2つのことを行うコードがあります:まずシート2にあるデータ検証ドロップリストの項目を、 "、"でSheetにあるセルの希望の範囲にソートしますまた、ユーザが同じアイテムを選択すると、選択されたセルから削除されます。VBA ExcelをTarget.Address =セルの範囲

コードの他のオプションは、ユーザーがドロップダウンリストのセルを選択したとき(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.Adressは全範囲D2を選択することができますどのように:それはかなりうまく動作し、事前

+0

コードの先頭にこの行がある場合は 'If Target.Count> 1 Then GoTo exitHandler'、複数のセルを選択すると' Sub'が終了します –

答えて

0
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 

でF325

感謝。

関連する問題