VB code alternative

WAF875

New Member
Joined
Apr 17, 2016
Messages
24
Private Sub CommandButton1_Click()
If Range("A1") = 0 Then
Range("A1") = 1
Exit Sub
End If
If Range("A1") > 0 Then
Range("B1") = 1
Exit Sub
End If
If Range("B1") > 0 Then
Range("C1") = 1
Exit Sub
End If
If Range("C1") > 0 Then
Range("D1") = 1
Exit Sub
End If
End Sub

I have tried the above code to input"1" to the next cell each time I click the button but it only does A1 and B1 then stops could someone explain why and maybe come up with an alternative Thanks
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
It stops because you told it to.

If A1 > 0 Then B1 = 1...exit sub.

You want it to put one in the next cell that don't have one each time you click the button? Maybe:

Code:
[SIZE=1]Private Sub CommandButton1_Click()
Dim LastCol as Long
LastCol = Range("A1").End(xlToRight).Column
If LastCol > 4 Then Exit sub
Range("A" & LastCol + 1).Value = 1
End Sub[/SIZE]

That is totally untested as I have no excel at home, but I'm pretty sure that's what you need. Only line I'm not 100% about is defining the LastCol variable.
 
Last edited:
Upvote 0
This will start from B1 and then put a 1 in C1, D1 etc. (provided the rest of the row is blank). If you need it to start at A1 it needs a slight change.

Code:
lr = Cells(1, Columns.Count).End(xlToLeft).Column
Cells(1, lr + 1) = 1
 
Upvote 0
This will start from B1 and then put a 1 in C1, D1 etc. (provided the rest of the row is blank). If you need it to start at A1 it needs a slight change.

Code:
lr = Cells(1, Columns.Count).End(xlToLeft).Column
Cells(1, lr + 1) = 1

Thanks Steve
This is way beyond my excel knowledge but it works fine. I've noticed it starts after the last populated cell on the row inputted (1) so I should be able to work it in okay as I can't figure out how to make it start anywhere else.
Thanks Again
Alan
 
Upvote 0
It stops because you told it to.

If A1 > 0 Then B1 = 1...exit sub.

You want it to put one in the next cell that don't have one each time you click the button? Maybe:

Code:
[SIZE=1]Private Sub CommandButton1_Click()
Dim LastCol as Long
LastCol = Range("A1").End(xlToRight).Column
If LastCol > 4 Then Exit sub
Range("A" & LastCol + 1).Value = 1
End Sub[/SIZE]

That is totally untested as I have no excel at home, but I'm pretty sure that's what you need. Only line I'm not 100% about is defining the LastCol variable.

Thanks for the code but it doesn't seem to do anything when I click the button
Thanks Alan
 
Upvote 0
Yea, after a second look, I see why...

Cells(LastCol + 1, 1).Value = 1 in place of Range

Sorry about that. Glad Steve's code worked out for you :)
 
Upvote 0

Forum statistics

Threads
1,224,824
Messages
6,181,186
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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