Code doesn't go on a new line (emptyRow)

Patriot2879

Well-known Member
Joined
Feb 1, 2018
Messages
1,227
Office Version
  1. 2010
Platform
  1. Windows
Hi please can you help me with the code below, it doesnt seem to put data on a new line and go down 1 column. instead it overwrites the previous data once the command button is clicked on. Hope you can help me please?

VBA Code:
Private Sub CommandButton4_Click()
Dim emptyRow As Long

With ThisWorkbook.Sheets("Additional Job")
   emptyRow = Cells(Rows.Count, "A").End(xlUp).Row + 1

   .Cells(emptyRow, 3).Value = TextBox1.Value
   .Cells(emptyRow, 2).Value = TextBox10.Value
   .Cells(emptyRow, 1).Value = ComboBox2.Value
   .Cells(emptyRow, 5).Value = TextBox2.Value
   .Cells(emptyRow, 6).Value = TextBox11.Value
   .Cells(emptyRow, 7).Value = TextBox3.Value
   .Cells(emptyRow, 13).Value = ComboBox4.Value
   .Cells(emptyRow, 12).Value = ComboBox3.Value
   .Cells(emptyRow, 14).Value = TextBox4.Value
   .Cells(emptyRow, 15).Value = TextBox12.Value
   .Cells(emptyRow, 17).Value = TextBox5.Value
   .Cells(emptyRow, 18).Value = TextBox6.Value
   .Cells(emptyRow, 19).Value = TextBox7.Value
   .Cells(emptyRow, 20).Value = TextBox8.Value
   .Cells(emptyRow, 21).Value = TextBox9.Value
End With
Unload Me



End Sub
 

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
Your code is checking column A for the last non-empty cell, starting at the bottom. As soon as it finds a non-empty cell it adds 1 and uses that row. In the rest of your code, the value in column A is derived from the value in "ComboBox2". If nothing is selected in that combobox, nothing will get written into column A on your worksheet. In that case, it will keep overwriting that row until you DO pick a value in ComboBox2. At that point it should go to the next row when the button is clicked.

If you always want to make sure your data goes into a new row, check a column that will ALWAYS have a value after you click the button. For example, if TextBox1 will always have a value, check column 3 (C) instead:
VBA Code:
emptyRow = Cells(Rows.Count, "C").End(xlUp).Row + 1
 
Upvote 0
On this line
Excel Formula:
emptyRow = Cells(Rows.Count, "A").End(xlUp).Row + 1
you need to add a full stop before the word cells as you have on the other lines.
 
Upvote 0
Solution
Hi thanks for the reply, that’s so odd then as column A always has data in it, but still gets overwritten. I’d there anything else in the code that can be changed?
 
Upvote 0
Did you see my suggestion?
 
Upvote 0
Try
VBA Code:
emptyRow = .Cells(Rows.Count, "A").End(xlUp).Row + 1
instead of
VBA Code:
emptyRow = Cells(Rows.Count, "A").End(xlUp).Row + 1
 
Upvote 0
You're welcome & Thanks for the feedback.
 
Upvote 1

Forum statistics

Threads
1,215,072
Messages
6,122,968
Members
449,095
Latest member
Mr Hughes

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