VBA Form Help

TDC21

Board Regular
Joined
Mar 31, 2017
Messages
97
I am trying to create form to update a table. The goal is to search by Template ID and add a MTR code in the corresponding row. It is a simple form but my VBA is pretty simple to. What I have is based on online samples that I have tried to modify to my data. With that said, not sure if I am missing something simple or if I am going about this entirely wrong.

If anyone can offer some advice it will be greatly appreciated.

Form has only 3 controls, text box "SearchTemplate", text box "Mtr", and a command button "CommandButton1"

My table data begins on row 8 and the column which contains the Template ID is "I", the column congaing the MTR is "J" . The code I have associated with the command button is as follows,

Code:
Private Sub CommandButton1_Click()


Dim searchRange As Range
Dim foundCell As Range
Dim mysearch As String


mysearch = Me.SearchTemplate.Value


With ThisWorkbook.Sheets("Sheet2")
    Set searchRange = .Range("I8", .Range("I" & .Rows.Count).End(xlUp))
End With


Set foundCell = searchRange.Find(what:=mysearch, Lookat:=xlWhole, MatchCase:=False, SearchFormat:=False)
If Not foundCell Is Nothing Then
     If foundCell.Offset(0, 1).Value = Me.SearchTemplate.Value Then
         foundCell.Offset(0, 2).Value = Me.Mtr.Value
     Else
         MsgBox "Name does not exist."
     End If
Else
     MsgBox "ID does not exist."
End If


End Sub
When I run the search it processes but it but returns the "Name does not exist." prompt.
 
Last edited by a moderator:

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
What is this line meant to be doing?
Code:
 If foundCell.Offset(0, 1).Value = Me.SearchTemplate.Value Then
At the moment you find the SearchTemplate value in col I & then you check if the same value is in col J on the same row.
 
Upvote 0
The offsets were the part I understood least but I was thinking that was a prompt to say if foundCell = the data entry in the SearchTemplate txt box then to set the value for txt box Mtr = the corosponding row as FoundCell in column J.

With that said, my VBA is beginner level and most of this code was copied and edited so it is probably more accurate to say that is what I was hoping that line was doing.
 
Upvote 0
Based on what you said I tried replacing with,


If foundCell = Me.SearchTemplate.Value Then
foundCell.Offset(0, 1).Value = Me.Mtr.Value


Still getting the Name dose not exist prompt
 
Upvote 0
Try
Code:
If Not FoundCell Is Nothing Then
   FoundCell.Offset(0, 2).Value = Me.Mtr.Value
Else
   MsgBox "ID does not exist."
End If
 
Upvote 0
Glad to help & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,215,562
Messages
6,125,546
Members
449,237
Latest member
Chase S

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