Userform code not posting changes to col C(3) of sheet

chazrab

Well-known Member
Joined
Oct 21, 2006
Messages
780
Office Version
  1. 365
Platform
  1. Windows
Clicking on the Update button in the image below, this code only posts changes to Textbox1. For some reason it doesn't post any changes made to Textbox2(below Textbox1 on rhe userform.
Code:
Private Sub cmdUPDATE_Click()
Dim ws As Worksheet
Dim r As Variant
r = ListBox1.ListIndex '----> current selected item and(sheet row) of Listbox1
If r = 0 Then
r = r + 1
Else
End If
Set ws = Worksheets("THEEAST")
  With wst
    ws.Cells(r, 2).Value = Me.TextBox1.Text ---> rhis line posts code successully to col B(2) of the sheet
    ws.Cells(r, 3).Value = Me.TextBox2.Text '--->this line does not post changes to col C(3) for some reason
End With
End Sub
If I comment out the first ws line above, ( ws.Cells(r, 2).Value = Me.TextBox1.Text ),
the code runs the second ws line and changes are posted to col C(3) which is Textbox2.
I've used this same code successfully many times, with textboxes on a userform, but NOT with a listbox. It's as if something quirky is going on
I should be able to make changes to BOTH textboxes on the form and have them reflected back to the sheet in their proper cols.

Can someone please help me figure out what's happening and not happening here?
Thanks for anyone's help. I'm sure its something simple. This has worked many times.
cr
 

Attachments

  • USERFORM AND UNDERLYING SHEET.jpg
    USERFORM AND UNDERLYING SHEET.jpg
    212.3 KB · Views: 9

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
Try this:

VBA Code:
Private Sub cmdUPDATE_Click()
  Dim r As Long
  Dim sh As Worksheet
  
  If ListBox1.ListIndex = -1 Then
    MsgBox "Select an item from listbox"
    Exit Sub
  End If
  r = ListBox1.ListIndex + 1
  Set sh = Sheets("THEEAST")
  sh.Range("B" & r).Value = TextBox1.Value
  sh.Range("C" & r).Value = TextBox2.Value
End Sub
 
Upvote 0
Hi Dante. Copied your code exactly as above. Still not posting to Textbox2. See new images below and test wkbk. Seems like
the code for Textbox1 entry is blocking updating Textbox2. Never had this happen b4.

Thanks for helping.
cr

 

Attachments

  • ENTRIES.jpg
    ENTRIES.jpg
    58.8 KB · Views: 6
  • CODE TEST.jpg
    CODE TEST.jpg
    75.2 KB · Views: 6
  • CLICKED ON UPFATE SHEET BUTTON.  NO CHANGE IN TEXTBOX2..jpg
    CLICKED ON UPFATE SHEET BUTTON. NO CHANGE IN TEXTBOX2..jpg
    65 KB · Views: 6
Upvote 0
I downloaded your TESTWKBK file, but I can't open it, because of your file version.

You can put ALL your code here.
 
Upvote 0
Code:
[B]code for Listbox1:[/B]
Private Sub ListBox1_Click()
Me.TextBox1.Value = Me.ListBox1.List(Me.ListBox1.ListIndex, 1)
Me.TextBox2.Value = Me.ListBox1.List(Me.ListBox1.ListIndex, 2)
Me.TextBox3.Value = ListBox1.ListIndex
Me.TextBox1.SetFocus
Me.TextBox1.SelStart = 0
End Sub
[B]code for update button which is your code:[/B]
Private Sub cmdUPDATE_Click()
  Dim r As Long
  Dim sh As Worksheet
    If ListBox1.ListIndex = -1 Then
    MsgBox "Select an item from listbox"
    Exit Sub
  End If
  r = ListBox1.ListIndex + 1
  Set sh = Sheets("THEEAST")
  sh.Range("B" & r).Value = TextBox1.Value
  sh.Range("C" & r).Value = TextBox2.Value [B]----->this line is not updating values in col C on row r.  Changes not posted to the sheet.  It is left unchanged[/B]
End Sub
This is the entire code that performs sheet updating in cols B and C from  Listbox1 and two textboxes 1 and 2 on a userform.  
Any userform with this code and controls should work on a test sheet and give the exact same result.
Let me know what you find out.  Weird. 
cr
 
Upvote 0
Note: To highlight lines in the code use the Rich Code Tag:
1641858571350.png


Test the following:
Rich (BB code):
Private Sub cmdUPDATE_Click()
  Dim r As Long
  Dim sh As Worksheet
  
  If ListBox1.ListIndex = -1 Then
    MsgBox "Select an item from listbox"
    Exit Sub
  End If
  r = ListBox1.ListIndex + 1
  Set sh = Sheets("THEEAST")
  sh.Range("B" & r).Value = TextBox1.Value
  sh.Range("C" & r).Value = "Some text to try"
End Sub
 
Upvote 0
Do you have more code or some other macro running in the sheet events?

Try to do it in a new book, on a new sheet. create the userform, paste the code and try again.
 
Upvote 0
I tried your code and it works fine - I made a small change

Change Sheet name in bold

Rich (BB code):
Dim ws As Worksheet
Dim r As Variant
r = ListBox1.ListIndex '----> current selected item and(sheet row) of Listbox1
MsgBox ListBox1.ListIndex
r = r + 1

Set ws = Worksheets("Sheet1")
  With ws
    ws.Cells(r, 2).Value = Me.TextBox1.Text '---> rhis line posts code successully to col B(2) of the sheet
    ws.Cells(r, 3).Value = Me.TextBox2.Text '--->this line does not post changes to col C(3) for some reason
End With
 
Upvote 0
Dante, Mitchell, see this: "some text to try" will of course populate in Textbox2 because you assigned it that value in quotes as copied your code below.
Comment it out, as I did below, add Textbox2.value in front run the form, enter anything in Textbox1 and Textbox2, click the Update button and tell me if what you entered is
(1) displayed in both Textbox1 and Textbox2 on the form and (2) simultaneously gets entered into col C on the same row r.
If it works for you, then the sheet must be corrupted or something else not obvious is going on.

Here's what I did:
Code:
Private Sub cmdUPDATE_Click()
  Dim r As Long
  Dim sh As Worksheet
    If ListBox1.ListIndex = -1 Then
    MsgBox "Select an item from listbox"
    Exit Sub
  End If
  r = ListBox1.ListIndex + 1
  Set sh = Sheets("THEEAST")
  sh.Range("B" & r).Value = TextBox1.Value
  sh.Range("C" & r).Value = TextBox2.Value '"Some text to try"------->Textbox2.value
End Sub
images show new comments not posting to the sheet. I've included the new test workbook just in case you can open it.


cr
 

Attachments

  • ADDED COMMENTS B4 CLICKING UPDATE BUTTON. .jpg
    ADDED COMMENTS B4 CLICKING UPDATE BUTTON. .jpg
    84.9 KB · Views: 4
  • DATE ENTERED IN TEXTBOXES B4 CLICKING UPDATE BUTTON.jpg
    DATE ENTERED IN TEXTBOXES B4 CLICKING UPDATE BUTTON.jpg
    151 KB · Views: 4
Upvote 0

Forum statistics

Threads
1,203,600
Messages
6,056,204
Members
444,850
Latest member
dancasta7

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