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

ibruzzi

New Member
Joined
Jun 25, 2021
Messages
26
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 Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
You are using x for the last row in both sheets, so it will always use the last row in sheet1
 
Upvote 0
Solution
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
 
Upvote 0
You are still using x for the row inside both With Statements, you need to change the 1st one from x to w
 
Upvote 0
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.
 
Upvote 0

Forum statistics

Threads
1,223,888
Messages
6,175,219
Members
452,619
Latest member
Shiv1198

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