+ Reply to Thread
Results 1 to 2 of 2

Private Sub Worksheet_Change problem

Hybrid View

  1. #1
    Registered User
    Join Date
    06-17-2013
    Location
    Carbondale, Illinois
    MS-Off Ver
    Excel 2007
    Posts
    1

    Private Sub Worksheet_Change problem

    Hello all! First off I'd like to thank anyone for any help given. Here goes:

    I've got a workbook that has six worksheet templates within it. Each one has a different number of hidden columns. It is used for serial number input from a barcode scanner. The idea is that when you scan a bar code, it automatically duplicate checks against the rest of the entire workbook, but if that isn't possible at least against the rest of the worksheet. The first one that I tried, on the 2 column worksheet worked! This is what the code looked like:

    Private Sub Worksheet_Change(ByVal Target As Range)
        If Target.Column = 2 Then
            Target.Offset(0, 1).Select
        ElseIf Target.Column = 3 Then
            Target.Offset(1, -1).Select
                With Application
            .ScreenUpdating = False
            .EnableEvents = True
        End With
        If Not Application.Intersect(Range("C3:C2002"), Target) Is Nothing Then
                    ActiveSheet.Unprotect
            Rows(Target.Row + 1).Hidden = Target.Value <= 0
            ActiveSheet.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True
        End If
        With Application
            .ScreenUpdating = True
            .EnableEvents = True
        End With
        End If
    End Sub
    Private Sub Worksheet_Change2(ByVal Target As Range)
        If Not Intersect(Target, Target.Worksheet.Range("B3:xlCellTypeVisible")) Is Nothing Then Duplicate_Check2
    End Sub
    With Duplicate_Check2 being:

    Sub Duplicate_Check2()
        ActiveSheet.Unprotect
        ActiveSheet.Range("B3:C2002").Rows.SpecialCells(xlCellTypeVisible).Select
        Selection.FormatConditions.AddUniqueValues
        Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
        Selection.FormatConditions(1).DupeUnique = xlDuplicate
        With Selection.FormatConditions(1).Font
            .Color = -16383844
            .TintAndShade = 0
        End With
        With Selection.FormatConditions(1).Interior
            .PatternColorIndex = xlAutomatic
            .Color = 13551615
            .TintAndShade = 0
        End With
        Selection.FormatConditions(1).StopIfTrue = False
        ActiveSheet.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True
    End Sub
    However when I updated this and tried it for the three column template, it doesn't work. Here is the code for that:

    Private Sub Worksheet_Change(ByVal Target As Range)
        If Target.Column = 2 Then
            Target.Offset(0, 1).Select
        ElseIf Target.Column = 3 Then
            Target.Offset(0, 1).Select
        ElseIf Target.Column = 4 Then
            Target.Offset(1, -2).Select
                With Application
            .ScreenUpdating = False
            .EnableEvents = True
        End With
        If Not Application.Intersect(Range("D3:D2002"), Target) Is Nothing Then
                   ActiveSheet.Unprotect
            Rows(Target.Row + 1).Hidden = Target.Value <= 0
            ActiveSheet.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True
        End If
        With Application
            .ScreenUpdating = True
            .EnableEvents = True
        End With
        End If
    End Sub
    Private Sub Worksheet_Change2(ByVal Target As Range)
        If Not Intersect(Target, Target.Worksheet.Range("B3:xlCellTypeVisible")) Is Nothing Then Duplicate_Check3
    End Sub
    And Duplicate_Check3 being:

    Sub Duplicate_Check3()
        ActiveSheet.Unprotect
        ActiveSheet.Range("B3:D2002").Rows.SpecialCells(xlCellTypeVisible).Select
        Selection.FormatConditions.AddUniqueValues
        Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
        Selection.FormatConditions(1).DupeUnique = xlDuplicate
        With Selection.FormatConditions(1).Font
            .Color = -16383844
            .TintAndShade = 0
        End With
        With Selection.FormatConditions(1).Interior
            .PatternColorIndex = xlAutomatic
            .Color = 13551615
            .TintAndShade = 0
        End With
        Selection.FormatConditions(1).StopIfTrue = False
        ActiveSheet.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True
    End Sub
    What the heck am I doing wrong? It works perfectly for the two column sheet and not at all for the three column sheet. Any suggestions?

    Thank you for your time,
    -Robb-

  2. #2
    Forum Expert
    Join Date
    07-15-2012
    Location
    Leghorn, Italy
    MS-Off Ver
    Excel 2010
    Posts
    3,431

    Re: Private Sub Worksheet_Change problem

    Attach please a sample file with three column sheet
    If solved remember to mark Thread as solved

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1