Messagebox after userform textbox match

basbergie

New Member
Joined
Jul 15, 2021
Messages
5
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
Good afternoon,

I'm looking for a vba code that based on a value of Textbox4 in a userform, looks for a match on Sheet2 Column A and then returns a messagebox with a comment that is given next to it on Column B.

If there is no comment, he does not have to give a message box
 

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
Hi and welcome to MrExcel

Try this:

VBA Code:
Private Sub CommandButton1_Click()
  Dim f As Range
  
  Set f = Sheets("Sheet2").Range("A:A").Find(TextBox4.Value, , xlValues, xlWhole, , , False)
  If Not f Is Nothing Then
    If Not f.Offset(, 1).Comment Is Nothing Then
      MsgBox "Comment: " & f.Offset(, 1).Comment.Text
    End If
  End If
End Sub
 
Upvote 0
I tried that code but when i push the button nothing happens.
 
Upvote 0
I tried also:
VBA Code:
Private Sub CommandButton4_Click()
Dim F As Range
  
  Set F = Sheets("Sheet2").Range("A:A").Find(TextBox4.Value, , xlValues, xlWhole, , , False)
  If Not F Is Nothing Then
    If Not F.Offset(, 1).Comment Is Nothing Then
      MsgBox "test"
    End If
  End If
End Sub

That doesn't work either so it has nothing to do with the message section
 
Upvote 0
maybe it could be much simpler. but I have now managed to select the correct cell based on the textbox, but now I want the selected cell to appear as messagebox.
What code should I use for that?

VBA Code:
Private Sub CommandButton4_Click()
Dim ws As Worksheet
Dim Rng As Range
Set ws = Sheets("Sheet2")   'Change the sheet name as per requirement
Set Rng = ws.Range("A:A").Find(Me.Textbox4.Value)
If Not Rng Is Nothing Then
    ws.Activate: 'this line makes sure that you are on the right sheet
    Rng.Select
    ActiveCell.Offset(0, 1).Select

    
    End If
End Sub
 
Upvote 0
ow wow that was simpler then i thought, it was:
MsgBox ActiveCell.Value
but now i need a code when theactive cell is empty then i dont need the messagebox :)
 
Upvote 0
Go to the sheet, activate the cell, activate the other cell, it does not seem to me to be simpler.

returns a messagebox with a comment
What do you mean by "comment", a "some text" in the cell or the comments that you can insert in the box comment:

1626441607061.png


If you mean some text, and I think it is, then use the following:

VBA Code:
Private Sub CommandButton1_Click()
  Dim f As Range
  Set f = Sheets("Sheet2").Range("A:A").Find(TextBox4.Value, , xlValues, xlWhole, , , False)
  If Not f Is Nothing Then
    If f.Offset(0, 1) <> "" Then MsgBox "test"
  End If
End Sub

Note:
Set f = Sheets("Sheet2").Range("A:A").Find(TextBox4.Value, , xlValues, xlWhole, , , False)
xlWhole in Find method, it means match must be exact, if you want partial search then change to xlPart
 
Upvote 0

Forum statistics

Threads
1,215,676
Messages
6,126,173
Members
449,296
Latest member
tinneytwin

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