Excel VBA - Find & Replace using Userform

Rokine

New Member
Joined
Jun 28, 2022
Messages
11
Office Version
  1. 2016
Platform
  1. Windows
Please I have a Userform with combobox1, combobox2 and Textbox1, and the associated Sheet is "sample". Tabulated data in the Sheet is shown below:
A B C R
1 AB001 T21 25
2 AB001 T22 25
3 AB003 T23 20
4 AB003 T24 20
5 AB003 T25 20
6 AB002 T26 15
7 AB001 T27 25
8 AB003 T28 20

At a click of a button, I want to compare combobox1 value (string) with values in column B, and a match is found, those values (in column B) should be replaced with combobox2 value and the corresponding rows in column R should be replaced with Textbox1 value. For example, given that combobox1.Value = "AB003", combobox2.Value = "AB005" and Textbox1.Value = 32, then
1. Replace Rows 3, 4, 5 & 8 of column B with "AB005"
2. Replace Rows 3, 4, 5 & 8 of column R with 32

Please your help will be greatly appreciated.

I have tried the code below and it is not working for me:
Dim Lastrow As Long, ws As Worksheet
Set ws = Worksheets("sample")
Lastrow = ws.Cells(Rows.Count, "A").End(xlUp).Row
For i = 2 To Lastrow
If Not IsError(Application.Match(Trim(Me.Combobox1.Value), ws.Range("B:B"), 0)) Then
ws.Cells(i, "B").Value = Me.Combobox2.Value.Value
ws.Cells(i, "R").Value = Me.Textbox1.Value
End If

Next i
 

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December

Forum statistics

Threads
1,224,820
Messages
6,181,159
Members
453,021
Latest member
Justyna P

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