Simplify repetative userform code

Macklin

Board Regular
Joined
Jun 1, 2011
Messages
51
I have a userform with multiple buttons and textboxes. Each one needs to find the current row of the combobox value, so I use the same code in each sub. I have found how to declare the variable once. Is there a way to simplify the rest? Thank you for any help.

Code:
 MyVal = ComboBox1.Value
 
       With Sheets("Sheet1").Range("A:A")
         Set cell = .Find(MyVal, LookIn:=xlValues)
         rw = cell.row
       End With
   On Error GoTo 0
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
I guess I should clarify my question. The code is simple enough, but is there a way to achieve the same thing the code does without repeating it in every sub procedure?
 
Upvote 0
Hi Macklin,

Hope you are trying to retrive the row number. Hence define the code as a function and use the function whereever the code is used.

Code:
Function rw_check(rw)
    MyVal = UserForm1.ComboBox1.Text
 
       With Sheets("Sheet1").range("A:A")
         Set cell = .Find(MyVal, LookIn:=xlValues)
         rw = cell.Row
       End With
   On Error GoTo 0
End Function

Call the Function using "Call rw_check(roww)" whereever you use the code.

Hope this works!!

Cheers,
Dine
 
Upvote 0
Thank you dine, This makes sense to me. It will certainly clean up the code. Do you know or think it would be more efficient as well, or is it about the same?
 
Upvote 0
I hope there will not be much difference in time taken to run the macro, it can be considered efficient because it will reduce the number of lines in coding and if there is a huge data if will be more effective.

Cheers,
Dine
 
Upvote 0

Forum statistics

Threads
1,224,521
Messages
6,179,286
Members
452,902
Latest member
Knuddeluff

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