Need Help on inputing values in a cell VBA

Fiske

Board Regular
Joined
Jun 15, 2015
Messages
82
Hi.
I am doing a code where once the command button is press, in open up an input box and asked to input the name after which will be key in the first cell and if input a second name when pressed the command button, it will automatically fill in the next empty cell of the same column.
However, every time input a name, i would fill in 10 cells rather than just one cell. Here is my code.

Private Sub CommandButton1_Click()
Dim name As String
Dim i As Integer


name = InputBox("name?")
For i = 1 To 10
Cells(i, 1).Value = name
Next

End Sub

Hope i could get help from here as i am still new to VBA.
thank you!
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
Great information Rick.
For those who might be interested, the button's Click event code can be written using one line of code...
Code:
Private Sub CommandButton1_Click()
  Cells(Rows.Count, "A").End(xlUp).Offset(-([A1] <> "")) = InputBox("Name?")
End Sub
 
Upvote 0
WOW! thanks for the info!! ^^
You are quite welcome... I am glad you found what I posted interesting.


Great information Rick.
Thank you for your kind comment. I notice you quoted my originally posted code. I am wondering, though, did you see the code I posted in Message #10... it is still a one-liner, but more compact.
 
Upvote 0
I see now your last post. showing it this way:
Code:
Private Sub CommandButton1_Click()
  Cells([MIN(IF(A:A="",ROW(A:A)))], 1) = InputBox("Name?")
End Sub
I'll use that next time I have the need. Thanks for all your sharing Rick.
 
Upvote 0

Forum statistics

Threads
1,215,734
Messages
6,126,543
Members
449,316
Latest member
sravya

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