Code failing to place item at required location

kelly mort

Well-known Member
Joined
Apr 10, 2017
Messages
2,169
Office Version
  1. 2016
Platform
  1. Windows
Variables
Code:
Private fEnterValue As Variant
Private fNameID As Variant
Private fChangeValue As Variant
Private fSName As Variant


Textbox enter event
Code:
Private Sub freg2_Enter()
If freg3 <> "" Then fEnterValue = Trim(freg2.Text)
Select Case freg3.Text
    Case "X", "Y", "Z"
    For Each fSName In Sheet1.[AZ7:AZ407]
        If fSName = fEnterValue Then
            fNameID = fSName.Offset(0, -1)
            Exit For
        End If
    Next fSName
    
    Case Else
    For Each fSName In Sheet1.[C7:C407]
        If fSName = fEnterValue Then
            fNameID = fSName.Offset(0, -1)
            Exit For
        End If
    Next fSName
End Select
End Sub


Textbox change event
Code:
Private Sub freg2_Change()
    freg2 = UCase(freg2)
    If freg3 <> "" Then fChangeValue = Trim(freg2.Text)
End Sub


Command button click event
Code:
Private CmdCompare_Click()
    If Trim(fEnterValue) <> Trim(fChangeValue) Then
    Select Case freg3.Text
        Case "X", "Y", "Z"
            For Each fNameID In Sheet1 .[AY7:AY407]
                If fNameID.Offset(0, 1) = fEnterValue Then
                    fNameID.Offset(0, 1) = fChangeValue
                Exit For
                End If
            Next fNameID
    
        Case Else
            For Each fNameID In  Sheet1.[B7:B407]
                If fNameID.Offset(0, 1) = fEnterValue Then
                    fNameID.Offset(0, 1) = fChangeValue
                Exit For
                End If
            Next fNameID
    End Select
    End If
End Sub


This code is placing the changed name under the last item in the database instead of placing it in the place or location of where the item (before changed) is located actually.


For example, say I have “Blue Cats” and I change it to “Blue cars”, it places the “Blue Cars” under the last item in the column while having the “Blue cats” at its original place. Meanwhile I want to have the “Blue Cats” replaced with the “Blue Cars”. I have tried all the tricks I know so far and it’s freaking me out. Can someone pull me out?.
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.

Forum statistics

Threads
1,215,756
Messages
6,126,692
Members
449,331
Latest member
smckenzie2016

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top