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

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
Assuming you only want to fill one cell each time the user inputs a name:

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


name = InputBox("name?")
If name <>"" Then
If Isempty(Cells(1,1)) then
Cells(1, 1).Value= name
Else
Cells(rows.count,1).End(xlup).Offset(1,0).Value = name
End If
End IF
End Sub
 
Upvote 0
So do you want the entered name entered on your sheet one time or ten times?
Not sure what your question is?
 
Upvote 0
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
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
Actually, I think I like the following version of my one-liner... first, it is one characters shorter :biggrin:, and second, that "mysterious" minus sign in the Offset property is eliminated.
Code:
Private Sub CommandButton1_Click()
  Cells(Rows.Count, "A").End(xlUp).Offset([0+(A1<>"")]) = InputBox("Name?")
End Sub
 
Upvote 0
Actually, I think I like the following version of my one-liner... first, it is one characters shorter :biggrin:, and second, that "mysterious" minus sign in the Offset property is eliminated.
Code:
Private Sub CommandButton1_Click()
  Cells(Rows.Count, "A").End(xlUp).Offset([0+(A1<>"")]) = InputBox("Name?")
End Sub
Still a one-liner and 17 characters shorter than the short code above...
Code:
Private Sub CommandButton1_Click()
  Cells([MIN(IF(A:A="",ROW(A:A)))], 1) = InputBox("Name?")
End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,375
Messages
6,119,166
Members
448,870
Latest member
max_pedreira

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