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?
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
btw: I also added a button and code to search by Sub Initials in column D. This also works fine.
 
Upvote 0
Are the values in col B text or actual numbers?
 
Upvote 0
They are formatted "General" the same as the Sub Name column.
 
Upvote 0
But was is in the cell? what does
Excel Formula:
=isnumber(B2)
return?
 
Upvote 0
OK, when I put '1119 in B1 instead of just 1119 the search finds it.
 
Upvote 0
When I switch the search column from 2 to 3 it works (see below). The issue with that is that there's a formula in column 3 that trims column 2 down to just a 4-digit number. Some SubCodes have an important "M" that follows the 4 digits. What I would need to happen if I search for 1119M with just 1119 is that the return would display correctly as 1119M. Right now if I search the SubCodes for 1119 I get back 1119. No "M". If the user clicks "Update Record" at this point the "M" is now gone. Make sense?

VBA Code:
Private Sub cmdSearchSubCode_Click()
Dim SubCode_id As String
SubCode = Trim(SubCode.Text)
lastrow = Worksheets("DataCenter").Cells(Rows.Count, 3).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
 
Upvote 0
Is it possible to combine these in column 3?:

VBA Code:
=LEFT($B1,4)

...and

VBA Code:
="" & $B1
 
Upvote 0
Tried this in C1:

VBA Code:
=LEFT($B1,4*(""&$B1))

Got this error when I ran a search:

Run-time error '13':

Type mismatch
 
Last edited:
Upvote 0

Forum statistics

Threads
1,215,497
Messages
6,125,155
Members
449,208
Latest member
emmac

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