UserForm with 2 macros. One works and one doesn't!

jmpatrick

Active Member
Joined
Aug 17, 2016
Messages
477
Office Version
  1. 365
Platform
  1. Windows
Happy Wednesday!

I'm working with a UserForm that I created (called frmSubMaintenance) that pulls information from a sheet (called DataCenter) and allows me to edit and save the results back to DataCenter. There are two command buttons with rather simple code that do the work. Here's the first one. It works as designed:

VBA Code:
Private Sub cmdSearchSubName_Click()
Dim SubName_id As String
SubName = Trim(SubName.Text)
lastrow = Worksheets("DataCenter").Cells(Rows.Count, 5).End(xlUp).Row
For i = 1 To lastrow
If Worksheets("DataCenter").Cells(i, 5).Value = SubName Then
SubCode.Text = Worksheets("DataCenter").Cells(i, 2).Value
SubInitials.Text = Worksheets("DataCenter").Cells(i, 4).Value
FieldManager.Text = Worksheets("DataCenter").Cells(i, 7).Value
End If
Next
End Sub

Here's the second. Almost the same code but it doesn't do anything:

VBA Code:
Private Sub cmdSearchSubCode_Click()
Dim SubCode_id As String
SubCode = Trim(SubCode.Text)
lastrow = Worksheets("DataCenter").Cells(Rows.Count, 2).End(xlUp).Row
For i = 1 To lastrow
If Worksheets("DataCenter").Cells(i, 2).Value = SubCode Then
SubName.Text = Worksheets("DataCenter").Cells(i, 5).Value
SubInitials.Text = Worksheets("DataCenter").Cells(i, 4).Value
FieldManager.Text = Worksheets("DataCenter").Cells(i, 7).Value
End If
Next
End Sub

Here's what DataCenter looks like. Sub Name is in column E and Sub Code is in column B.

edit.png


When I use the cmdSearchSubName command button all the relevant information is pulled as desired.

When I use the cmdSearchSubCode command button nothing happens. No error no data.

Thoughts?
 
Success! I'm searching one column (the one with 4-digits) but returning the value from another column (the one that may also include the "M"):

VBA Code:
Private Sub cmdSearchSubCode_Click()
Dim SubCode_id As String
SubCode = Trim(SubCode.Text)
lastrow = Worksheets("DataCenter").Cells(Rows.Count, 2).End(xlUp).Row
For i = 1 To lastrow
If Worksheets("DataCenter").Cells(i, 3).Value = SubCode Then
SubName.Text = Worksheets("DataCenter").Cells(i, 5).Value
SubInitials.Text = Worksheets("DataCenter").Cells(i, 4).Value
FieldManager.Text = Worksheets("DataCenter").Cells(i, 7).Value
End If
Next
End Sub

I didn't have to change any formulas or anything else and It seems to work. I'll test it thoroughly and report back.
 
Upvote 0

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
The only issue I see is this:

If you search for SubCode 1112 it correctly returns 1112M (as shown). However, if you delete the M and click Search By Sub Code it stays at 1112. It should return 1112M.

edit 2.png
 
Upvote 0
As col B is numeric use
VBA Code:
If Worksheets("DataCenter").Cells(i, 2).Value = Val(SubCode) Then
 
Upvote 0
As col B is numeric use
VBA Code:
If Worksheets("DataCenter").Cells(i, 2).Value = Val(SubCode) Then

That line was changed to:

VBA Code:
If Worksheets("DataCenter").Cells(i, 3).Value = SubCode Then

That was so it would scan column 3 (always 4-digit number) but return column 2 (with the M if present).
 
Upvote 0
There is nothing in your code that is looking at col 2 anymore
 
Upvote 0
There is nothing in your code that is looking at col 2 anymore

I still have a lot to learn! I thought this part was:

VBA Code:
lastrow = Worksheets("DataCenter").Cells(Rows.Count, 2).End(xlUp).Row
 
Upvote 0
That's just working out where the last row is.
If you want the subcode returned you will need to add this
VBA Code:
SubCode.Text = Worksheets("DataCenter").Cells(i, 2).Value
 
Upvote 0
Solution
That's just working out where the last row is.
If you want the subcode returned you will need to add this
VBA Code:
SubCode.Text = Worksheets("DataCenter").Cells(i, 2).Value

That was it! Thanks, as always.
 
Upvote 0
So this was the code that works in case someone else is trying to do something similar:

VBA Code:
Private Sub cmdSearchSubCode_Click()
Dim SubCode_id As String
SubCode = Trim(SubCode.Text)
lastrow = Worksheets("DataCenter").Cells(Rows.Count, 2).End(xlUp).Row
For i = 1 To lastrow
If Worksheets("DataCenter").Cells(i, 3).Value = SubCode Then
SubCode.Text = Worksheets("DataCenter").Cells(i, 2).Value
SubName.Text = Worksheets("DataCenter").Cells(i, 5).Value
SubInitials.Text = Worksheets("DataCenter").Cells(i, 4).Value
FieldManager.Text = Worksheets("DataCenter").Cells(i, 7).Value
End If
Next
End Sub
 
Upvote 0
You're welcome & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,214,991
Messages
6,122,628
Members
449,095
Latest member
bsb1122

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