Find match from listbox item in different sheet and copy text to next column

KestutisTower

New Member
Joined
Jun 2, 2022
Messages
23
Office Version
  1. 2016
Platform
  1. Windows
Hello everybody,
I have a userform with listbox1 and textbox1 and comandbutton1.

Untitled-1.jpg

Listbox is populated by RowSource property with named range( in sheet1).its product names.
Sheet1
Book1
ABC
1IDProductPrice
21tv
32radio
42mp3
Sheet1

also, i have a sheet2 witch has same product names( but different order)
sheet2
Book1
AB
1ProductPrice
2tv
3radio
4mp3
Sheet1

what i need to do is... when i select an item in listbox1 and enter value in textbox1, and press on comandbutton1(Enter price), textbox1 value to be copied to column C in sheet1 maching row,
and sheet2 column B maching row.
Untitled-2.jpg
Untitled-3.jpg

I can get first bit working with this code:
VBA Code:
Private Sub Comandbutton1_Click()
 
Dim sh As Worksheet
    Set sh = ThisWorkbook.Worksheets("Sheet1")
Dim lRw As Long
lRw = lstEile.ListIndex + 1

  With sh      
  
    .Cells(lRw, 3).Value = textbox1.Value
      
  End With
  textbox1.Value = ""
  
End Sub
but I can't get the second bit going. Please help!!
 

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
Hi,

I've added a few lines of code - not tested it myself, but see if it gets you close ?

Cheers
Rob
VBA Code:
Private Sub Comandbutton1_Click()
 
Dim sh, sh2 As Worksheet
Dim lRw As Long
Dim Rng As Range

Set sh = ThisWorkbook.Worksheets("Sheet1")
Set sh2 = ThisWorkbook.Worksheets("Sheet2")
    

lRw = lstEile.ListIndex + 1

Product = sh.Range("B" & lRw).Value

  With sh
  
    .Cells(lRw, 3).Value = textbox1.Value
      
  End With
  textbox1.Value = ""
  
  
  Set Rng = Sheet2.Columns("A:A").Find(What:=Product, _
    LookIn:=xlFormulas, LookAt:=xlWhole, SearchOrder:=xlByRows, _
    SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False)

If Rng Is Nothing Then Exit Sub  'exit if no data found
    
    With sh2
    
        .Cells(Rng.Row, 2).Value = textbox1.Value
  
    End With
  
End Sub
 
Upvote 0
Solution
Hi,

I've added a few lines of code - not tested it myself, but see if it gets you close ?

Cheers
Rob
VBA Code:
Private Sub Comandbutton1_Click()
 
Dim sh, sh2 As Worksheet
Dim lRw As Long
Dim Rng As Range

Set sh = ThisWorkbook.Worksheets("Sheet1")
Set sh2 = ThisWorkbook.Worksheets("Sheet2")
   

lRw = lstEile.ListIndex + 1

Product = sh.Range("B" & lRw).Value

  With sh
 
    .Cells(lRw, 3).Value = textbox1.Value
     
  End With
  textbox1.Value = ""
 
 
  Set Rng = Sheet2.Columns("A:A").Find(What:=Product, _
    LookIn:=xlFormulas, LookAt:=xlWhole, SearchOrder:=xlByRows, _
    SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False)

If Rng Is Nothing Then Exit Sub  'exit if no data found
   
    With sh2
   
        .Cells(Rng.Row, 2).Value = textbox1.Value
 
    End With
 
End Sub
Thank you RobP for quick response. The code worked perfectly. Just had to change
VBA Code:
Set Rng = Sheet2.Column....
to
VBA Code:
Set Rng = sh2.Column...
, because it required for object. Otherwise perfect fit😁. Thanks again.
 
Upvote 0
Thats great .. sorry for my Typo :) Glad to have been able to help.

cheers
Rob
 
Upvote 0

Forum statistics

Threads
1,214,590
Messages
6,120,423
Members
448,961
Latest member
nzskater

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