For each loop getting tricky

kelly mort

Well-known Member
Joined
Apr 10, 2017
Messages
2,169
Office Version
  1. 2016
Platform
  1. Windows
I am using this code to capture two events “Enter” and “Change” textbox data. So when there is a change in both variables, I want to use the “Enter” variable to locate the ID of that item. The reason being that I will change that item with the “Change” variable. I observed I can’t change it since the location is same. So I tried using some of the tricks I know so far but the VBE is just laughing at me.


What is it that I am doing wrongly?


Thanks


Code:
Option Explicit
Dim EnterValue As Variant
Dim NameID As Variant
Dim ChangeValue As Variant
Dim SName As Variant


Private Sub Rw2_Change()
    Rw2 = UCase(Rw2)
    ChangeValue = Rw2.Text
End Sub
Private Sub Rw2_Enter()
    EnterValue = Rw2.Text
End Sub
Private Sub CmdCompareNameChange_Click()


With Sheet1
    If Trim(EnterValue) = Trim(ChangeValue) Then
        MsgBox "Match " & EnterValue & " , " & ChangeValue
    Else
        For Each SName In .[C4:C404]
            For Each NameID In .[B4:B404]
                If StName = EnterValue Then
                    NameID = SName.Offset(0, -1)
                    'NameID.Offset(0, 1) = ChangeValue
                    NameID.Offset(0, 1).Interior.ColorIndex = 37
                    MsgBox "Not Match " & EnterValue & " , " & ChangeValue & " , " '& NameID.Offset(0, 1).Value
                    Exit For
                End If
            Next NameID
        Next SName
    End If
End With
End Sub
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
Code:
Option Explicit
Dim EnterValue As Variant
Dim NameID As Variant
Dim ChangeValue As Variant
Dim SName As Variant


Private Sub Rw2_Change()
    Rw2 = UCase(Rw2)
    ChangeValue = Rw2.Text
End Sub


Private Sub Rw2_Enter()
EnterValue = Trim(Rw2.Text)
With Sheet1
    For Each SName In .[C4:C404]
        If SName = EnterValue Then
            NameID = SName.Offset(0, -1)
            Exit For
        End If
    Next SName
End With
End Sub


Private Sub CmdCompareNameChange_Click()
With Sheet1
    If Trim(EnterValue) = Trim(ChangeValue) Then
    
    Else
    
        For Each NameID In .[B4:B404]
        If NameID.Offset(0, 1) = EnterValue Then
            NameID.Offset(0, 1) = ChangeValue
            MsgBox "Not Match " & EnterValue & " , " & ChangeValue & " , " & NameID
            Exit For
        End If
        Next NameID
        
    End If
End With
End Sub


I just fixed it my way.

If there is a better way, I am willing to know.

Thanks
 
Upvote 0
Ok, I see that you've got it working :)
 
Upvote 0
You're certainly making progress & we've all been in that position.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,175
Members
453,021
Latest member
Justyna P

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