+ Reply to Thread
Results 1 to 8 of 8

VBA update search find routine using userform listbox selection

Hybrid 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.

  2. #2
    Forum Expert
    Join Date
    12-14-2012
    Location
    London England
    MS-Off Ver
    MS 365 Office Suite.
    Posts
    8,448

    Re: Worksheet change errors when macro changes values

    The Change event macro only works on the host sheet.

    If you want to use the macro to change another sheet then use the change event macro to call another macro.

    The Second Macro should be in a normal macro module.

    You can delete the Message Box, if that line is not needed.
    Last edited by mehmetcik; 09-05-2017 at 07:19 PM.
    My General Rules if you want my help. Not aimed at any person in particular:

    1. Please Make Requests not demands, none of us get paid here.

    2. Check back on your post regularly. I will not return to a post after 4 days.
    If it is not important to you then it definitely is not important to me.

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

    Re: Worksheet change errors when macro changes values

    I understand that the change event only works on the host sheet. The change is indeed made to the host sheet from the sub routine. It's when a change to the host sheet occurs, values are updated within another database within another sheet. Like I say, it works, but only if the host sheet is active. I guess I don't understand why the sheet needs to be active for the sub to fire properly. Is there an alternative, such as something in the update macro to activate the host sheet, make the update (or add), then reactivate whatever sheet the change was made from? Something like:
    Sub update_click()
    application.screenupdating = false
    Set ws1 = activesheet
    Set ws2 = Sheets(5) 'the host sheet
    ws2.activate
    'run code
    Set ws = ws1 'whatever sheet macro was initiated from
    ws1.activate
    application.screenupdating = true
    end sub
    Could something like that work? Does it make any sense? Apologies for the simplicity, as I'm somewhat of a vba novice.

    UPDATE: so I ran the below code and it worked to update the host sheet without any error:
    Private Sub cmdUpdate_Click()
        
        Dim ws1 As Worksheet
        Set ws1 = ActiveSheet
        Dim ws2 As Worksheet
        Set ws2 = Worksheets(msSHEET_NAME)
    
        Application.ScreenUpdating = False
        'msg box code
        ws2.Activate
        'update code
        ws1.Activate
        Application.ScreenUpdating = True
        'more code
        End Sub
    The value on the host sheet changed via the macro. Why is the worksheet change event not recognizing the value change even when the macro is run while the host sheet is active? It works when I change the value on my keyboard, but not through the form.
    Last edited by terriertrip; 09-05-2017 at 08:08 PM.

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

    Re: Worksheet change errors when macro changes values

    Can anybody help me with this?

  5. #5
    Forum Contributor
    Join Date
    05-05-2009
    Location
    Wisconsin
    MS-Off Ver
    Excel 2010 and 365
    Posts
    113

    Re: Worksheet change errors when macro changes values

    Assuming that

    If Intersect(Target, ActiveSheet.Columns(7)) Is Nothing Then Exit Sub
    is the problem area then try removing ActiveSheet so that line becomes:

    If Intersect(Target, Columns(7)) Is Nothing Then Exit Sub

    Or try

    If Intersect(Target, Worksheets("Replace this with your worksheet name but leave quotes").Columns(7)) Is Nothing Then Exit Sub
    Last edited by Cyclops; 09-06-2017 at 04:59 PM.

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

    Re: Worksheet change errors when macro changes values

    That seems to work to solve the error. The worksheet change event does not fire when the change is made.

  7. #7
    Forum Contributor
    Join Date
    05-05-2009
    Location
    Wisconsin
    MS-Off Ver
    Excel 2010 and 365
    Posts
    113

    Re: Worksheet change errors when macro changes values

    Maybe the intersect is returning nothing?



    Change

    If Intersect(Target, Columns(7)) Is Nothing Then Exit Sub
    to

    If Intersect(Target, Columns(7)) Is Nothing Then 
         Exit Sub
    Else
         msgbox "Change not in column 7"
         msgbox target.address & " was changed"
    end if


    If you get the message boxes, it should clear things up. If you don't then it isn't triggering.

    If it's not triggering then I would make sure that events are enabled and that you are actually changing the cell. If it's a formula that references a cell on another sheet, changing the reference cell won't trigger the worksheet change event.

  8. #8
    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

    UPDATE: This is an update to my problem of updating a secondary data using an update to primary data value. Originally I was using a sheet update event, but decided that the easier solution would be to just put the code within the update routine. The title has been changed to reflect the issue. I thought maybe posting my solution may help others if they were ever so inclined to have the same issue. It is only for a command button, but I believe one can get the gist.

    The userform code allows updates to list of primary values (attributes) using listbox selection through text control. Range data are used to compile a secondary dataset within separate sheet, therefore can contain many copies of primary value within secondary dataset.

    When user updates original primary value through the listbox selection, dataset values compiled from the list are updated to reflect updated value. Below is final code.
    Private Sub cmdUpdate_Click()    
    
       Dim OldValue As String
       Dim NewValue As Variant
       Dim SearchRange As Range
       Dim Found As Variant
       Dim FirstAddress As Variant
       Dim CountReplaces As Integer
    
       Set ws1 = Worksheets(1)
       Set ws2 = Worksheets(2)
       NewValue = frmGroups.TextBox1
    
          With ws1
             RowCurrent = Me.ListBox1.ListIndex + 2
             OldValue = ws1.Cells(RowCurrent, 1).Value
             .Cells(RowCurrent, 1).Value = Me.TextBox1.Text
          End With
          Set SearchRange = ws2.Range(Cells(2, 1), 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
                ws2.Range(Found.Address) = NewValue
                CountReplaces = CountReplaces + 1
                Set Found = .FindNext(Found)
             If Found Is Nothing Then Exit Do
             Loop While Found.Address <> FirstAddress
             If CountReplaces > 0 Then MsgBox ("You've been replaced!")
             End If
           End With
        TextBox1.Text = ""
        LoadListBox
    End Sub
    Last edited by terriertrip; 09-07-2017 at 04:33 PM. Reason: simplified code

+ Reply to Thread

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