Results 1 to 8 of 8

VBA update search find routine using userform listbox selection

Threaded View

  1. #1
    Forum Contributor
    Join Date
    02-25-2013
    Location
    Colorado, US
    MS-Off Ver
    Microsoft 365 Apps
    Posts
    518

    VBA update search find routine using userform listbox selection

    So someone recently helped me with implementing a way to manage data updates using a sheet change event within a lookuplist sheet and it works great...however only if the user updates values on the sheet directly. The goal is this list will be hidden and controlled using only forms allowing the user to work within the forms regardless of which sheet is selected. Many thanks to mart37 for the code:
    Private Sub Worksheet_Change(ByVal Target As Range)
    
    Dim NewValue As Variant
    Dim OldValue As Variant
    Dim SearchRange As Range
    Dim Found As Variant
    Dim FirstAddress As Variant
    Dim CountReplaces As Integer
    
    If Intersect(Target, ActiveSheet.Columns(7)) Is Nothing Then Exit Sub
    If IsEmpty(Target) Then
        MsgBox "Cell is empty!"  '\\I don't know if I really need this
        Exit Sub
    End If
    On Error GoTo Err:
    Application.EnableEvents = False
    NewValue = Target
    Application.Undo
    OldValue = Target
    Target = NewValue
    Set SearchRange = Sheets(7).Range(Sheets(7).Cells(2, 2), Sheets(7).Cells(Rows.Count, 1).End(xlUp).Address)
    With SearchRange
        Set Found = .Find(OldValue, LookIn:=xlValues)
        If Not Found Is Nothing Then
            FirstAddress = Found.Address
            CountReplaces = 0
            Do
                Sheets(7).Range(Found.Address) = NewValue
                CountReplaces = CountReplaces + 1
                Set Found = .FindNext(Found)
                If Found Is Nothing Then GoTo DoneFinding
            Loop While Found.Address <> FirstAddress
    DoneFinding:
            If CountReplaces > 0 Then MsgBox ("This group was replaced" & CountReplaces & " times within the distribution list.")
        End If
    End With
    Err:
    Application.EnableEvents = True
    End Sub
    When a value is updated (or added) when not on the lookup sheet, the error occurs on:
    If Intersect(Target, Sheets(5).Columns(7)) Is Nothing Then Exit Sub
    I've changed ActiveSheet to Sheets(5) - but I'm not sure if I'm referencing the sheets correctly. Regardless, it doesn't seem to make a difference.
    Last edited by terriertrip; 09-07-2017 at 04:33 PM.

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] Form values not being added to worksheet-No errors
    By rob_h in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 06-01-2017, 06:08 AM
  2. PivotChart macro, to display all field values, errors out on last value
    By darkorder in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 06-10-2016, 09:02 PM
  3. [SOLVED] Functional VBA code errors out when included in Worksheet.Change event
    By aquixano in forum Excel Programming / VBA / Macros
    Replies: 18
    Last Post: 09-21-2015, 12:20 PM
  4. Macro to change cell values based on changes in another worksheet
    By joee0201 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 04-02-2014, 03:29 PM
  5. Combo Box Form Control w/macro to change pivot table filters - Getting Errors
    By Nyolls in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 04-29-2013, 05:37 PM
  6. Change colour of shape if there are any formula errors in worksheet
    By asha3010 in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 06-23-2010, 09:31 AM
  7. Code for button errors due to worksheet name change
    By ge0rge in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 10-15-2008, 12:22 PM

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