why doesn't my code copy Textbox2 value??

redspanna

Well-known Member
Joined
Jul 27, 2005
Messages
1,602
Office Version
  1. 365
Platform
  1. Windows
HI all,

I have this code that simply copies the values input in Textbox1 and Textbox2 on next available row in the 'songs' sheet

Code:
Private Sub CommandButton1_Click()
Dim iRow As Long
Dim ws As Worksheet
Set ws = Worksheets("songs")

'find first empty row in database
iRow = ws.Cells(Rows.Count, 2) _
  .End(xlUp).Offset(1, 0).Row

'check for a part number
If Trim(Me.TextBox1.Value) = "" Then
  Me.TextBox1.SetFocus
  MsgBox "Please enter Artist Name"
  Exit Sub
End If

'copy the data to the database

ws.Cells(iRow, 3).Value = Me.TextBox1.Value
ws.Cells(iRow, 2).Value = Me.TextBox2.Value

Me.TextBox1.Value = ""
Me.TextBox2.Value = ""
MsgBox "New Track added, thanks!"


End Sub

but the Textbox2 value isn't being copied -- why ??

many thanks
 

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
Silly question I know but have you got two text boxes on your form?

It will error out on this line if you haven't:

Code:
ws.Cells(iRow, 2).Value = Me.TextBox2.Value

Otherwise it works fine for me.

Nigel.
 
Upvote 0
Hi,

Yep def have two Textboxes on the form and no error is displayed ie the code runs fine, but it just doesn't copy the value in textbox2 to the worksheet :(
 
Upvote 0
Have you checked the names are correct?

You haven't deleted a box and then added another so now it's really 'textbox3' or something?
 
Upvote 0
I have just re-created a new userform using the same code and the same happens only Textbox1 is copied !!:(
 
Upvote 0
That's very odd.

All I did was to create a form, add two textboxes and a button, and copied your code into it. It worked fine for me.

Can anyone else help?

Thanks.
 
Upvote 0
:coffee:

I'm sooooooo sorry, I have just spotted my mistake........

The code works just fine.......I had the column that Textbox2 was being copied to hidden.!!!!!!!!

PLEASE accept my apologies for this rather embarrassing 'error'

But thanks for your help anyway !
 
Upvote 0
redspanna

I did exactly the same as nigelk did and didn't have any problems.

The only strange thing I found was that if I didn't enter anything in the first textbox, but something in the 2nd textbox data was still transferred to the ws.

Do you have any other code?
 
Upvote 0
redspanna

Don't apologise or whatever - we all make these mistakes.

I just wish there was a 'I'll get my coat smillie'.:)
 
Upvote 0

Forum statistics

Threads
1,215,352
Messages
6,124,455
Members
449,161
Latest member
NHOJ

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