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

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.

JoeMo

MrExcel MVP
Joined
May 26, 2009
Messages
18,144
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
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

My Aswer Is This

Well-known Member
Joined
Jul 5, 2014
Messages
19,343
Office Version
  1. 2021
Platform
  1. Windows
So do you want the entered name entered on your sheet one time or ten times?
Not sure what your question is?
 
Upvote 0

Fiske

Board Regular
Joined
Jun 15, 2015
Messages
82
ADVERTISEMENT
So do you want the entered name entered on your sheet one time or ten times?
Not sure what your question is?

Its like what joe assume, fill in a cell each time a user inputs a name.
Sorry that i did not explain my question properly. :eek:
 
Upvote 0

My Aswer Is This

Well-known Member
Joined
Jul 5, 2014
Messages
19,343
Office Version
  1. 2021
Platform
  1. Windows
ADVERTISEMENT
Glad to see you have it working
Its like what joe assume, fill in a cell each time a user inputs a name.
Sorry that i did not explain my question properly. :eek:
 
Upvote 0

Rick Rothstein

MrExcel MVP
Joined
Apr 18, 2011
Messages
38,154
Office Version
  1. 2019
  2. 2010
Platform
  1. Windows
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

Rick Rothstein

MrExcel MVP
Joined
Apr 18, 2011
Messages
38,154
Office Version
  1. 2019
  2. 2010
Platform
  1. Windows
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

Rick Rothstein

MrExcel MVP
Joined
Apr 18, 2011
Messages
38,154
Office Version
  1. 2019
  2. 2010
Platform
  1. Windows
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,195,590
Messages
6,010,611
Members
441,558
Latest member
lambierules

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
Top