why this code doesn't work at all?

KalilMe

Active Member
Joined
Mar 5, 2021
Messages
343
Office Version
  1. 2016
Platform
  1. Windows
hi guys
I have this code but doesn't work at all , I try updating values in some columns based on filled values in multiple textboxes
VBA Code:
Private Sub CommandButton1_Click()
Dim sh As Worksheet
Dim sr As Range
Dim cl
Set sh = Sheets("sheet1")
c1 = Me.ComboBox1.Value
If c1 <> "" Then
 Set sr = ws.Columns(4).Find(c1, LookAt:=xlWhole)
 If Not c1 Is Nothing Then
 sh.Cells(sr.Row, "A") = Me.TextBox1.Value
 sh.Cells(sr.Row, "B") = Me.TextBox2.Value
 sh.Cells(sr.Row, "C") = Me.TextBox3.Value

 End If
End If
End Sub
can any body guide me where is the mistake ?
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
I suspect this:

Code:
Set sr = ws.Columns(4).Find(c1, LookAt:=xlWhole)

should be:

Rich (BB code):
Set sr = sh.Columns(4).Find(c1, LookAt:=xlWhole)
 
Upvote 0
nothing changes .this is the code
VBA Code:
Private Sub CommandButton2_Click()
Dim sh As Worksheet
Dim sr As Range
Dim cl
Set sh = Sheets("sheet1")
c1 = Me.ComboBox1.Value
If c1 <> "" Then
 Set sr = sh.Columns(4).Find(c1, LookAt:=xlWhole)
 If Not sr Is Nothing Then
 sh.Cells(sr.Row, "A") = Me.TextBox1.Value
 sh.Cells(sr.Row, "B") = Me.TextBox2.Value
 sh.Cells(sr.Row, "C") = Me.TextBox3.Value

 End If
End If

End Sub
 
Upvote 0
Step through the code and see what is happening. The most likely cause for no visible output would be if the Find isn't matching anything.
 
Upvote 0
Step through the code and see what is happening. The most likely cause for no visible output would be if the Find isn't matching anything.
if I use F8 it will run the userform I can't show the steps . what I suposse to do
 
Upvote 0
Put a breakpoint (f9) on the Set sh = ... line, then run your form, select an item in your list and press the button. You can then use f8 to step through the code and see which lines get executed.

I also suggest reading this: Debugging VBA
 
Upvote 0
it highlights in this line

VBA Code:
 Set sr = sh.Columns(4).Find(c1, LookAt:=xlWhole)

I would add another thing when I try updating value in textbox and press button .it clears new value and return the old value
 
Upvote 0
What is the error message? If there isn't one on that line, keep pressing f8 until you either get an error or you see what the code is missing.

There is nothing in that code to clear any other controls.
 
Upvote 0
it doesn't show any error .it passes for all lines without any error
 
Upvote 0

Forum statistics

Threads
1,215,839
Messages
6,127,208
Members
449,369
Latest member
JayHo

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