.End(xlUp).Row + 1 not finding the last used row

ibruzzi

New Member
Joined
Jun 25, 2021
Messages
25
Office Version
  1. 365
  2. 2013
Platform
  1. Windows
I'm trying to enter data into two sheets at once. Everything works fine except in the sheet 2, it doesn't put data on the last used row, which should be A2. Instead, it keeps adding data from A13 and below. I deleted everything in the sheet and when I enter the data, it goes to A14, repeat the same thing, it goes to A15 and so on.
what am I doing wrong here? The sheet 1 works as it's supposed to.
couldn't find an answer to this on the site.

VBA Code:
Private Sub CommandButton2_Click()
Dim x As Long
Dim y, z As Worksheet

Set y = Sheet1
Set z = Sheet2

x = z.Range("A" & Rows.Count).End(xlUp).Row + 1
x = y.Range("A" & Rows.Count).End(xlUp).Row + 1


With z
.Cells(x, 1).Value = [Text(Now(), "MM-DD-YYYY")] 'DATE
.Cells(x, 2).Value = ComboBox3.Text 'DETAILS
.Cells(x, 3).Value = ComboBox5.Text 'DURATION
.Cells(x, 4).Value = TextBox2.Text 'WEIGHT
'.Cells(x, 5).Value = ComboBox2.Text 'TYPE
'.Cells(x, 6).Value = ComboBox1.Text 'DEPT
'.Cells(x, 8) = [Text(Now(), "DD-MM-YYYY HH:MM")]
End With

With y
.Cells(x, 1).Value = [Text(Now(), "MM-DD-YYYY")] 'DATE
.Cells(x, 2).Value = ComboBox3.Text 'DETAILS
.Cells(x, 3).Value = ComboBox5.Text 'DURATION
.Cells(x, 4).Value = TextBox2.Text 'WEIGHT
'.Cells(x, 5).Value = ComboBox2.Text 'TYPE
'.Cells(x, 6).Value = ComboBox1.Text 'DEPT
'.Cells(x, 8) = [Text(Now(), "DD-MM-YYYY HH:MM")]

End With

Unload Me
D001.Show

End Sub
 

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
68,086
Office Version
  1. 365
Platform
  1. Windows
You are using x for the last row in both sheets, so it will always use the last row in sheet1
 
Solution

ibruzzi

New Member
Joined
Jun 25, 2021
Messages
25
Office Version
  1. 365
  2. 2013
Platform
  1. Windows
You are using x for the last row in both sheets, so it will always use the last row in sheet1
I added
VBA Code:
Dim w As Long
and replaced one "x" with "w" and it did the same. 👀

Also tried

VBA Code:
Dim w As Long
Dim x As Long


VBA Code:
Private Sub CommandButton2_Click()
Dim x, w As Long

Dim y, z As Worksheet

Set y = Sheet1
Set z = Sheet2

w = z.Range("A" & Rows.Count).End(xlUp).Row + 1
x = y.Range("A" & Rows.Count).End(xlUp).Row + 1


With z
.Cells(x, 1).Value = [Text(Now(), "MM-DD-YYYY")] 'DATE
.Cells(x, 2).Value = ComboBox3.Text 'DETAILS
.Cells(x, 3).Value = ComboBox5.Text 'DURATION
.Cells(x, 4).Value = TextBox2.Text 'WEIGHT
'.Cells(x, 5).Value = ComboBox2.Text 'TYPE
'.Cells(x, 6).Value = ComboBox1.Text 'DEPT
'.Cells(x, 8) = [Text(Now(), "DD-MM-YYYY HH:MM")]
End With

With y
.Cells(x, 1).Value = [Text(Now(), "MM-DD-YYYY")] 'DATE
.Cells(x, 2).Value = ComboBox3.Text 'DETAILS
.Cells(x, 3).Value = ComboBox5.Text 'DURATION
.Cells(x, 4).Value = TextBox2.Text 'WEIGHT
'.Cells(x, 5).Value = ComboBox2.Text 'TYPE
'.Cells(x, 6).Value = ComboBox1.Text 'DEPT
'.Cells(x, 8) = [Text(Now(), "DD-MM-YYYY HH:MM")]

End With

Unload Me
D001.Show

End Sub
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
68,086
Office Version
  1. 365
Platform
  1. Windows
You are still using x for the row inside both With Statements, you need to change the 1st one from x to w
 

ibruzzi

New Member
Joined
Jun 25, 2021
Messages
25
Office Version
  1. 365
  2. 2013
Platform
  1. Windows
I added
VBA Code:
Dim w As Long
and replaced one "x" with "w" and it did the same. 👀

Also tried

VBA Code:
Dim w As Long
Dim x As Long


VBA Code:
Private Sub CommandButton2_Click()
Dim x, w As Long

Dim y, z As Worksheet

Set y = Sheet1
Set z = Sheet2

w = z.Range("A" & Rows.Count).End(xlUp).Row + 1
x = y.Range("A" & Rows.Count).End(xlUp).Row + 1


With z
.Cells(x, 1).Value = [Text(Now(), "MM-DD-YYYY")] 'DATE
.Cells(x, 2).Value = ComboBox3.Text 'DETAILS
.Cells(x, 3).Value = ComboBox5.Text 'DURATION
.Cells(x, 4).Value = TextBox2.Text 'WEIGHT
'.Cells(x, 5).Value = ComboBox2.Text 'TYPE
'.Cells(x, 6).Value = ComboBox1.Text 'DEPT
'.Cells(x, 8) = [Text(Now(), "DD-MM-YYYY HH:MM")]
End With

With y
.Cells(x, 1).Value = [Text(Now(), "MM-DD-YYYY")] 'DATE
.Cells(x, 2).Value = ComboBox3.Text 'DETAILS
.Cells(x, 3).Value = ComboBox5.Text 'DURATION
.Cells(x, 4).Value = TextBox2.Text 'WEIGHT
'.Cells(x, 5).Value = ComboBox2.Text 'TYPE
'.Cells(x, 6).Value = ComboBox1.Text 'DEPT
'.Cells(x, 8) = [Text(Now(), "DD-MM-YYYY HH:MM")]

End With

Unload Me
D001.Show

End Sub
oooohh.
You are still using x for the row inside both With Statements, you need to change the 1st one from x to w
ooohh I see that now lmao 🤦‍♂️ my bad. totally missed that. Thank you.
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
68,086
Office Version
  1. 365
Platform
  1. Windows
Glad to help & thanks for the feedback.
 
Master Excel Bundle

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

Forum statistics

Threads
1,152,111
Messages
5,768,163
Members
425,458
Latest member
Jaspal1996

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